Skip to content

Instantly share code, notes, and snippets.

@kbastani
Created June 12, 2023 04:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kbastani/1ccf4e35486b7dcf9aeae05380d61949 to your computer and use it in GitHub Desktop.
Save kbastani/1ccf4e35486b7dcf9aeae05380d61949 to your computer and use it in GitHub Desktop.
This algorithm demonstrates how to measure ambient noise pressure to optimally and more naturally use natural voice conversations with chatbots

Here's the code with detailed comments explaining each step:

public void metrics() {
    new Thread(() -> {
        while (true) {
            // Calculate and print the average loudness
            System.out.println("Average loudness: " + MOVING_AVERAGE_LOUDNESS.getAverage());

            // Find the maximum value in the moving average loudness
            MOVING_AVERAGE_LOUDNESS.toStream().max(Comparator.comparingDouble(value -> value)).ifPresent(max -> {
                System.out.println("Max: " + max);
                // Set the max value of the gauge to the max value of the moving average
                gauge.getScale().getDomain()[1] = max;
                MOVING_AVERAGE_AMBIENT_NOISE.add(max);
            });

            // Find the minimum value in the moving average loudness
            MOVING_AVERAGE_LOUDNESS.toStream().min(Comparator.comparingDouble(value -> value)).ifPresent(min -> {
                // Set the min value of the gauge to the min value of the moving average
                System.out.println("Min: " + min);
                gauge.getScale().getDomain()[0] = min;
                MOVING_AVERAGE_AMBIENT_DECAY.add(min);
            });

            // Set the decay rate of the gauge to the absolute value of the average ambient decay
            gauge.setDecayRate(Math.abs(MOVING_AVERAGE_AMBIENT_DECAY.getAverage()));

            // Set the pedal force of the gauge to the absolute value of the average loudness
            gauge.setPedalForce(Math.abs(MOVING_AVERAGE_LOUDNESS.getAverage()));

            // Print the current pedal force, decay rate, and noise pressure (gauge value)
            System.out.println("Pedal force: " + gauge.getPedalForce());
            System.out.println("Decay rate: " + gauge.getDecayRate());
            System.out.println("Noise pressure: " + gauge.getGaugeValue());

            // Update the gauge value using a fixed value of 0.8
            gauge.update(0.8);

            // If the gauge value becomes NaN, set it to a default value of 0.1
            if (Double.isNaN(gauge.getGaugeValue())) {
                gauge.setGaugeValue(0.1);
            }

            // Pause the thread for 60 milliseconds before the next iteration
            try {
                Thread.sleep(60);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }).start();
}

In this code, we have a background thread that continuously collects and analyzes metrics of the positive noise pressure in the room to optimize audio interference cancellation for hands-free voice-to-voice ChatBot conversations. Here's an overview of the steps:

  1. Calculate and print the average loudness.
  2. Find the maximum value in the moving average loudness and update the max value of the gauge.
  3. Find the minimum value in the moving average loudness and update the min value of the gauge.
  4. Set the decay rate of the gauge to the absolute value of the average ambient decay.
  5. Set the pedal force of the gauge to the absolute value of the average loudness.
  6. Print the current pedal force, decay rate, and noise pressure (gauge value).
  7. Update the gauge value using a fixed value of 0.8.
  8. If the gauge value becomes NaN, set it to a default value of 0.1.
  9. Pause the thread for 60 milliseconds before the next iteration.

This code continuously adapts the gauge based on the metrics and applies the appropriate pedal force and decay rate to optimize audio interference cancellation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment