Skip to content
elphonoPublic

About

A simple cadencer that works like a metronome

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

13 Commits

Folders and files

Repository files navigation

Metronome Engine

A simple, platform-independent metronome engine for Java-based applications (Android, Web, Desktop, etc.).

Features

  • Adjustable Tempo: Set BPM (Beats Per Minute) from 20 to 300
  • Time Signatures: Support for any time signature (4/4, 3/4, 6/8, etc.)
  • Event-Driven: Implement BeatListener to receive beat events
  • Thread-Safe: Safe concurrent start/stop operations
  • Lightweight: No external dependencies (core library)
  • Platform-Independent: Pure Java implementation

Maven Dependency

Build the project:

mvn clean install

Add to your project:

<dependency>
    <groupId>com.elphono</groupId>
    <artifactId>metronome</artifactId>
    <version>1.0.0</version>
</dependency>

Usage Example

import com.elphono.metronome.Metronome;
import com.elphono.metronome.BeatListener;
import com.elphono.metronome.TimeSignature;

public class Example {
    public static void main(String[] args) throws InterruptedException {
        // Create a metronome with 120 BPM, 4/4 time
        Metronome metronome = new Metronome(120, new TimeSignature(4, 4));
        
        // Add a listener to respond to beats
        metronome.addBeatListener(new BeatListener() {
            @Override
            public void onBeat(int beatNumber, boolean isAccent) {
                if (isAccent) {
                    System.out.println("TICK (Beat " + beatNumber + ")");
                } else {
                    System.out.println("tick (Beat " + beatNumber + ")");
                }
            }
        });
        
        // Start the metronome
        metronome.start();
        
        // Let it run for 5 seconds
        Thread.sleep(5000);
        
        // Change tempo while running
        metronome.setBpm(140);
        
        // Run for another 5 seconds
        Thread.sleep(5000);
        
        // Stop the metronome
        metronome.stop();
    }
}

API Reference

Metronome Class

Constructors:

  • Metronome() - Creates a metronome with default settings (120 BPM, 4/4 time)
  • Metronome(int bpm, TimeSignature timeSignature) - Creates a metronome with specified settings

Methods:

  • void start() - Starts the metronome
  • void stop() - Stops the metronome
  • void setBpm(int bpm) - Sets the tempo (20-300 BPM)
  • int getBpm() - Gets the current tempo
  • void setTimeSignature(TimeSignature ts) - Sets the time signature
  • TimeSignature getTimeSignature() - Gets the current time signature
  • boolean isRunning() - Checks if the metronome is running
  • void addBeatListener(BeatListener listener) - Adds a beat listener
  • void removeBeatListener(BeatListener listener) - Removes a beat listener

BeatListener Interface

Methods:

  • void onBeat(int beatNumber, boolean isAccent) - Called on each beat
    • beatNumber - The current beat in the measure (1-based)
    • isAccent - True for the first beat of each measure

TimeSignature Class

Constructor:

  • TimeSignature(int beatsPerMeasure, int beatUnit) - Creates a time signature
    • Example: new TimeSignature(4, 4) for 4/4 time
    • Example: new TimeSignature(3, 4) for 3/4 time
    • Example: new TimeSignature(6, 8) for 6/8 time

Methods:

  • int getBeatsPerMeasure() - Gets the number of beats per measure
  • int getBeatUnit() - Gets the beat unit

Building the Project

# Compile
mvn compile

# Run tests
mvn test

# Build JAR
mvn package

The JAR file will be created in target/metronome-1.0.0.jar.

Platform Integration Examples

Android Integration

Implement BeatListener and use platform-specific sound playback:

public class AndroidMetronomeActivity extends Activity {
    private Metronome metronome;
    private SoundPool soundPool;
    private int tickSound;
    private int tockSound;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // Initialize sound pool
        soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
        tickSound = soundPool.load(this, R.raw.tick, 1);
        tockSound = soundPool.load(this, R.raw.tock, 1);
        
        // Create metronome
        metronome = new Metronome(120, new TimeSignature(4, 4));
        metronome.addBeatListener(new BeatListener() {
            @Override
            public void onBeat(int beatNumber, boolean isAccent) {
                if (isAccent) {
                    soundPool.play(tockSound, 1.0f, 1.0f, 1, 0, 1.0f);
                } else {
                    soundPool.play(tickSound, 1.0f, 1.0f, 1, 0, 1.0f);
                }
            }
        });
    }
    
    public void startMetronome() {
        metronome.start();
    }
    
    public void stopMetronome() {
        metronome.stop();
    }
}

Web/JavaScript Integration (via GWT or similar)

public class WebMetronome {
    private Metronome metronome;
    
    public WebMetronome() {
        metronome = new Metronome();
        metronome.addBeatListener(new BeatListener() {
            @Override
            public void onBeat(int beatNumber, boolean isAccent) {
                // Call JavaScript function
                playBeatSound(beatNumber, isAccent);
            }
        });
    }
    
    private native void playBeatSound(int beatNumber, boolean isAccent) /*-{
        if (isAccent) {
            $wnd.playTock();
        } else {
            $wnd.playTick();
        }
    }-*/;
}

License

This is a simple metronome engine project.

Contributing

Feel free to submit issues and pull requests

About

A simple cadencer that works like a metronome

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages