MIDI bounce.

This commit is contained in:
David Robillard 2014-12-17 21:48:09 -05:00
parent 5f66300349
commit 0f759e9a93
5 changed files with 77 additions and 29 deletions

View file

@ -21,7 +21,10 @@
#define __ardour_midi_buffer_h__ #define __ardour_midi_buffer_h__
#include "evoral/midi_util.h" #include "evoral/midi_util.h"
#include "evoral/EventSink.hpp"
#include "midi++/event.h" #include "midi++/event.h"
#include "ardour/buffer.h" #include "ardour/buffer.h"
#include "ardour/parameter_types.h" #include "ardour/parameter_types.h"
@ -29,7 +32,7 @@ namespace ARDOUR {
/** Buffer containing 8-bit unsigned char (MIDI) data. */ /** Buffer containing 8-bit unsigned char (MIDI) data. */
class LIBARDOUR_API MidiBuffer : public Buffer class LIBARDOUR_API MidiBuffer : public Buffer, public Evoral::EventSink<framepos_t>
{ {
public: public:
typedef framepos_t TimeType; typedef framepos_t TimeType;
@ -55,6 +58,9 @@ public:
bool insert_event(const Evoral::MIDIEvent<TimeType>& event); bool insert_event(const Evoral::MIDIEvent<TimeType>& event);
bool merge_in_place(const MidiBuffer &other); bool merge_in_place(const MidiBuffer &other);
/** EventSink interface for non-RT use (export, bounce). */
uint32_t write(TimeType time, Evoral::EventType type, uint32_t size, const uint8_t* buf);
template<typename BufferType, typename EventType> template<typename BufferType, typename EventType>
class iterator_base class iterator_base
{ {

View file

@ -632,7 +632,7 @@ class LIBARDOUR_API Session : public PBD::StatefulDestructible, public PBD::Scop
/* flattening stuff */ /* flattening stuff */
boost::shared_ptr<Region> write_one_track (AudioTrack&, framepos_t start, framepos_t end, boost::shared_ptr<Region> write_one_track (Track&, framepos_t start, framepos_t end,
bool overwrite, std::vector<boost::shared_ptr<Source> >&, InterThreadInfo& wot, bool overwrite, std::vector<boost::shared_ptr<Source> >&, InterThreadInfo& wot,
boost::shared_ptr<Processor> endpoint, boost::shared_ptr<Processor> endpoint,
bool include_endpoint, bool for_export, bool for_freeze); bool include_endpoint, bool for_export, bool for_freeze);

View file

@ -250,6 +250,13 @@ MidiBuffer::insert_event(const Evoral::MIDIEvent<TimeType>& ev)
return true; return true;
} }
uint32_t
MidiBuffer::write(TimeType time, Evoral::EventType type, uint32_t size, const uint8_t* buf)
{
insert_event(Evoral::MIDIEvent<TimeType>(type, time, size, const_cast<uint8_t*>(buf)));
return size;
}
/** Reserve space for a new event in the buffer. /** Reserve space for a new event in the buffer.
* *
* This call is for copying MIDI directly into the buffer, the data location * This call is for copying MIDI directly into the buffer, the data location

View file

@ -560,26 +560,52 @@ MidiTrack::write_out_of_band_data (BufferSet& bufs, framepos_t /*start*/, framep
} }
int int
MidiTrack::export_stuff (BufferSet& /*bufs*/, framepos_t /*start_frame*/, framecnt_t /*nframes*/, MidiTrack::export_stuff (BufferSet& buffers,
boost::shared_ptr<Processor> /*endpoint*/, bool /*include_endpoint*/, bool /*for_export*/, bool /*for_freeze*/) framepos_t start,
framecnt_t nframes,
boost::shared_ptr<Processor> endpoint,
bool include_endpoint,
bool for_export,
bool for_freeze)
{ {
return -1; if (buffers.count().n_midi() == 0) {
return -1;
}
boost::shared_ptr<MidiDiskstream> diskstream = midi_diskstream();
Glib::Threads::RWLock::ReaderLock rlock (_processor_lock);
boost::shared_ptr<MidiPlaylist> mpl = boost::dynamic_pointer_cast<MidiPlaylist>(diskstream->playlist());
if (!mpl) {
return -2;
}
buffers.get_midi(0).clear();
if (mpl->read(buffers.get_midi(0), start, nframes, 0) != nframes) {
return -1;
}
//bounce_process (buffers, start, nframes, endpoint, include_endpoint, for_export, for_freeze);
return 0;
} }
boost::shared_ptr<Region> boost::shared_ptr<Region>
MidiTrack::bounce (InterThreadInfo& /*itt*/) MidiTrack::bounce (InterThreadInfo& itt)
{ {
std::cerr << "MIDI bounce currently unsupported" << std::endl; return bounce_range (_session.current_start_frame(), _session.current_end_frame(), itt, main_outs(), false);
return boost::shared_ptr<Region> ();
} }
boost::shared_ptr<Region> boost::shared_ptr<Region>
MidiTrack::bounce_range (framepos_t /*start*/, framepos_t /*end*/, InterThreadInfo& /*itt*/, MidiTrack::bounce_range (framepos_t start,
boost::shared_ptr<Processor> /*endpoint*/, bool /*include_endpoint*/) framepos_t end,
InterThreadInfo& itt,
boost::shared_ptr<Processor> endpoint,
bool include_endpoint)
{ {
std::cerr << "MIDI bounce range currently unsupported" << std::endl; vector<boost::shared_ptr<Source> > srcs;
return boost::shared_ptr<Region> (); return _session.write_one_track (*this, start, end, false, srcs, itt, endpoint, include_endpoint, false, false);
} }
void void

View file

@ -4386,7 +4386,7 @@ Session::freeze_all (InterThreadInfo& itt)
} }
boost::shared_ptr<Region> boost::shared_ptr<Region>
Session::write_one_track (AudioTrack& track, framepos_t start, framepos_t end, Session::write_one_track (Track& track, framepos_t start, framepos_t end,
bool /*overwrite*/, vector<boost::shared_ptr<Source> >& srcs, bool /*overwrite*/, vector<boost::shared_ptr<Source> >& srcs,
InterThreadInfo& itt, InterThreadInfo& itt,
boost::shared_ptr<Processor> endpoint, bool include_endpoint, boost::shared_ptr<Processor> endpoint, bool include_endpoint,
@ -4394,7 +4394,7 @@ Session::write_one_track (AudioTrack& track, framepos_t start, framepos_t end,
{ {
boost::shared_ptr<Region> result; boost::shared_ptr<Region> result;
boost::shared_ptr<Playlist> playlist; boost::shared_ptr<Playlist> playlist;
boost::shared_ptr<AudioFileSource> fsource; boost::shared_ptr<Source> source;
ChanCount diskstream_channels (track.n_channels()); ChanCount diskstream_channels (track.n_channels());
framepos_t position; framepos_t position;
framecnt_t this_chunk; framecnt_t this_chunk;
@ -4416,8 +4416,8 @@ Session::write_one_track (AudioTrack& track, framepos_t start, framepos_t end,
diskstream_channels = track.bounce_get_output_streams (diskstream_channels, endpoint, diskstream_channels = track.bounce_get_output_streams (diskstream_channels, endpoint,
include_endpoint, for_export, for_freeze); include_endpoint, for_export, for_freeze);
if (diskstream_channels.n_audio() < 1) { if (diskstream_channels.n(track.data_type()) < 1) {
error << _("Cannot write a range with no audio.") << endmsg; error << _("Cannot write a range with no data.") << endmsg;
return result; return result;
} }
@ -4443,26 +4443,27 @@ Session::write_one_track (AudioTrack& track, framepos_t start, framepos_t end,
legal_playlist_name = legalize_for_path (playlist->name()); legal_playlist_name = legalize_for_path (playlist->name());
for (uint32_t chan_n = 0; chan_n < diskstream_channels.n_audio(); ++chan_n) { for (uint32_t chan_n = 0; chan_n < diskstream_channels.n(track.data_type()); ++chan_n) {
string base_name = string_compose ("%1-%2-bounce", playlist->name(), chan_n); string base_name = string_compose ("%1-%2-bounce", playlist->name(), chan_n);
string path = new_audio_source_path (legal_playlist_name, diskstream_channels.n_audio(), chan_n, false, true); string path = ((track.data_type() == DataType::AUDIO)
? new_audio_source_path (legal_playlist_name, diskstream_channels.n_audio(), chan_n, false, true)
: new_midi_source_path (legal_playlist_name));
if (path.empty()) { if (path.empty()) {
goto out; goto out;
} }
try { try {
fsource = boost::dynamic_pointer_cast<AudioFileSource> ( source = SourceFactory::createWritable (track.data_type(), *this, path, false, frame_rate());
SourceFactory::createWritable (DataType::AUDIO, *this, path, false, frame_rate()));
} }
catch (failed_constructor& err) { catch (failed_constructor& err) {
error << string_compose (_("cannot create new audio file \"%1\" for %2"), path, track.name()) << endmsg; error << string_compose (_("cannot create new file \"%1\" for %2"), path, track.name()) << endmsg;
goto out; goto out;
} }
srcs.push_back (fsource); srcs.push_back (source);
} }
/* tell redirects that care that we are about to use a much larger /* tell redirects that care that we are about to use a much larger
@ -4512,11 +4513,20 @@ Session::write_one_track (AudioTrack& track, framepos_t start, framepos_t end,
uint32_t n = 0; uint32_t n = 0;
for (vector<boost::shared_ptr<Source> >::iterator src=srcs.begin(); src != srcs.end(); ++src, ++n) { for (vector<boost::shared_ptr<Source> >::iterator src=srcs.begin(); src != srcs.end(); ++src, ++n) {
boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(*src); boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(*src);
boost::shared_ptr<MidiSource> ms;
if (afs) { if (afs) {
if (afs->write (buffers.get_audio(n).data(latency_skip), current_chunk) != current_chunk) { if (afs->write (buffers.get_audio(n).data(latency_skip), current_chunk) != current_chunk) {
goto out; goto out;
} }
} else if ((ms = boost::dynamic_pointer_cast<MidiSource>(*src))) {
Source::Lock lock(ms->mutex());
ms->mark_streaming_write_started(lock);
const MidiBuffer& buf = buffers.get_midi(0);
for (MidiBuffer::const_iterator i = buf.begin(); i != buf.end(); ++i) {
ms->append_event_frames(lock, *i, ms->timeline_position());
}
} }
} }
latency_skip = 0; latency_skip = 0;
@ -4553,10 +4563,14 @@ Session::write_one_track (AudioTrack& track, framepos_t start, framepos_t end,
for (vector<boost::shared_ptr<Source> >::iterator src=srcs.begin(); src != srcs.end(); ++src) { for (vector<boost::shared_ptr<Source> >::iterator src=srcs.begin(); src != srcs.end(); ++src) {
boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(*src); boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(*src);
boost::shared_ptr<MidiSource> ms;
if (afs) { if (afs) {
afs->update_header (position, *xnow, now); afs->update_header (position, *xnow, now);
afs->flush_header (); afs->flush_header ();
} else if ((ms = boost::dynamic_pointer_cast<MidiSource>(*src))) {
Source::Lock lock(ms->mutex());
ms->mark_streaming_write_completed(lock);
} }
} }
@ -4575,12 +4589,7 @@ Session::write_one_track (AudioTrack& track, framepos_t start, framepos_t end,
out: out:
if (!result) { if (!result) {
for (vector<boost::shared_ptr<Source> >::iterator src = srcs.begin(); src != srcs.end(); ++src) { for (vector<boost::shared_ptr<Source> >::iterator src = srcs.begin(); src != srcs.end(); ++src) {
boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(*src); (*src)->mark_for_remove ();
if (afs) {
afs->mark_for_remove ();
}
(*src)->drop_references (); (*src)->drop_references ();
} }