Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9080715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:08:14+00:00 2026-06-16T20:08:14+00:00

as an excercise I’m trying to create a metronome with Java using Thread.sleep as

  • 0

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! 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T20:08:15+00:00Added an answer on June 16, 2026 at 8:08 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create my own templated List class as a practice excercise. I
I am learning Java, now working with arrays. I am doing an excercise where
Im trying to unit test a class in some Java code that I've inherited.
I'm trying to learn Objective-C. I almost finished one excercise but it is not
I am currently trying to learn JavaScript and in a particular excercise, I am
I'm having more and more trouble while trying to make an easy excercise. i'm
I'm writing a skiplist class in java as an excercise. I've written a class
I am new to using threads, and I thought a good excercise would be
So, I'm trying to create a page that reads the specific XML file on
I'm trying to solve the count a sequence excercise at 4clojure.com . The excercise

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.