5

as an excercise I'm trying to create a metronome with Java using Thread.sleep as a timer and JMF for sounds. It's working out quite well but for some reason JMF seems to be only playing sounds at a maximum of 207 beats per minute.

From my Metronome-class:

public void play() {
    soundPlayer.play();
    waitPulse();        
    play();
}

From my SoundPlayer-class:

public void play() {
    new Thread(new ThreadPlayer()).start();
}

private class ThreadPlayer implements Runnable {
    public void run() {
        System.out.println("Click");
        player.setMediaTime(new Time(0));
        player.start();
    }   
}

I've made SoundPlayer.play() work as a thread to test if it would make a difference, but it's not. I can easily change tempos up to about 207bpm but even if I set my timer to 1000bpm the sounds aren't played any faster than about 207bpm.

I've put the System.out.println("Click"); inside my ThreadPlayer.run() to check if my loop is working correctly – it is.

The issue seems to be with my implementation of JMF. I'm pretty sure there is a simple solution, can anybody help me out?

Thank you so much for your help! :)

Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
Macks
  • 1,696
  • 5
  • 23
  • 38

5 Answers5

10

The answer about Thread.sleep() being unreliable is correct: you can't count on it to return in exactly the amount of time you specify. In fact, I'm rather surprised your metronome is usable at all, especially when your system is under load. Read the docs for Thread.sleep() for more details. Max Beikirch's answer about MIDI is a good suggestion: MIDI handles timing very well.

But you ask how to do this with audio. The trick is to open an audio stream and fill it with silence between the metronome clicks and insert the metronome clicks where you want. When you do that, your soundcard plays back the samples (whether they contain a click or silence) at a constant rate. The key here is to keep the audio stream open and never close it. The clock, then, is the audio hardware, not your system clock -- a subtle but important distinction.

So, let's say you are generating 16 bit mono samples at 44100 Hz. Here is a function that will create a click sound at the requested rate. Keep in mind that this click sound is bad for speakers (and your ears) so if you actually use it, play it at a low volume. (Also, this code is untested -- it's just to demonstrate the concept)

int interval = 44100; // 1 beat per second, by default
int count = 0;
void setBPM( float bpm ) {
    interval = ( bpm / 60 ) * 44100 ;
}
void generateMetronomeSamples( short[] s ) {
    for( int i=0; i<s.length; ++i ) {
       s = 0;
       ++count;
       if( count == 0 ) {
          s = Short.MAX_VALUE;
       }
       if( count == interval ) {
          count = 0;
       }
    }
}

Once you set the tempo with setBPM, you can play back the samples generated by calling the the generateMetronomeSamples() function repeatedly, and streaming that output to your speakers using JavaSound. (see JSResources.org for a good tutorial)

Once you have that working, you could replace the harsh click with a sound you get from a WAV or AIFF or a short tone or whatever.

Bjorn Roche
  • 11,279
  • 6
  • 36
  • 58
  • Hi Bjorn, thank you for your comprehensive answer. This is more complicated than I thought but I guess it makes sense. I'm just wondering if this is the go-to-approach for time-sensitive audio playback. Or to put it differently: is there a reliable way of dealing with timing issues without this low-level stuff? Something like Marko suggested? (`ScheduledExecutorService.scheduleAtFixedRate`) I knew that Thread.sleep() wasn't the most reliable solution but I hadn't encountered any hardcore issues yet so I figured I would come back to timing issues once I got the actual sound playback working. – Macks Jan 04 '13 at 22:43
  • Ok, so I'm learning what you are suggesting. This is the tutorial I am using to get into the whole sampling-topic (for anyone interested): http://www.developer.com/java/other/article.php/2226701/Java-Sound-Creating-Playing-and-Saving-Synthetic-Sounds.htm I'll see how far I can get, thank you again! – Macks Jan 05 '13 at 01:41
  • While you might get better behavior with ScheduledExecutorService.scheduleAtFixedRate() than Thread.sleep() in some ways, ultimately there are a number of problems with both approaches. It boils down to thread scheduling, which is a complex issue in modern OSes. In some OSes, you can create conditions where you get good accuracy using these kinds of timers, but you are usually limited to around 1 ms, in the best of cases, and that involves doing things like running at the highest priority, setting the OS in "low latency" mode, never doing any sort of IO, or blocking, and so on. – Bjorn Roche Jan 05 '13 at 04:09
  • Ultimately, the method I describe above is how music applications, like ProTools, Audacity, and Ableton Live keep audio in sync. It might seem like overkill for a metronome, but as you've discovered, it's not. Gaming applications sometimes do something more or less like your approach -- opening extra "streams" as needed to make extra explosion sounds or whatever -- but when they do, they aren't trying to keep a rhythm going, just make extra sounds in response to something happening in the game. – Bjorn Roche Jan 05 '13 at 04:15
  • Thank you Bjorn for the insight, that was extremely helpful! :) – Macks Jan 05 '13 at 10:34
1

Take your time and take a look at MIDI! - http://www.ibm.com/developerworks/library/it/it-0801art38/ or http://docs.oracle.com/javase/tutorial/sound/TOC.html . It's the best solution for everything related to computer made sound.

Max Beikirch
  • 2,053
  • 5
  • 25
  • 36
  • Hi Max, thank you for your answer. Maybe MIDI is a better solution, but it has to be possible to make a metronome-like app with wav-files. Any drum-machine does this. – Macks Jan 04 '13 at 21:17
1

My assumption would be, and maybe someone else can jump in here, is that thread running times are up to the whims of the thread scheduler. You can't guaranty how long it will take for the JVM to get back to that thread. Also, seeing as the JVM is running as a process on the machine, and is subject to the OS's process scheduler, you are looking at at least two levels of unpredictability.

Jamie Duby
  • 76
  • 3
  • Hi Jamie, thank you for your answer. I don't understand what you are getting at – I have no problem with thread run times (yet). I have measured them to be as accurate as ± 2ms in my scenario. Am I missing something? – Macks Jan 04 '13 at 21:18
1

Just like Jamie Duby said, just because you tell the Thread to sleep 1 millisecond, doesn't mean that it will get called back at exactly one millisecond. The ONLY guarantee is that AT LEAST one millisecond has passed since you called Thread.sleep();. In reality, the processor cannot process the code fast enough to play a beep sound every millisecond, which is why you see the delay. If you want a dramatic example, make a home-made timer class and try making it count one millisecond for a full minute, you will see that the timer is off by quite a bit.

The person who really deserves the answer credit here is Max Beikrich, Midi is the only way you are going to be able to produce the output you are looking for.

John
  • 3,769
  • 6
  • 30
  • 49
  • 1
    I don't understand. How do drum machines and virtual software instruments handle 32nd-notes that are being played at 200bpm? They don't play MIDI-sounds. They play samples from wav-files. Also we're not talking about playing sounds every **millisecond**. More like playing sounds at like 1000 beats per **minute**. Am I completely missing a point here? – Macks Jan 04 '13 at 21:38
  • Theres a couple ways they could do it, they could have predefined files for say 100bpm, 95bpm, 90bpm... and then repeat those depending on what the user has requested (there is not a noticable difference between say 100 bpm and 98 bmp) Those files might only contain say one beat per file, so that would be 4 quarter beats or 8 8th note beats and so on. – John Jan 04 '13 at 21:45
  • Hi John, thank you for your help. I definitely want to work out a more elegant solution than pre-recorded audio-files. Especially because I want the flexibility to have the possibility of transforming it into a drum machine that can play any rhythm. Do you have an idea in which direction I should be looking for my problem? Thank you! :) – Macks Jan 04 '13 at 22:48
0

I have far more experience as a musician than a programmer but I just finished a metronome application I started a while back, I had put the project on hold for a while because I couldn't figure out why I was having the same problem you are. Yes Thread.sleep() can be unreliable but I'm managed to make a good metronome using that method.

I saw you mentioned trying an ExecutorService I don't think using concurrent classes will solve your problem. My guess is its a system resource issue, I'm pretty certain MIDIs are the way to go with a metronome. I force my students to practice with a metronome and I've used many, I haven't ever been too concerned with the audio quality of the ticks, timing is far more important and MIDIs are going to be much faster than any other audio file. I used the javax.sound.midi library from the Sound API. I suspect that will solve your problem.

You may notice your ticks are uneven when it works properly, this is due to the Thread.sleep() method not being very reliable. How I solved that problem was by doing all my calculations in nanoseconds using the System.nanoTime() method instead of the System.currentTimeMillis() method, just don't forget to convert back to milliseconds before passing the sleep time into the Thread.sleep() method.

I don't want to post the code for my metronome here in case you want to figure it out on your own but if you'd like to see it just send me an e-mail kevin.bigler3@gmail.com and I'd be happy to send it to you. Good luck.

Kevin Bigler
  • 256
  • 1
  • 2
  • 9