-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathplay-script.rs
More file actions
159 lines (137 loc) · 5.06 KB
/
play-script.rs
File metadata and controls
159 lines (137 loc) · 5.06 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
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
use std::{
cell::RefCell,
fs,
path::{Path, PathBuf},
rc::Rc,
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
};
use notify::{RecursiveMode, Watcher};
use simplelog::*;
use pattrns::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 sample_pool = Arc::new(SamplePool::new());
struct PatternEntry {
instrument_id: InstrumentId,
script_path: PathBuf,
}
let mut entries = vec![];
for dir_entry in fs::read_dir(DEMO_PATH)?.flatten() {
let path = dir_entry.path();
if let Some(extension) = path.extension().map(|e| e.to_string_lossy()) {
// collect all audio file's that have a lua file next to it
if matches!(extension.as_bytes(), b"mp3" | b"wav" | b"flac") {
let script_path = path.clone().with_extension("lua");
if script_path.exists() {
let instrument_id = sample_pool.load_sample(path)?;
entries.push(PatternEntry {
instrument_id,
script_path,
});
}
}
}
}
// create sample player
let mut player = SamplePlayer::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.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
let mut previous_sequence = None;
while !stop_running.load(Ordering::Relaxed) {
if script_files_changed.load(Ordering::Relaxed) {
script_files_changed.store(false, Ordering::Relaxed);
log::info!("Rebuilding all patterns...");
}
// build final phrase
let load = |instrument: Option<InstrumentId>, file_name: &Path| {
new_pattern_from_file(beat_time, instrument, file_name).unwrap_or_else(|err| {
log::warn!(
"Script '{}' failed to compile:\n{}",
file_name.display(),
err
);
Rc::new(RefCell::new(BeatTimePattern::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]);
// run until we got a stop signal or on script file changes
let reset_playback_pos = false;
player.run_until(
previous_sequence.as_mut(),
&mut sequence,
&beat_time,
reset_playback_pos,
|| script_files_changed.load(Ordering::Relaxed) || stop_running.load(Ordering::Relaxed),
);
// memorize previous sequence for swapping
previous_sequence.replace(sequence);
}
#[cfg(feature = "dhat-profiler")]
drop(profiler);
Ok(())
}