~yujiri/yuki

2ce8298036afd1558e865c719696efdd7f9de66e — Evin Yulo 1 year, 9 months ago f869726 master
make it real!
1 files changed, 37 insertions(+), 106 deletions(-)

M src/main.rs
M src/main.rs => src/main.rs +37 -106
@@ 1,7 1,7 @@
#![feature(decl_macro)]
use raylib::prelude::*;
use alsa::{Direction, ValueOr};
use alsa::pcm::{PCM, HwParams, Format, Access};
use std::env;

fn main() {
	let (mut rl, thread) = raylib::init().title("yuki").build();


@@ 10,121 10,52 @@ fn main() {
	set_hw_params(&pcm);
	let io = pcm.io_i16().unwrap();

	let tet = env::args().nth(1).map(|s| s.parse().unwrap()).unwrap_or(19.0);
	let base_freq: f32 = 440.0;
	let octave: f32 = 2.0;
	let semitone: f32 = octave.powf(1.0/19.0);
	let semitone: f32 = octave.powf(1.0/tet);

	rl.set_target_fps(100);
	let mut wave1_index = 0;
	let mut wave2_index = 0;
	let mut wave3_index = 0;
	let mut wave4_index = 0;
	let mut wave5_index = 0;
	let mut wave6_index = 0;
	let mut wave7_index = 0;
	let mut wave8_index = 0;
	let mut wave9_index = 0;
	let mut wave10_index = 0;
	let mut wave11_index = 0;
	let mut wave12_index = 0;
	let mut wave13_index = 0;
	let mut wave_indexes = [0; 20];
	let keys = [
		KeyboardKey::KEY_GRAVE,
		KeyboardKey::KEY_ONE,
		KeyboardKey::KEY_TWO,
		KeyboardKey::KEY_THREE,
		KeyboardKey::KEY_FOUR,
		KeyboardKey::KEY_FIVE,
		KeyboardKey::KEY_SIX,
		KeyboardKey::KEY_SEVEN,
		KeyboardKey::KEY_EIGHT,
		KeyboardKey::KEY_NINE,
		KeyboardKey::KEY_ZERO,
		KeyboardKey::KEY_MINUS,
		KeyboardKey::KEY_EQUAL,
		KeyboardKey::KEY_Q,
		KeyboardKey::KEY_W,
		KeyboardKey::KEY_E,
		KeyboardKey::KEY_R,
		KeyboardKey::KEY_T,
		KeyboardKey::KEY_Y,
		KeyboardKey::KEY_U,
	];
	while !rl.window_should_close() {
		let mut waves = vec![];
		if rl.is_key_down(KeyboardKey::KEY_ONE) {
			let wave = make_sine_wave(440.0*semitone.powi(0), wave1_index);
			waves.push(wave);
			wave1_index += wave.len();
		} else {
			wave1_index = 0;
		}
		if rl.is_key_down(KeyboardKey::KEY_TWO) {
			let wave = make_sine_wave(440.0*semitone.powi(1), wave2_index);
			waves.push(wave);
			wave2_index += wave.len();
		} else {
			wave2_index = 0;
		}
		if rl.is_key_down(KeyboardKey::KEY_THREE) {
			let wave = make_sine_wave(440.0*semitone.powi(2), wave3_index);
			waves.push(wave);
			wave3_index += wave.len();
		} else {
			wave3_index = 0;
		}
		if rl.is_key_down(KeyboardKey::KEY_FOUR) {
			let wave = make_sine_wave(440.0*semitone.powi(3), wave4_index);
			waves.push(wave);
			wave4_index += wave.len();
		} else {
			wave4_index = 0;
		}
		if rl.is_key_down(KeyboardKey::KEY_FIVE) {
			let wave = make_sine_wave(440.0*semitone.powi(4), wave5_index);
			waves.push(wave);
			wave5_index += wave.len();
		} else {
			wave5_index = 0;
		}
		if rl.is_key_down(KeyboardKey::KEY_SIX) {
			let wave = make_sine_wave(440.0*semitone.powi(5), wave6_index);
			waves.push(wave);
			wave6_index += wave.len();
		} else {
			wave6_index = 0;
		}
		if rl.is_key_down(KeyboardKey::KEY_SEVEN) {
			let wave = make_sine_wave(440.0*semitone.powi(6), wave7_index);
			waves.push(wave);
			wave7_index += wave.len();
		} else {
			wave7_index = 0;
		}
		if rl.is_key_down(KeyboardKey::KEY_EIGHT) {
			let wave = make_sine_wave(440.0*semitone.powi(7), wave8_index);
			waves.push(wave);
			wave8_index += wave.len();
		} else {
			wave8_index = 0;
		}
		if rl.is_key_down(KeyboardKey::KEY_NINE) {
			let wave = make_sine_wave(440.0*semitone.powi(8), wave9_index);
			waves.push(wave);
			wave9_index += wave.len();
		} else {
			wave9_index = 0;
		}
		if rl.is_key_down(KeyboardKey::KEY_ZERO) {
			let wave = make_sine_wave(440.0*semitone.powi(9), wave10_index);
			waves.push(wave);
			wave10_index += wave.len();
		} else {
			wave10_index = 0;
		}
		if rl.is_key_down(KeyboardKey::KEY_MINUS) {
			let wave = make_sine_wave(440.0*semitone.powi(10), wave11_index);
			waves.push(wave);
			wave11_index += wave.len();
		} else {
			wave11_index = 0;
		}
		if rl.is_key_down(KeyboardKey::KEY_EQUAL) {
			let wave = make_sine_wave(440.0*semitone.powi(11), wave12_index);
			waves.push(wave);
			wave12_index += wave.len();
		} else {
			wave12_index = 0;
		}
		if rl.is_key_down(KeyboardKey::KEY_BACKSPACE) {
			let wave = make_sine_wave(440.0*semitone.powi(12), wave13_index);
			waves.push(wave);
			wave13_index += wave.len();
		} else {
			wave13_index = 0;
		for (semitones, key) in keys.iter().enumerate() {
			if rl.is_key_down(*key) {
				let freq = base_freq*semitone.powi(semitones as i32);
				let wave = make_sine_wave(freq, wave_indexes[semitones]);
				waves.push(wave);
				wave_indexes[semitones] += wave.len();
			} else {
				wave_indexes[semitones] = 0;
			}
		}
		let mut final_wave = [0i16; SAMPLE_WIDTH];
		for (i, a) in final_wave.iter_mut().enumerate() {
			*a = waves.iter().map(|w| w[i]).sum()
		}
		assert_eq!(io.writei(&final_wave).unwrap(), SAMPLE_WIDTH);		
		assert_eq!(io.writei(&final_wave).unwrap(), SAMPLE_WIDTH);
		let mut d = rl.begin_drawing(&thread);
		d.clear_background(Color::WHITE);
	}