mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 12:16:42 +01:00
reenable phrase recording/playback, pt.1
This commit is contained in:
parent
b60aca88d3
commit
eba7044916
3 changed files with 92 additions and 70 deletions
|
|
@ -119,6 +119,8 @@ pub struct PhrasePlayer<E: Engine> {
|
||||||
pub clock: Arc<TransportTime>,
|
pub clock: Arc<TransportTime>,
|
||||||
/// Start time and phrase being played
|
/// Start time and phrase being played
|
||||||
pub phrase: Option<(AtomicUsize, Option<Arc<RwLock<Phrase>>>)>,
|
pub phrase: Option<(AtomicUsize, Option<Arc<RwLock<Phrase>>>)>,
|
||||||
|
/// Start time (FIXME move into phrase, using Instant)
|
||||||
|
pub started: Option<(usize, usize)>,
|
||||||
/// Start time and next phrase
|
/// Start time and next phrase
|
||||||
pub next_phrase: Option<(AtomicUsize, Option<Arc<RwLock<Phrase>>>)>,
|
pub next_phrase: Option<(AtomicUsize, Option<Arc<RwLock<Phrase>>>)>,
|
||||||
/// Play input through output.
|
/// Play input through output.
|
||||||
|
|
@ -129,8 +131,10 @@ pub struct PhrasePlayer<E: Engine> {
|
||||||
pub overdub: bool,
|
pub overdub: bool,
|
||||||
/// Send all notes off
|
/// Send all notes off
|
||||||
pub reset: bool, // TODO?: after Some(nframes)
|
pub reset: bool, // TODO?: after Some(nframes)
|
||||||
/// Output from current sequence.
|
/// Record from MIDI ports to current sequence.
|
||||||
pub midi_out: Option<Port<MidiOut>>,
|
pub midi_inputs: Vec<Port<MidiIn>>,
|
||||||
|
/// Play from current sequence to MIDI ports
|
||||||
|
pub midi_outputs: Vec<Port<MidiOut>>,
|
||||||
/// MIDI output buffer
|
/// MIDI output buffer
|
||||||
pub midi_out_buf: Vec<Vec<Vec<u8>>>,
|
pub midi_out_buf: Vec<Vec<Vec<u8>>>,
|
||||||
/// Notes currently held at input
|
/// Notes currently held at input
|
||||||
|
|
@ -333,13 +337,15 @@ impl<E: Engine> PhrasePlayer<E> {
|
||||||
_engine: Default::default(),
|
_engine: Default::default(),
|
||||||
clock: clock.clone(),
|
clock: clock.clone(),
|
||||||
phrase: None,
|
phrase: None,
|
||||||
|
started: None,
|
||||||
next_phrase: None,
|
next_phrase: None,
|
||||||
notes_in: Arc::new(RwLock::new([false;128])),
|
notes_in: Arc::new(RwLock::new([false;128])),
|
||||||
notes_out: Arc::new(RwLock::new([false;128])),
|
notes_out: Arc::new(RwLock::new([false;128])),
|
||||||
monitoring: false,
|
monitoring: false,
|
||||||
recording: false,
|
recording: false,
|
||||||
overdub: true,
|
overdub: true,
|
||||||
midi_out: None,
|
midi_inputs: vec![],
|
||||||
|
midi_outputs: vec![],
|
||||||
midi_out_buf: vec![Vec::with_capacity(16);16384],
|
midi_out_buf: vec![Vec::with_capacity(16);16384],
|
||||||
reset: true,
|
reset: true,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,73 +7,37 @@ impl<E: Engine> Audio for Sequencer<E> {
|
||||||
Control::Continue
|
Control::Continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl Phrase {
|
|
||||||
/// Write a chunk of MIDI events to an output port.
|
|
||||||
pub fn process_out (
|
|
||||||
&self,
|
|
||||||
output: &mut PhraseChunk,
|
|
||||||
notes_on: &mut [bool;128],
|
|
||||||
timebase: &Arc<Timebase>,
|
|
||||||
(frame0, frames, _): (usize, usize, f64),
|
|
||||||
) {
|
|
||||||
let mut buf = Vec::with_capacity(8);
|
|
||||||
for (time, tick) in Ticks(timebase.pulses_per_sample()).between_samples(
|
|
||||||
frame0, frame0 + frames
|
|
||||||
) {
|
|
||||||
let tick = tick % self.length;
|
|
||||||
for message in self.notes[tick].iter() {
|
|
||||||
buf.clear();
|
|
||||||
let channel = 0.into();
|
|
||||||
let message = *message;
|
|
||||||
LiveEvent::Midi { channel, message }.write(&mut buf).unwrap();
|
|
||||||
output[time as usize].push(buf.clone());
|
|
||||||
match message {
|
|
||||||
MidiMessage::NoteOn { key, .. } => notes_on[key.as_int() as usize] = true,
|
|
||||||
MidiMessage::NoteOff { key, .. } => notes_on[key.as_int() as usize] = false,
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl<E: Engine> PhrasePlayer<E> {
|
impl<E: Engine> PhrasePlayer<E> {
|
||||||
pub fn process (
|
pub fn process (
|
||||||
&mut self,
|
&mut self,
|
||||||
input: Option<MidiIter>,
|
input: Option<MidiIter>,
|
||||||
timebase: &Arc<Timebase>,
|
scope: &ProcessScope,
|
||||||
playing: Option<TransportState>,
|
|
||||||
started: Option<(usize, usize)>,
|
|
||||||
quant: usize,
|
|
||||||
reset: bool,
|
|
||||||
scope: &ProcessScope,
|
|
||||||
(frame0, frames): (usize, usize),
|
(frame0, frames): (usize, usize),
|
||||||
(_usec0, _usecs): (usize, usize),
|
(_usec0, _usecs): (usize, usize),
|
||||||
period: f64,
|
period: f64,
|
||||||
) {
|
) {
|
||||||
if self.midi_out.is_some() {
|
let has_midi_inputs = self.has_midi_inputs();
|
||||||
// Clear the section of the output buffer that we will be using
|
let has_midi_outputs = self.has_midi_outputs();
|
||||||
for frame in &mut self.midi_out_buf[0..frames] {
|
let quant = self.clock.quant();
|
||||||
frame.clear();
|
if has_midi_outputs {
|
||||||
}
|
self.clear_midi_out_buf(frames);
|
||||||
// Emit "all notes off" at start of buffer if requested
|
self.reset_midi_out_buf(false /* FIXME where did force-reset come from? */);
|
||||||
if self.reset {
|
|
||||||
all_notes_off(&mut self.midi_out_buf);
|
|
||||||
self.reset = false;
|
|
||||||
} else if reset {
|
|
||||||
all_notes_off(&mut self.midi_out_buf);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if let (
|
if let (
|
||||||
Some(TransportState::Rolling),
|
Some(TransportState::Rolling),
|
||||||
Some((start_frame, _)),
|
Some((start_frame, _)),
|
||||||
Some((_started, Some(ref phrase)))
|
Some((_started, Some(ref phrase)))
|
||||||
) = (playing, started, &self.phrase) {
|
) = (
|
||||||
|
*self.clock.playing.read().unwrap(),
|
||||||
|
self.started,
|
||||||
|
&self.phrase
|
||||||
|
) {
|
||||||
phrase.read().map(|phrase|{
|
phrase.read().map(|phrase|{
|
||||||
if self.midi_out.is_some() {
|
if has_midi_outputs {
|
||||||
phrase.process_out(
|
phrase.process_out(
|
||||||
&mut self.midi_out_buf,
|
&mut self.midi_out_buf,
|
||||||
&mut self.notes_out.write().unwrap(),
|
&mut self.notes_out.write().unwrap(),
|
||||||
timebase,
|
&self.clock.timebase,
|
||||||
(frame0.saturating_sub(start_frame), frames, period)
|
(frame0.saturating_sub(start_frame), frames, period)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
@ -92,7 +56,7 @@ impl<E: Engine> PhrasePlayer<E> {
|
||||||
}
|
}
|
||||||
if self.recording {
|
if self.recording {
|
||||||
phrase.record_event({
|
phrase.record_event({
|
||||||
let pulse = timebase.samples_to_pulse(
|
let pulse = self.clock.timebase.samples_to_pulse(
|
||||||
(frame0 + frame - start_frame) as f64
|
(frame0 + frame - start_frame) as f64
|
||||||
);
|
);
|
||||||
let quantized = (
|
let quantized = (
|
||||||
|
|
@ -116,7 +80,7 @@ impl<E: Engine> PhrasePlayer<E> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if input.is_some() && self.midi_out.is_some() && self.monitoring {
|
} else if input.is_some() && has_midi_outputs && self.monitoring {
|
||||||
let mut notes_in = self.notes_in.write().unwrap();
|
let mut notes_in = self.notes_in.write().unwrap();
|
||||||
for (frame, event, bytes) in parse_midi_input(input.unwrap()) {
|
for (frame, event, bytes) in parse_midi_input(input.unwrap()) {
|
||||||
match event {
|
match event {
|
||||||
|
|
@ -136,8 +100,9 @@ impl<E: Engine> PhrasePlayer<E> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let Some(out) = &mut self.midi_out {
|
|
||||||
let writer = &mut out.writer(scope);
|
for port in self.midi_outputs.iter_mut() {
|
||||||
|
let writer = &mut port.writer(scope);
|
||||||
let output = &self.midi_out_buf;
|
let output = &self.midi_out_buf;
|
||||||
for time in 0..frames {
|
for time in 0..frames {
|
||||||
for event in output[time].iter() {
|
for event in output[time].iter() {
|
||||||
|
|
@ -147,6 +112,56 @@ impl<E: Engine> PhrasePlayer<E> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
pub fn has_midi_inputs (&self) -> bool {
|
||||||
|
self.midi_inputs.len() > 0
|
||||||
|
}
|
||||||
|
pub fn has_midi_outputs (&self) -> bool {
|
||||||
|
self.midi_outputs.len() > 0
|
||||||
|
}
|
||||||
|
/// Clear the section of the output buffer that we will be using
|
||||||
|
pub fn clear_midi_out_buf (&mut self, frames: usize) {
|
||||||
|
for frame in &mut self.midi_out_buf[0..frames] {
|
||||||
|
frame.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// Emit "all notes off" at start of buffer if requested
|
||||||
|
pub fn reset_midi_out_buf (&mut self, force_reset: bool) {
|
||||||
|
if self.reset {
|
||||||
|
all_notes_off(&mut self.midi_out_buf);
|
||||||
|
self.reset = false;
|
||||||
|
} else if force_reset {
|
||||||
|
all_notes_off(&mut self.midi_out_buf);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl Phrase {
|
||||||
|
/// Write a chunk of MIDI events to an output port.
|
||||||
|
pub fn process_out (
|
||||||
|
&self,
|
||||||
|
output: &mut PhraseChunk,
|
||||||
|
notes_on: &mut [bool;128],
|
||||||
|
timebase: &Timebase,
|
||||||
|
(frame0, frames, _): (usize, usize, f64),
|
||||||
|
) {
|
||||||
|
let mut buf = Vec::with_capacity(8);
|
||||||
|
for (time, tick) in Ticks(timebase.pulses_per_sample()).between_samples(
|
||||||
|
frame0, frame0 + frames
|
||||||
|
) {
|
||||||
|
let tick = tick % self.length;
|
||||||
|
for message in self.notes[tick].iter() {
|
||||||
|
buf.clear();
|
||||||
|
let channel = 0.into();
|
||||||
|
let message = *message;
|
||||||
|
LiveEvent::Midi { channel, message }.write(&mut buf).unwrap();
|
||||||
|
output[time as usize].push(buf.clone());
|
||||||
|
match message {
|
||||||
|
MidiMessage::NoteOn { key, .. } => notes_on[key.as_int() as usize] = true,
|
||||||
|
MidiMessage::NoteOff { key, .. } => notes_on[key.as_int() as usize] = false,
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
/// Add "all notes off" to the start of a buffer.
|
/// Add "all notes off" to the start of a buffer.
|
||||||
pub fn all_notes_off (output: &mut PhraseChunk) {
|
pub fn all_notes_off (output: &mut PhraseChunk) {
|
||||||
|
|
|
||||||
|
|
@ -85,17 +85,18 @@ impl<E: Engine> TransportToolbar<E> {
|
||||||
started: None,
|
started: None,
|
||||||
jack: None,
|
jack: None,
|
||||||
transport,
|
transport,
|
||||||
clock: if let Some(clock) = clock {
|
clock: match clock {
|
||||||
clock.clone()
|
Some(clock) => clock.clone(),
|
||||||
} else {
|
None => {
|
||||||
let timebase = Timebase::default();
|
let timebase = Timebase::default();
|
||||||
Arc::new(TransportTime {
|
Arc::new(TransportTime {
|
||||||
playing: Some(TransportState::Stopped).into(),
|
playing: Some(TransportState::Stopped).into(),
|
||||||
quant: 24.into(),
|
quant: 24.into(),
|
||||||
sync: (timebase.ppq() as usize * 4).into(),
|
sync: (timebase.ppq() as usize * 4).into(),
|
||||||
instant: Instant::default(),
|
instant: Instant::default(),
|
||||||
timebase,
|
timebase,
|
||||||
})
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue