old school music tracker audio backend
1use std::num::NonZero;
2
3use torque_tracker_engine::{
4 PlaybackSettings,
5 audio_processing::{Interpolation, playback::PlaybackState},
6 file::impulse_format::{header::PatternOrder, sample::VibratoWave},
7 project::{
8 event_command::NoteCommand,
9 note_event::{Note, NoteEvent, VolumeEffect},
10 pattern::InPatternPosition,
11 song::Song,
12 },
13 sample::{Sample, SampleMetaData},
14};
15
16fn main() {
17 let mut reader = hound::WavReader::open("test-files/770_Hz_Tone.wav").unwrap();
18 let spec = reader.spec();
19 println!("sample specs: {spec:?}");
20 assert!(spec.channels == 1);
21 let sample_data = reader
22 .samples::<i16>()
23 .map(|result| <f32 as dasp::Sample>::from_sample(result.unwrap()));
24 let sample = Sample::new_mono(sample_data);
25
26 let meta = SampleMetaData {
27 sample_rate: NonZero::new(spec.sample_rate).unwrap(),
28 default_volume: 200,
29 global_volume: 200,
30 default_pan: None,
31 vibrato_speed: 0,
32 vibrato_depth: 0,
33 vibrato_rate: 0,
34 vibrato_waveform: VibratoWave::Sine,
35 base_note: Note::new(20).unwrap(),
36 };
37
38 let mut song: Song = Song::default();
39 song.pattern_order[0] = PatternOrder::Number(0);
40 song.samples[0] = Some((meta, sample));
41 song.patterns[0].set_event(
42 InPatternPosition { row: 0, channel: 0 },
43 NoteEvent {
44 note: Note::default(),
45 sample_instr: 0,
46 vol: VolumeEffect::None,
47 command: NoteCommand::None,
48 },
49 );
50 song.patterns[0].set_event(
51 InPatternPosition { row: 0, channel: 2 },
52 NoteEvent {
53 note: Note::default(),
54 sample_instr: 0,
55 vol: VolumeEffect::None,
56 command: NoteCommand::None,
57 },
58 );
59
60 let mut playback = PlaybackState::new(
61 &song,
62 NonZero::new(44100).unwrap(),
63 PlaybackSettings::Order {
64 idx: 0,
65 should_loop: true,
66 },
67 )
68 .unwrap();
69 let iter = playback.iter::<{ Interpolation::Nearest as u8 }>(&song);
70 for _ in iter.take(50) {
71 // dbg!(frame);
72 }
73}