-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlive_note.rs
More file actions
82 lines (76 loc) · 2.63 KB
/
live_note.rs
File metadata and controls
82 lines (76 loc) · 2.63 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
use std::{
num::{NonZero, NonZeroU16},
time::Duration,
};
use cpal::traits::{DeviceTrait, HostTrait};
use torque_tracker_engine::{
AudioManager, OutputConfig, ToWorkerMsg,
audio_processing::Interpolation,
file::impulse_format::sample::VibratoWave,
project::{
event_command::NoteCommand,
note_event::{Note, NoteEvent, VolumeEffect},
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(),
default_volume: 150,
global_volume: 20,
default_pan: None,
vibrato_speed: 0,
vibrato_depth: 0,
vibrato_rate: 0,
vibrato_waveform: VibratoWave::default(),
base_note: Note::new(64).unwrap(),
};
manager
.try_edit_song()
.unwrap()
.apply_operation(SongOperation::SetSample(1, meta, sample))
.unwrap();
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: 2048,
channel_count: NonZeroU16::new(2).unwrap(),
sample_rate: NonZero::new(default_config.sample_rate().0).unwrap(),
interpolation: Interpolation::Quadratic,
};
let (mut audio_callback, _, _, mut send) = manager.get_callback::<f32, ()>(config, ());
let stream = default_device
.build_output_stream(
&default_config.config(),
move |data, _| audio_callback(data, ()),
|e| eprintln!("{e:?}"),
None,
)
.unwrap();
let note_event = NoteEvent {
note: Note::new(30).unwrap(),
sample_instr: 1,
vol: VolumeEffect::None,
command: NoteCommand::None,
};
send.try_msg_worker(ToWorkerMsg::PlayEvent(note_event))
.unwrap();
std::thread::sleep(Duration::from_secs(1));
send.try_msg_worker(ToWorkerMsg::PlayEvent(note_event))
.unwrap();
std::thread::sleep(Duration::from_secs(3));
drop(stream);
}