Metronome: Difference between revisions

5,011 bytes added ,  2 months ago
added RPL
(New post.)
(added RPL)
 
(5 intermediate revisions by 3 users not shown)
Line 265:
return 0;
}</syntaxhighlight>
 
 
=={{header|C#}}==
{{trans|Java}}
<syntaxhighlight lang="C#">
using System;
using System.Threading;
 
public class Program
{
public static void Main(string[] args)
{
Metronome metronome1 = new Metronome(120, 4);
metronome1.Start();
}
}
 
public class Metronome
{
private double bpm;
private int measure;
private int counter;
 
public Metronome(double bpm, int measure)
{
this.bpm = bpm;
this.measure = measure;
}
 
public void Start()
{
Thread thread = new Thread(() =>
{
while (true)
{
try
{
Thread.Sleep((int)(1000 * (60.0 / bpm)));
}
catch (ThreadInterruptedException e)
{
Console.WriteLine(e.StackTrace);
}
counter++;
if (counter % measure == 0)
{
Console.WriteLine("TICK");
}
else
{
Console.WriteLine("TOCK");
}
}
});
 
thread.Start();
}
}
</syntaxhighlight>
 
 
=={{header|C++}}==
Line 276 ⟶ 336:
class Metronome {
public:
Metronome(const doubleint32_t& aBeats_per_minute, const int32_t& aMeasure, const int32_t& aDuration_in_minutes)
: beats_per_minute(aBeats_per_minute), measure(aMeasure), durationduration_in_minutes(aDuration_in_minutes) {
counter = 0;
}
 
void start() {
while ( counter < durationduration_in_minutes * beats_per_minute ) {
start_time = std::chrono::system_clock::now();
 
Line 303 ⟶ 363:
int32_t counter;
 
const doubleint32_t beats_per_minute, measure, duration_in_minutes;
const int32_t measure, duration;
};
 
Line 1,029 ⟶ 1,088:
metronome1.start();
}
}
</syntaxhighlight>
 
===AudioVisual===
This example provides an audible and visual indications of the metronome timing.
 
It uses a timing loop to ensure that its timing is reliable in the long term.
<syntaxhighlight lang="java">
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
 
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
 
public final class MetronomeTask {
 
public static void main(String[] aArgs) {
EventQueue.invokeLater( () -> { new Metronome(60, 4, 1).start(); } );
}
 
}
 
final class Metronome extends JPanel {
public Metronome(int aBeatsPerMinute, int aMeasure, int aDurationInMinutes) {
beatsPerMinute = aBeatsPerMinute; measure = aMeasure; durationInMinutes = aDurationInMinutes;
SoundEffect.initialise();
createAndShowGUI();
}
public void start() {
executorService = Executors.newSingleThreadScheduledExecutor();
executorService.scheduleAtFixedRate(provideService, 1, 1, TimeUnit.SECONDS);
}
private void createAndShowGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
frame = new JFrame("Metronome");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setIconImage( new ImageIcon("./metronomeJava.png").getImage() );
frame.add(createPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
}
private JPanel createPanel() {
setPreferredSize( new Dimension(800, 600) );
setBackground(Color.CYAN);
return this;
}
private JFrame frame;
private ScheduledExecutorService executorService;
private int beatsPerMinute, measure, durationInMinutes, counter;
private Runnable provideService = () -> {
if ( counter < durationInMinutes * beatsPerMinute ) {
counter++;
if ( counter % measure != 0 ) {
SoundEffect.Tick.play();
if ( getBackground() != Color.PINK ) {
setBackground(Color.PINK);
} else {
setBackground(Color.CYAN);
}
} else {
SoundEffect.Tock.play();
setBackground(Color.ORANGE);
}
} else {
executorService.shutdown();
frame.dispose();
Runtime.getRuntime().exit(0);
}
};
}
 
enum SoundEffect {
Tick("./metronomeTickJava.wav"), Tock("./metronomeTockJava.wav");
public static void initialise() {
values();
}
public void play() {
if ( clip.isRunning() ) {
clip.stop();
}
clip.setFramePosition(0);
clip.start();
}
private SoundEffect(String soundFileName) {
URL url = getClass().getClassLoader().getResource(soundFileName);
try ( AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(url) ) {
clip = AudioSystem.getClip();
clip.open(audioInputStream);
} catch (IOException | LineUnavailableException | UnsupportedAudioFileException ex) {
ex.printStackTrace();
}
}
private Clip clip;
}
</syntaxhighlight>
Line 2,179 ⟶ 2,358:
end /*until et≥dur*/
/*stick a fork in it, we're all done. */</syntaxhighlight>
 
=={{header|RPL}}==
« .02 0 → pattern duration beat
« -56 CF <span style="color:grey">''@ sound ON'' </span>
60 SWAP / duration - →NUM
'''DO''' "Beat!" 1 DISP
440 'beat' INCR pattern MOD 1 2 IFTE * duration BEEP
CLLCD DUP WAIT
'''UNTIL''' KEY '''END''' DROP2
» » '<span style="color:blue">METRO</span>' STO <span style="color:grey">''@ ( bpm pattern → )'' </span>
 
=={{header|Ruby}}==
Line 2,284 ⟶ 2,473:
{{trans|Kotlin}}
Modified to ring the bell on each beat.
<syntaxhighlight lang="ecmascriptwren">import "timer" for Timer
import "io" for Stdout
 
1,150

edits