-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpattern_playback.rs
More file actions
96 lines (88 loc) · 3.15 KB
/
Copy pathpattern_playback.rs
File metadata and controls
96 lines (88 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
use std::{
num::{NonZero, NonZeroU16},
time::Duration,
};
use cpal::traits::{DeviceTrait, HostTrait};
use torque_tracker_engine::{
AudioManager, OutputConfig, PlaybackSettings, ToWorkerMsg,
audio_processing::Interpolation,
file::impulse_format::{header::PatternOrder, sample::VibratoWave},
project::{
event_command::NoteCommand,
note_event::{Note, NoteEvent, VolumeEffect},
pattern::{InPatternPosition, PatternOperation},
song::{Song, SongOperation},
},
sample::{Sample, SampleMetaData},
};
fn main() {
let mut manager = AudioManager::new(Song::default());
let mut reader = hound::WavReader::open("test-files/770_Hz_Tone.wav").unwrap();
let spec = reader.spec();
println!("sample specs: {spec:?}");
assert!(spec.channels == 1);
let sample_data = reader
.samples::<i16>()
.map(|result| <f32 as dasp::Sample>::from_sample(result.unwrap()));
let sample = Sample::new_mono(sample_data);
let meta = SampleMetaData {
sample_rate: NonZero::new(spec.sample_rate).unwrap(),
base_note: Note::new(64).unwrap(),
default_volume: 20,
global_volume: 20,
default_pan: None,
vibrato_speed: 0,
vibrato_depth: 0,
vibrato_rate: 0,
vibrato_waveform: VibratoWave::default(),
};
let mut song = manager.try_edit_song().unwrap();
song.apply_operation(SongOperation::SetSample(0, meta, sample))
.unwrap();
for i in 0..12 {
let command = PatternOperation::SetEvent {
position: InPatternPosition {
row: i * 2,
channel: i as u8,
},
event: NoteEvent {
note: Note::new(60 + (i as u8) * 2).unwrap(),
sample_instr: 0,
vol: VolumeEffect::None,
command: NoteCommand::None,
},
};
song.apply_operation(SongOperation::PatternOperation(0, command))
.unwrap();
}
song.apply_operation(SongOperation::SetOrder(0, PatternOrder::Number(0)))
.unwrap();
song.finish();
let host = cpal::default_host();
let default_device = host.default_output_device().unwrap();
let default_config = default_device.default_output_config().unwrap();
println!("default config {:?}", default_config);
println!("device: {:?}", default_device.name());
let config = OutputConfig {
buffer_size: 1024,
channel_count: NonZeroU16::new(2).unwrap(),
sample_rate: NonZero::new(default_config.sample_rate().0).unwrap(),
interpolation: Interpolation::Linear,
};
let (mut callback, _, mut status, mut send) = manager.get_callback::<f32, ()>(config, ());
let _stream = default_device
.build_output_stream(
&default_config.config(),
move |data, _| callback(data, ()),
|e| eprintln!("{e:?}"),
None,
)
.unwrap();
send.try_msg_worker(ToWorkerMsg::Playback(PlaybackSettings::Pattern {
idx: 0,
should_loop: true,
}))
.unwrap();
std::thread::sleep(Duration::from_secs(5));
println!("{:?}", *(status.try_get().unwrap()));
}