# Audio Signal still going after AudioSample loop stopped

**URL:** <https://discourse.processing.org/t/audio-signal-still-going-after-audiosample-loop-stopped/10589>\
**Category:** Libraries\
**Created:** [April 24, 2019, 1:55am UTC](https://discourse.processing.org/t/audio-signal-still-going-after-audiosample-loop-stopped/10589 "2019-04-24T01:55:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aeggy](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@Aeggy](https://discourse.processing.org/u/Aeggy)\
**Post date:** [April 24, 2019, 1:55am UTC](https://discourse.processing.org/t/audio-signal-still-going-after-audiosample-loop-stopped/10589/1 "2019-04-24T01:55:05Z")

</div>

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

```auto
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).

![](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/ca10d36aadb25d3a86d3c89b49294c0199ebc870.jpeg)

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

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [April 24, 2019, 6:54am UTC](https://discourse.processing.org/t/audio-signal-still-going-after-audiosample-loop-stopped/10589/2 "2019-04-24T06:54:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [April 24, 2019, 7:10am UTC](https://discourse.processing.org/t/audio-signal-still-going-after-audiosample-loop-stopped/10589/3 "2019-04-24T07:10:06Z")

</div>

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

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

```

here the more featured code for example.

```auto
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](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/b9fa843c20c00fb81b22c98cf03cc1cbaf26f631.jpeg)
