Audio Signal still going after AudioSample loop stopped

Here I’m playing a 440Hz sine wave in an audio sample on loop.

import processing.sound.*;
AudioSample sample;

void setup() {
  int resolution = 1000;
  float[] sinewave = new float[resolution];
  for (int i = 0; i < resolution; i++) {
  sinewave[i] = sin(TWO_PI*i/resolution);
  }
  sample = new AudioSample(this, sinewave, false, int(440 * resolution));
  sample.amp(0.5);
  sample.loop();
}

void draw() {}


void keyReleased() {
  if (key == ' ') {
    sample.stop();
  }
}

It’s set up so that the loop will stop once you press and release the space bar. However, there’s still an inaudible sound signal that’s coming through (too high or low to hear I believe).

Not sure why it doesn’t stop completely. Any clue?

1 Like

Any good audio library will keep the connection to the sound card open like that so that it’s ready to use - setting up the output takes time.

Do other applications outputting silence show any signal? That’s potentially odder. Maybe it is just silence or the audio output is dithered and you’re seeing that.

Either way I wouldn’t worry about it. Why does it concern you? Maybe there’s a way to uninitialize the audio library if it’s a problem.

1 Like

just for confirm,
win 7 / 64bit
processing 3.5.3
sound lib 2.1.0

i tested with a code where i can loop/stop the sample easily
and found that the remaining amplitude in the windows volume mixer
is random

you can avoid that by first

sample.amp(0);
sample.stop();

here the more featured code for example.

import processing.sound.*;
AudioSample sample;
Waveform waveform;

int resolution = 4;                    // samples per sinus  ( 4 is just triangle )
int samples = 100;                     // for waveform
float[] sinewave = new float[resolution];
int freq = 440;
int amp  =  50; //0 .. 100 %

void setup() {
  for (int i = 0; i < resolution; i++) sinewave[i] = sin(TWO_PI*i/resolution);
  sample = new AudioSample(this, sinewave, false, freq * resolution);
  sample.amp(0.5);
  sample.loop();

  waveform = new Waveform(this, samples);
  waveform.input(sample);

  println("SFSampleRate= " + sample.sampleRate() + " Hz");
  println("SFSamples= " + sample.frames() + " samples");
  println("SFDuration= " + sample.duration() + " seconds");
}

void draw() {
  background(200,200,0);
  noFill();
  waveform.analyze();   // Perform the analysis
  beginShape();
  for (int i = 0; i < samples; i++)
    vertex( map(i, 0, samples, 0, width),  map(waveform.data[i], -1, 1, 0, height) );
  endShape();
}

void keyPressed() {
  if (key == ' ') {
    if ( sample.isPlaying() ) {
      sample.amp(0);
      sample.stop();
    } else {
      sample.amp(amp/100.0);      
      sample.loop();
    }
  }
  int oldamp = amp;
  if ( key == '+' ) amp++;
  if ( key == '-' ) amp--;
  if ( oldamp != amp ) {
    amp = constrain(amp, 0, 100);
    println("amp% "+amp);
    sample.amp(amp/100.0);
  }
}


p.s. i might have just answered to use a better sound lib like
minim, lucky i did not.
not in this case, on windows 7 that sound volume mixer of a minim project is a mess!
( but possibly my code?? )
SNAG-0084

2 Likes