What i'm trying to do is making a piano song with individual sound files for the piano claps. I did a version of it in javascript, with WebAudioAPI, using an oscillator instead of audio files. Here's a part of the code out of context:
var ctx = new AudioContext();
var time = ctx.currentTime;
for(...){
osc = ctx.createOscillator();
osc.start(time);
time = time + 3;
osc.stop(time);
osc.disconnect(time);
}
I don't know how to stop an audio file like i did with the oscillator. (P.S.)I only kept the part of the code that was relevant to my question, so please ignore the functionality of it.
Thank you!
With a Clip one uses the methodstop()
to halt the playback. A subsequently called start()
will resume playback from the same point. But one can change the playback point to a new location with either the setFramePosition()
or setMicrosecondPosition()
methods.
With a SourceDataLine one can also use start()
and stop()
methods. But changing to a new playback point is limited and trickier. One can only move forward, and this is done by using the skip() method of the AudioInputStream
that is used to obtain the data being output through the SourceDataLine
.
If the sound you are playing is only meant to be played once through, I would tend to favor using the SourceDataLine
. If you are going to play the sound over, repeatedly, and holding the data in memory is not a burden, then Clip
would likely be the better choice.
With SourceDataLine
you are not limited to using AudioInputStream
as your source signal. You can also use a custom-made oscillator or synthesizer. Converting the output of the oscillator, if it is PCM, will have to be handled to match the audio format in that case.