-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplay-script.rs
162 lines (140 loc) · 5.41 KB
/
play-script.rs
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use std::{
cell::RefCell,
collections::HashSet,
fs,
path::{Path, PathBuf},
rc::Rc,
sync::{
atomic::{AtomicBool, Ordering},
Arc, RwLock,
},
};
use notify::{RecursiveMode, Watcher};
use simplelog::*;
use afseq::prelude::*;
// -------------------------------------------------------------------------------------------------
#[cfg(feature = "dhat-profiler")]
#[global_allocator]
static ALLOC: dhat::Alloc = dhat::Alloc;
// -------------------------------------------------------------------------------------------------
// TODO: make this configurable with an cmd line arg
const DEMO_PATH: &str = "./examples/assets";
// -------------------------------------------------------------------------------------------------
#[allow(non_snake_case)]
fn main() -> Result<(), Box<dyn std::error::Error>> {
#[cfg(feature = "dhat-profiler")]
let profiler = dhat::Profiler::builder().trim_backtraces(Some(100)).build();
// init logging
TermLogger::init(
log::STATIC_MAX_LEVEL,
ConfigBuilder::default().build(),
TerminalMode::Mixed,
ColorChoice::Auto,
)
.unwrap_or_else(|err| {
log::error!("init_logger error: {:?}", err);
});
// fetch contents from demo dir
log::info!("Searching for wav/script files in path '{}'...", DEMO_PATH);
let mut entry_stems = HashSet::<String>::new();
let paths = fs::read_dir(DEMO_PATH).expect("Failed to access demo content directory");
for path in paths {
let path = path?.path();
if let Some(extension) = path.extension() {
let extension = extension.to_string_lossy().to_string();
if matches!(extension.as_str(), "lua" | "wav") {
if let Some(stem) = path.file_stem() {
entry_stems.insert(stem.to_string_lossy().to_string());
}
}
}
}
// load samples and get paths to the rhythm scripts
let sample_pool = SamplePool::new();
struct RhythmEntry {
instrument_id: InstrumentId,
script_path: String,
}
let mut entries = vec![];
for stem in entry_stems.iter() {
let base_path = PathBuf::new().join(DEMO_PATH).join(stem);
let wave_file = base_path.with_extension("wav");
let lua_file = base_path.with_extension("lua");
if wave_file.exists() && lua_file.exists() {
log::info!("Found file/script: '{}'...", stem);
let instrument_id = sample_pool.load_sample(&wave_file.to_string_lossy())?;
let script_path = lua_file.to_string_lossy().to_string();
entries.push(RhythmEntry {
instrument_id,
script_path,
});
} else if lua_file.exists() || wave_file.exists() {
log::warn!("Ignoring file/script: '{}'...", stem);
}
}
// create event player
let mut player = SamplePlayer::new(Arc::new(RwLock::new(sample_pool)), None)?;
// set default time base config
let beat_time = BeatTimeBase {
beats_per_min: 124.0,
beats_per_bar: 4,
samples_per_sec: player.file_player().output_sample_rate(),
};
// Watch for script changes, signaling in 'script_files_changed'
let script_files_changed = Arc::new(AtomicBool::new(false));
let mut watcher = notify::recommended_watcher({
let script_files_changed = script_files_changed.clone();
move |res: Result<notify::Event, notify::Error>| match res {
Ok(event) => {
if !event.kind.is_access() {
log::info!("File change event: {:?}", event);
script_files_changed.store(true, Ordering::Relaxed);
}
}
Err(err) => log::error!("File watch error: {}", err),
}
})?;
watcher.watch(Path::new(DEMO_PATH), RecursiveMode::Recursive)?;
// stop on Control-C
let stop_running = Arc::new(AtomicBool::new(false));
ctrlc::set_handler({
let stop_running = stop_running.clone();
move || {
stop_running.store(true, Ordering::Relaxed);
}
})?;
// (re)run all scripts
while !stop_running.load(Ordering::Relaxed) {
if script_files_changed.load(Ordering::Relaxed) {
script_files_changed.store(false, Ordering::Relaxed);
log::info!("Rebuilding all rhythms...");
}
// build final phrase
let load = |instrument: Option<InstrumentId>, file_name: &str| {
new_rhythm_from_file(beat_time, instrument, file_name).unwrap_or_else(|err| {
log::warn!("Script '{}' failed to compile:\n{}", file_name, err);
Rc::new(RefCell::new(BeatTimeRhythm::new(
beat_time,
BeatTimeStep::Beats(1.0),
)))
})
};
let phrase = Phrase::new(
beat_time,
entries
.iter()
.map(|e| load(Some(e.instrument_id), &e.script_path))
.collect(),
BeatTimeStep::Bar(4.0),
);
// wrap phrase into a sequence
let mut sequence = Sequence::new(beat_time, vec![phrase]);
let reset_playback_pos = false;
player.run_until(&mut sequence, &beat_time, reset_playback_pos, || {
script_files_changed.load(Ordering::Relaxed) || stop_running.load(Ordering::Relaxed)
});
}
#[cfg(feature = "dhat-profiler")]
drop(profiler);
Ok(())
}