More calls per second for more accurate timing, sequencer, threads

Hey there,

i like to build a lil sequencer app for triggering sounds.
most examples i checked are using the graphic card draw thread to trigger samples which in my opinion isn’t a good way doing this because the thread is only up t0 60 fps or so…
is there another way of getting a higher resolution on the time axis without making an own thread?

and if i have to use the thread solution, is there a simple way to let the thread sleep shorter than 1 millisecond?
would it be a good idea to use the audio thread for my needs?

if you are working with sound you should be using the sound thread to calculate the timing. every time you get a callback you know which time it is from the tick count.

timepertick = (buffersize*1000)/samplerate; //in milliseconds
now = tickCount * timepertick;

this is the most accurate because you generate the sound in the exact time it’ll be rendered by the sound card

thanks arturo,

I am now using the ofSopundStream to trigger my trigger.

works great so far!

//--------------------------------------------------------------
void ofApp::audioOut(ofSoundBuffer &outBuffer) {

        timepertick = (buffersize*1000)/samplerate; //in milliseconds
        now = tickCount * timepertick;
	myTrigger.call(now);
}

hey there again,

i tried now to play sounds directly from within the audioOut function but having some problems.

When doing like following:

void ofApp::audioOut(ofSoundBuffer &outBuffer) { 
 
    tickCount++; 
    float  timepertick = (bufferSize*1000)/sampleRate; //in milliseconds 
  now = tickCount * timepertick; 
    //myTrigger3.call(tickCount); 
 
    if(now % 8 == 0) 
        sound.play(); 
}

My sound is panned to one speaker and the play speed seems not straight. There are always some ticks missing and the speed seems to change slightly. I am playing a really short tick sound. Any ideas on this?
I am trying to build a rudimentary sequencer which plays sounds as tightly timed like ableton live or similar daws.

i thought you were using sound stream only. ofSoundPlayer works on a different system and thread than soundstream so doing things like this won’t help and will probably be problematic.

if you want something really accurate you need to use soundstream alone and play the samples manually by using something like ofxMaxim. then the timming will be accurate to the sample using this method

sound player is just meant to play small sounds for things like video game events or long sounds for example for a background music but for something like a sequencer if you want somehting as accurate as live then you need to do the calculations your self start sending the samples to the sound card exactly when it corresponds. the new ofSoundBuffer class in 0.9 can help managing raw audio like this

Ok, I thought that it is like you said. Unfortunately ofxMaxim is making strange noises under my linux setup. Maybe you have an idea on this. The new ofSoundBuffer sounds great. Maybe i could use this in combination with libsoundfile… Or i will try ofxStk.

yes using it in combination with libsoundfile is a good idea, we are planing to have an ofSoundFile class for next release that will do just that.

some time ago i started an ofBasicSoundPlayer: https://github.com/arturoc/openFrameworks/blob/feature-ofSoundRefactoring/libs/openFrameworks/sound/ofBasicSoundPlayer.cpp that uses ofSoundStream + libsndfile + ofSoundBuffer. you can probably use it with 0.9 with minor changes. if you want really tight control over the time, (sub buffer size) the you probably want to get the sound on an ofSoundBuffer and send it to the sound card manually but the code to load a sound file onto an ofSoundBuffer should be the same

With next release you mean 0.9 or something after that? I will check your code! Sounds great. Thanks!

Hey,
I was also working with arturo and some other guys in this. Since it has been left aside for a while I decided to move it into an addon. It’s ready to use with OF0.9 and it already has implemented the ofSoundFile and ofBasicSoundPlayer that arturo mentioned.

Hope that it help.
Best!

:slight_smile: great! thanks!