Data update
This commit is contained in:
parent
61b93a2cd1
commit
5af6d93694
858 changed files with 20572 additions and 2082 deletions
41
Task/Metronome/C++/metronome.cpp
Normal file
41
Task/Metronome/C++/metronome.cpp
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
|
||||
class Metronome {
|
||||
public:
|
||||
Metronome(const int32_t& aBeats_per_minute, const int32_t& aMeasure, const int32_t& aDuration_in_minutes)
|
||||
: beats_per_minute(aBeats_per_minute), measure(aMeasure), duration_in_minutes(aDuration_in_minutes) {
|
||||
counter = 0;
|
||||
}
|
||||
|
||||
void start() {
|
||||
while ( counter < duration_in_minutes * beats_per_minute ) {
|
||||
start_time = std::chrono::system_clock::now();
|
||||
|
||||
std::this_thread::sleep_until(time_to_awake());
|
||||
counter++;
|
||||
if ( counter % measure != 0 ) {
|
||||
std::cout << "Tick " << std::flush;
|
||||
} else {
|
||||
std::cout << "Tock" << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::chrono::system_clock::time_point time_to_awake() const {
|
||||
return start_time + std::chrono::seconds(1);
|
||||
}
|
||||
|
||||
std::chrono::system_clock::time_point start_time;
|
||||
int32_t counter;
|
||||
|
||||
const int32_t beats_per_minute, measure, duration_in_minutes;
|
||||
};
|
||||
|
||||
int main() {
|
||||
Metronome metronome(60, 4, 1);
|
||||
metronome.start();
|
||||
}
|
||||
113
Task/Metronome/Java/metronome-2.java
Normal file
113
Task/Metronome/Java/metronome-2.java
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
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;
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue