Add audio playback

This commit is contained in:
2023-07-01 21:54:53 -06:00
parent 36edefebdb
commit 22bea018a3
3 changed files with 899 additions and 17 deletions
Generated
+876 -11
View File
File diff suppressed because it is too large Load Diff
+1
View File
@@ -9,4 +9,5 @@ edition = "2021"
anyhow = "1.0.71"
inpt = "0.1.3"
num-rational = "0.4.1"
rodio = "0.17.1"
sdl2 = { version = "0.35.2", features = ["gfx", "ttf"] }
+22 -6
View File
@@ -1,9 +1,14 @@
use std::collections::HashSet;
use std::fs::File;
use std::io::BufReader;
use std::path::PathBuf;
use std::time::{Duration, Instant};
use anyhow::{anyhow, Error};
use num_rational::Ratio;
use rodio::{source::Source, Decoder, OutputStream};
use sdl2::audio::{AudioSpecDesired, AudioSpecWAV};
use sdl2::event::Event;
use sdl2::gfx::primitives::{DrawRenderer, ToColor};
use sdl2::keyboard::Keycode;
@@ -206,11 +211,21 @@ fn render_score(canvas: &mut WindowCanvas, font: &Font, score: u64) -> Result<()
fn main() -> Result<(), Error> {
println!("WELCOME TO THE GAME!");
let song_path = std::env::args()
.nth(1)
.ok_or(anyhow!("Please supply a song"))?;
let chart_folder = PathBuf::from(
std::env::args()
.nth(1)
.ok_or(anyhow!("Please supply a song"))?,
);
let song = chart::parse_chart(song_path)?;
let song = chart::parse_chart(chart_folder.join("notes.chart"))?;
// Get a output stream handle to the default physical sound device
let (_stream, stream_handle) = OutputStream::try_default().unwrap();
// Load a sound from a file, using a path relative to Cargo.toml
let file = BufReader::new(File::open(chart_folder.join("song.ogg")).unwrap());
// Decode that sound file into a source
let source = Decoder::new(file).unwrap();
// Play the sound directly on the device
dbg!(&song);
@@ -248,12 +263,13 @@ fn main() -> Result<(), Error> {
let bpm = 105;
// window size in ms
let window_before = 70;
let window_after = 70;
let window_before = 140;
let window_after = 140;
let mut sent_set = HashSet::new();
let mut state = State::default();
stream_handle.play_raw(source.convert_samples())?;
'running: loop {
let now = start.elapsed();