mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 04:06:45 +01:00
wip: refactor pt.4, reduce number of files
This commit is contained in:
parent
adf5b3f0f8
commit
8c37c95cc6
60 changed files with 2185 additions and 2187 deletions
|
|
@ -1,8 +0,0 @@
|
|||
[package]
|
||||
name = "tek_snd"
|
||||
edition = "2021"
|
||||
version = "0.1.0"
|
||||
|
||||
[dependencies]
|
||||
tek_core = { path = "../tek_core" }
|
||||
tek_api = { path = "../tek_api" }
|
||||
|
|
@ -1 +0,0 @@
|
|||
jack-based audio engine
|
||||
|
|
@ -1,10 +0,0 @@
|
|||
pub(crate) use tek_core::*;
|
||||
submod! {
|
||||
snd_arranger
|
||||
snd_mixer
|
||||
snd_plugin
|
||||
snd_sampler
|
||||
snd_sequencer
|
||||
snd_transport
|
||||
}
|
||||
|
||||
|
|
@ -1,31 +0,0 @@
|
|||
use crate::*;
|
||||
|
||||
impl Audio for Arranger {
|
||||
fn process (&mut self, client: &Client, scope: &ProcessScope) -> Control {
|
||||
if let Some(ref transport) = self.transport {
|
||||
transport.write().unwrap().process(client, scope);
|
||||
}
|
||||
let Arrangement { scenes, ref mut tracks, selected, .. } = &mut self.arrangement;
|
||||
for track in tracks.iter_mut() {
|
||||
track.player.process(client, scope);
|
||||
}
|
||||
if let ArrangementFocus::Clip(t, s) = selected {
|
||||
if let Some(Some(Some(phrase))) = scenes.get(*s).map(|scene|scene.clips.get(*t)) {
|
||||
if let Some(track) = tracks.get(*t) {
|
||||
if let Some((ref started_at, Some(ref playing))) = track.player.phrase {
|
||||
let phrase = phrase.read().unwrap();
|
||||
if *playing.read().unwrap() == *phrase {
|
||||
let pulse = self.clock.current.pulse.get();
|
||||
let start = started_at.pulse.get();
|
||||
let now = (pulse - start) % phrase.length as f64;
|
||||
self.editor.now.set(now);
|
||||
return Control::Continue
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
self.editor.now.set(0.);
|
||||
Control::Continue
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
use crate::*;
|
||||
|
||||
impl Audio for Mixer {
|
||||
fn process (&mut self, _: &Client, _: &ProcessScope) -> Control {
|
||||
Control::Continue
|
||||
}
|
||||
}
|
||||
|
|
@ -1,59 +0,0 @@
|
|||
use crate::*;
|
||||
|
||||
impl Audio for Plugin {
|
||||
fn process (&mut self, _: &Client, scope: &ProcessScope) -> Control {
|
||||
match self.plugin.as_mut() {
|
||||
Some(PluginKind::LV2(LV2Plugin {
|
||||
features,
|
||||
ref mut instance,
|
||||
ref mut input_buffer,
|
||||
..
|
||||
})) => {
|
||||
let urid = features.midi_urid();
|
||||
input_buffer.clear();
|
||||
for port in self.ports.midi_ins.values() {
|
||||
let mut atom = ::livi::event::LV2AtomSequence::new(
|
||||
&features,
|
||||
scope.n_frames() as usize
|
||||
);
|
||||
for event in port.iter(scope) {
|
||||
match event.bytes.len() {
|
||||
3 => atom.push_midi_event::<3>(
|
||||
event.time as i64,
|
||||
urid,
|
||||
&event.bytes[0..3]
|
||||
).unwrap(),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
input_buffer.push(atom);
|
||||
}
|
||||
let mut outputs = vec![];
|
||||
for _ in self.ports.midi_outs.iter() {
|
||||
outputs.push(::livi::event::LV2AtomSequence::new(
|
||||
&features,
|
||||
scope.n_frames() as usize
|
||||
));
|
||||
}
|
||||
let ports = ::livi::EmptyPortConnections::new()
|
||||
.with_atom_sequence_inputs(
|
||||
input_buffer.iter()
|
||||
)
|
||||
.with_atom_sequence_outputs(
|
||||
outputs.iter_mut()
|
||||
)
|
||||
.with_audio_inputs(
|
||||
self.ports.audio_ins.values().map(|o|o.as_slice(scope))
|
||||
)
|
||||
.with_audio_outputs(
|
||||
self.ports.audio_outs.values_mut().map(|o|o.as_mut_slice(scope))
|
||||
);
|
||||
unsafe {
|
||||
instance.run(scope.n_frames() as usize, ports).unwrap()
|
||||
};
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
Control::Continue
|
||||
}
|
||||
}
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
use crate::*;
|
||||
|
||||
impl Audio for Sampler {
|
||||
fn process (&mut self, _: &Client, scope: &ProcessScope) -> Control {
|
||||
self.process_midi_in(scope);
|
||||
self.clear_output_buffer();
|
||||
self.process_audio_out(scope);
|
||||
self.write_output_buffer(scope);
|
||||
Control::Continue
|
||||
}
|
||||
}
|
||||
|
|
@ -1,231 +0,0 @@
|
|||
use crate::*;
|
||||
|
||||
/// JACK process callback for sequencer app
|
||||
impl Audio for Sequencer {
|
||||
fn process (&mut self, client: &Client, scope: &ProcessScope) -> Control {
|
||||
if let Some(ref transport) = self.transport {
|
||||
transport.write().unwrap().process(client, scope);
|
||||
}
|
||||
self.player.process(client, scope);
|
||||
Control::Continue
|
||||
}
|
||||
}
|
||||
|
||||
/// JACK process callback for a sequencer's phrase player/recorder.
|
||||
impl Audio for PhrasePlayer {
|
||||
fn process (&mut self, _: &Client, scope: &ProcessScope) -> Control {
|
||||
let has_midi_outputs = self.has_midi_outputs();
|
||||
let has_midi_inputs = self.has_midi_inputs();
|
||||
// Clear output buffer(s)
|
||||
self.clear(scope, false);
|
||||
// Write chunk of phrase to output, handle switchover
|
||||
if self.play(scope) {
|
||||
self.switchover(scope);
|
||||
}
|
||||
if has_midi_inputs {
|
||||
if self.recording || self.monitoring {
|
||||
// Record and/or monitor input
|
||||
self.record(scope)
|
||||
} else if has_midi_outputs && self.monitoring {
|
||||
// Monitor input to output
|
||||
self.monitor(scope)
|
||||
}
|
||||
}
|
||||
// Write to output port(s)
|
||||
self.write(scope);
|
||||
Control::Continue
|
||||
}
|
||||
}
|
||||
|
||||
/// Methods used primarily by the process callback
|
||||
impl PhrasePlayer {
|
||||
fn is_rolling (&self) -> bool {
|
||||
*self.clock.playing.read().unwrap() == Some(TransportState::Rolling)
|
||||
}
|
||||
fn has_midi_inputs (&self) -> bool {
|
||||
self.midi_inputs.len() > 0
|
||||
}
|
||||
fn has_midi_outputs (&self) -> bool {
|
||||
self.midi_outputs.len() > 0
|
||||
}
|
||||
/// Clear the section of the output buffer that we will be using,
|
||||
/// emitting "all notes off" at start of buffer if requested.
|
||||
fn clear (&mut self, scope: &ProcessScope, force_reset: bool) {
|
||||
for frame in &mut self.midi_chunk[0..scope.n_frames() as usize] {
|
||||
frame.clear();
|
||||
}
|
||||
if self.reset || force_reset {
|
||||
all_notes_off(&mut self.midi_chunk); self.reset = false;
|
||||
}
|
||||
}
|
||||
fn play (&mut self, scope: &ProcessScope) -> bool {
|
||||
let mut next = false;
|
||||
// Write MIDI events from currently playing phrase (if any) to MIDI output buffer
|
||||
if self.is_rolling() {
|
||||
let sample0 = scope.last_frame_time() as usize;
|
||||
let samples = scope.n_frames() as usize;
|
||||
// If no phrase is playing, prepare for switchover immediately
|
||||
next = self.phrase.is_none();
|
||||
if let Some((started, phrase)) = &self.phrase {
|
||||
// First sample to populate. Greater than 0 means that the first
|
||||
// pulse of the phrase falls somewhere in the middle of the chunk.
|
||||
let sample = started.sample.get() as usize;
|
||||
let sample = sample + self.clock.started.read().unwrap().unwrap().0;
|
||||
let sample = sample0.saturating_sub(sample);
|
||||
// Iterator that emits sample (index into output buffer at which to write MIDI event)
|
||||
// paired with pulse (index into phrase from which to take the MIDI event) for each
|
||||
// sample of the output buffer that corresponds to a MIDI pulse.
|
||||
let pulses = self.clock.timebase().pulses_between_samples(sample, sample + samples);
|
||||
// Notes active during current chunk.
|
||||
let notes = &mut self.notes_out.write().unwrap();
|
||||
for (sample, pulse) in pulses {
|
||||
// If a next phrase is enqueued, and we're past the end of the current one,
|
||||
// break the loop here (FIXME count pulse correctly)
|
||||
next = self.next_phrase.is_some() && if let Some(ref phrase) = phrase {
|
||||
pulse >= phrase.read().unwrap().length
|
||||
} else {
|
||||
true
|
||||
};
|
||||
if next {
|
||||
break
|
||||
}
|
||||
// If there's a currently playing phrase, output notes from it to buffer:
|
||||
if let Some(ref phrase) = phrase {
|
||||
// Source phrase from which the MIDI events will be taken.
|
||||
let phrase = phrase.read().unwrap();
|
||||
// Current pulse index in source phrase
|
||||
let pulse = pulse % phrase.length;
|
||||
// Output each MIDI event from phrase at appropriate frames of output buffer:
|
||||
for message in phrase.notes[pulse].iter() {
|
||||
// Clear output buffer for this MIDI event.
|
||||
self.midi_note.clear();
|
||||
// TODO: support MIDI channels other than CH1.
|
||||
let channel = 0.into();
|
||||
// Serialize MIDI event into message buffer.
|
||||
LiveEvent::Midi { channel, message: *message }
|
||||
.write(&mut self.midi_note)
|
||||
.unwrap();
|
||||
// Append serialized message to output buffer.
|
||||
self.midi_chunk[sample].push(self.midi_note.clone());
|
||||
// Update the list of currently held notes.
|
||||
update_keys(notes, &message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
next
|
||||
}
|
||||
fn switchover (&mut self, scope: &ProcessScope) {
|
||||
if self.is_rolling() {
|
||||
let sample0 = scope.last_frame_time() as usize;
|
||||
//let samples = scope.n_frames() as usize;
|
||||
if let Some((start_at, phrase)) = &self.next_phrase {
|
||||
let start = start_at.sample.get() as usize;
|
||||
let sample = self.clock.started.read().unwrap().unwrap().0;
|
||||
// If it's time to switch to the next phrase:
|
||||
if start <= sample0.saturating_sub(sample) {
|
||||
// Samples elapsed since phrase was supposed to start
|
||||
let skipped = sample0 - start;
|
||||
// Switch over to enqueued phrase
|
||||
let started = Instant::from_sample(&self.clock.timebase(), start as f64);
|
||||
self.phrase = Some((started, phrase.clone()));
|
||||
// Unset enqueuement (TODO: where to implement looping?)
|
||||
self.next_phrase = None
|
||||
}
|
||||
// TODO fill in remaining ticks of chunk from next phrase.
|
||||
// ?? just call self.play(scope) again, since enqueuement is off ???
|
||||
self.play(scope);
|
||||
// ?? or must it be with modified scope ??
|
||||
// likely not because start time etc
|
||||
}
|
||||
}
|
||||
}
|
||||
fn record (&mut self, scope: &ProcessScope) {
|
||||
let sample0 = scope.last_frame_time() as usize;
|
||||
if let (true, Some((started, phrase))) = (self.is_rolling(), &self.phrase) {
|
||||
let start = started.sample.get() as usize;
|
||||
let quant = self.clock.quant.get();
|
||||
// For highlighting keys and note repeat
|
||||
let mut notes_in = self.notes_in.write().unwrap();
|
||||
// Record from each input
|
||||
for input in self.midi_inputs.iter() {
|
||||
for (sample, event, bytes) in parse_midi_input(input.iter(scope)) {
|
||||
if let LiveEvent::Midi { message, .. } = event {
|
||||
if self.monitoring {
|
||||
self.midi_chunk[sample].push(bytes.to_vec())
|
||||
}
|
||||
if self.recording {
|
||||
if let Some(phrase) = phrase {
|
||||
let mut phrase = phrase.write().unwrap();
|
||||
let length = phrase.length;
|
||||
phrase.record_event({
|
||||
let sample = (sample0 + sample - start) as f64;
|
||||
let pulse = self.clock.timebase().samples_to_pulse(sample);
|
||||
let quantized = (pulse / quant).round() * quant;
|
||||
let looped = quantized as usize % length;
|
||||
looped
|
||||
}, message);
|
||||
}
|
||||
}
|
||||
update_keys(&mut notes_in, &message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if let (true, Some((start_at, phrase))) = (self.is_rolling(), &self.next_phrase) {
|
||||
// TODO switch to next phrase and record into it
|
||||
}
|
||||
}
|
||||
fn monitor (&mut self, scope: &ProcessScope) {
|
||||
let mut notes_in = self.notes_in.write().unwrap();
|
||||
for input in self.midi_inputs.iter() {
|
||||
for (sample, event, bytes) in parse_midi_input(input.iter(scope)) {
|
||||
if let LiveEvent::Midi { message, .. } = event {
|
||||
self.midi_chunk[sample].push(bytes.to_vec());
|
||||
update_keys(&mut notes_in, &message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
fn write (&mut self, scope: &ProcessScope) {
|
||||
let samples = scope.n_frames() as usize;
|
||||
for port in self.midi_outputs.iter_mut() {
|
||||
let writer = &mut port.writer(scope);
|
||||
let output = &self.midi_chunk;
|
||||
for time in 0..samples {
|
||||
for event in output[time].iter() {
|
||||
writer.write(&RawMidi { time: time as u32, bytes: &event })
|
||||
.expect(&format!("{event:?}"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Add "all notes off" to the start of a buffer.
|
||||
pub fn all_notes_off (output: &mut PhraseChunk) {
|
||||
let mut buf = vec![];
|
||||
let msg = MidiMessage::Controller { controller: 123.into(), value: 0.into() };
|
||||
let evt = LiveEvent::Midi { channel: 0.into(), message: msg };
|
||||
evt.write(&mut buf).unwrap();
|
||||
output[0].push(buf);
|
||||
}
|
||||
|
||||
/// Return boxed iterator of MIDI events
|
||||
pub fn parse_midi_input (input: MidiIter) -> Box<dyn Iterator<Item=(usize, LiveEvent, &[u8])> + '_> {
|
||||
Box::new(input.map(|RawMidi { time, bytes }|(
|
||||
time as usize,
|
||||
LiveEvent::parse(bytes).unwrap(),
|
||||
bytes
|
||||
)))
|
||||
}
|
||||
|
||||
/// Update notes_in array
|
||||
pub fn update_keys (keys: &mut[bool;128], message: &MidiMessage) {
|
||||
match message {
|
||||
MidiMessage::NoteOn { key, .. } => { keys[key.as_int() as usize] = true; }
|
||||
MidiMessage::NoteOff { key, .. } => { keys[key.as_int() as usize] = false; },
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,32 +0,0 @@
|
|||
use crate::*;
|
||||
impl Audio for TransportToolbar {
|
||||
fn process (&mut self, _: &Client, scope: &ProcessScope) -> Control {
|
||||
let times = scope.cycle_times().unwrap();
|
||||
let CycleTimes { current_frames, current_usecs, next_usecs: _, period_usecs: _ } = times;
|
||||
let _chunk_size = scope.n_frames() as usize;
|
||||
let transport = self.transport.query().unwrap();
|
||||
self.clock.current.sample.set(transport.pos.frame() as f64);
|
||||
let mut playing = self.clock.playing.write().unwrap();
|
||||
let mut started = self.clock.started.write().unwrap();
|
||||
if *playing != Some(transport.state) {
|
||||
match transport.state {
|
||||
TransportState::Rolling => {
|
||||
*started = Some((current_frames as usize, current_usecs as usize))
|
||||
},
|
||||
TransportState::Stopped => {
|
||||
*started = None
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
};
|
||||
*playing = Some(transport.state);
|
||||
if *playing == Some(TransportState::Stopped) {
|
||||
*started = None;
|
||||
}
|
||||
self.clock.current.update_from_usec(match *started {
|
||||
Some((_, usecs)) => current_usecs as f64 - usecs as f64,
|
||||
None => 0.
|
||||
});
|
||||
Control::Continue
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue