summaryrefslogtreecommitdiff
path: root/libs
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2006-08-21 19:12:26 +0000
committerDavid Robillard <d@drobilla.net>2006-08-21 19:12:26 +0000
commit48a4dc072c82dd382caa11405bf61a125d17eb16 (patch)
tree1b94f5a7fbd461d272f1b6c2142a1ddb2b0d746b /libs
parent7250433f50236a05fc652fa41c23bf53fbf6a0fd (diff)
Recording to SMF. Playback not quite working yet, just some buglets left to iron out.
git-svn-id: svn://localhost/ardour2/branches/midi@841 d708f5d6-7413-0410-9779-e7cbd77b26cf
Diffstat (limited to 'libs')
-rw-r--r--libs/ardour/ardour/midi_events.h136
-rw-r--r--libs/ardour/ardour/midi_ring_buffer.h12
-rw-r--r--libs/ardour/ardour/midi_source.h10
-rw-r--r--libs/ardour/ardour/midi_util.h72
-rw-r--r--libs/ardour/ardour/smf_source.h33
-rw-r--r--libs/ardour/midi_diskstream.cc74
-rw-r--r--libs/ardour/midi_region.cc12
-rw-r--r--libs/ardour/midi_source.cc5
-rw-r--r--libs/ardour/session_state.cc2
-rw-r--r--libs/ardour/smf_source.cc289
-rw-r--r--libs/ardour/sndfilesource.cc4
-rw-r--r--libs/ardour/source.cc2
12 files changed, 600 insertions, 51 deletions
diff --git a/libs/ardour/ardour/midi_events.h b/libs/ardour/ardour/midi_events.h
new file mode 100644
index 0000000000..f7e2442109
--- /dev/null
+++ b/libs/ardour/ardour/midi_events.h
@@ -0,0 +1,136 @@
+/* Definitions to ease working with raw MIDI.
+ *
+ * Adapted from ALSA's asounddef.h
+ *
+ * This library is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+#ifndef MIDI_H
+#define MIDI_H
+
+
+/**
+ * \defgroup midi MIDI Definitions
+ * MIDI command and controller number definitions.
+ * \{
+ */
+
+// Commands:
+
+#define MIDI_CMD_NOTE_OFF 0x80 /**< note off */
+#define MIDI_CMD_NOTE_ON 0x90 /**< note on */
+#define MIDI_CMD_NOTE_PRESSURE 0xA0 /**< key pressure */
+#define MIDI_CMD_CONTROL 0xB0 /**< control change */
+#define MIDI_CMD_PGM_CHANGE 0xC0 /**< program change */
+#define MIDI_CMD_CHANNEL_PRESSURE 0xD0 /**< channel pressure */
+#define MIDI_CMD_BENDER 0xE0 /**< pitch bender */
+
+#define MIDI_CMD_COMMON_SYSEX 0xF0 /**< sysex (system exclusive) begin */
+#define MIDI_CMD_COMMON_MTC_QUARTER 0xF1 /**< MTC quarter frame */
+#define MIDI_CMD_COMMON_SONG_POS 0xF2 /**< song position */
+#define MIDI_CMD_COMMON_SONG_SELECT 0xF3 /**< song select */
+#define MIDI_CMD_COMMON_TUNE_REQUEST 0xF6 /**< tune request */
+#define MIDI_CMD_COMMON_SYSEX_END 0xF7 /**< end of sysex */
+#define MIDI_CMD_COMMON_CLOCK 0xF8 /**< clock */
+#define MIDI_CMD_COMMON_TICK 0xF9 /**< tick */
+#define MIDI_CMD_COMMON_START 0xFA /**< start */
+#define MIDI_CMD_COMMON_CONTINUE 0xFB /**< continue */
+#define MIDI_CMD_COMMON_STOP 0xFC /**< stop */
+#define MIDI_CMD_COMMON_SENSING 0xFE /**< active sensing */
+#define MIDI_CMD_COMMON_RESET 0xFF /**< reset */
+
+
+// Controllers:
+
+#define MIDI_CTL_MSB_BANK 0x00 /**< Bank selection */
+#define MIDI_CTL_MSB_MODWHEEL 0x01 /**< Modulation */
+#define MIDI_CTL_MSB_BREATH 0x02 /**< Breath */
+#define MIDI_CTL_MSB_FOOT 0x04 /**< Foot */
+#define MIDI_CTL_MSB_PORTAMENTO_TIME 0x05 /**< Portamento time */
+#define MIDI_CTL_MSB_DATA_ENTRY 0x06 /**< Data entry */
+#define MIDI_CTL_MSB_MAIN_VOLUME 0x07 /**< Main volume */
+#define MIDI_CTL_MSB_BALANCE 0x08 /**< Balance */
+#define MIDI_CTL_MSB_PAN 0x0A /**< Panpot */
+#define MIDI_CTL_MSB_EXPRESSION 0x0B /**< Expression */
+#define MIDI_CTL_MSB_EFFECT1 0x0C /**< Effect1 */
+#define MIDI_CTL_MSB_EFFECT2 0x0D /**< Effect2 */
+#define MIDI_CTL_MSB_GENERAL_PURPOSE1 0x10 /**< General purpose 1 */
+#define MIDI_CTL_MSB_GENERAL_PURPOSE2 0x11 /**< General purpose 2 */
+#define MIDI_CTL_MSB_GENERAL_PURPOSE3 0x12 /**< General purpose 3 */
+#define MIDI_CTL_MSB_GENERAL_PURPOSE4 0x13 /**< General purpose 4 */
+#define MIDI_CTL_LSB_BANK 0x20 /**< Bank selection */
+#define MIDI_CTL_LSB_MODWHEEL 0x21 /**< Modulation */
+#define MIDI_CTL_LSB_BREATH 0x22 /**< Breath */
+#define MIDI_CTL_LSB_FOOT 0x24 /**< Foot */
+#define MIDI_CTL_LSB_PORTAMENTO_TIME 0x25 /**< Portamento time */
+#define MIDI_CTL_LSB_DATA_ENTRY 0x26 /**< Data entry */
+#define MIDI_CTL_LSB_MAIN_VOLUME 0x27 /**< Main volume */
+#define MIDI_CTL_LSB_BALANCE 0x28 /**< Balance */
+#define MIDI_CTL_LSB_PAN 0x2A /**< Panpot */
+#define MIDI_CTL_LSB_EXPRESSION 0x2B /**< Expression */
+#define MIDI_CTL_LSB_EFFECT1 0x2C /**< Effect1 */
+#define MIDI_CTL_LSB_EFFECT2 0x2D /**< Effect2 */
+#define MIDI_CTL_LSB_GENERAL_PURPOSE1 0x30 /**< General purpose 1 */
+#define MIDI_CTL_LSB_GENERAL_PURPOSE2 0x31 /**< General purpose 2 */
+#define MIDI_CTL_LSB_GENERAL_PURPOSE3 0x32 /**< General purpose 3 */
+#define MIDI_CTL_LSB_GENERAL_PURPOSE4 0x33 /**< General purpose 4 */
+#define MIDI_CTL_SUSTAIN 0x40 /**< Sustain pedal */
+#define MIDI_CTL_PORTAMENTO 0x41 /**< Portamento */
+#define MIDI_CTL_SOSTENUTO 0x42 /**< Sostenuto */
+#define MIDI_CTL_SUSTENUTO 0x42 /**< Sostenuto (a typo in the older version) */
+#define MIDI_CTL_SOFT_PEDAL 0x43 /**< Soft pedal */
+#define MIDI_CTL_LEGATO_FOOTSWITCH 0x44 /**< Legato foot switch */
+#define MIDI_CTL_HOLD2 0x45 /**< Hold2 */
+#define MIDI_CTL_SC1_SOUND_VARIATION 0x46 /**< SC1 Sound Variation */
+#define MIDI_CTL_SC2_TIMBRE 0x47 /**< SC2 Timbre */
+#define MIDI_CTL_SC3_RELEASE_TIME 0x48 /**< SC3 Release Time */
+#define MIDI_CTL_SC4_ATTACK_TIME 0x49 /**< SC4 Attack Time */
+#define MIDI_CTL_SC5_BRIGHTNESS 0x4A /**< SC5 Brightness */
+#define MIDI_CTL_SC6 0x4B /**< SC6 */
+#define MIDI_CTL_SC7 0x4C /**< SC7 */
+#define MIDI_CTL_SC8 0x4D /**< SC8 */
+#define MIDI_CTL_SC9 0x4E /**< SC9 */
+#define MIDI_CTL_SC10 0x4F /**< SC10 */
+#define MIDI_CTL_GENERAL_PURPOSE5 0x50 /**< General purpose 5 */
+#define MIDI_CTL_GENERAL_PURPOSE6 0x51 /**< General purpose 6 */
+#define MIDI_CTL_GENERAL_PURPOSE7 0x52 /**< General purpose 7 */
+#define MIDI_CTL_GENERAL_PURPOSE8 0x53 /**< General purpose 8 */
+#define MIDI_CTL_PORTAMENTO_CONTROL 0x54 /**< Portamento control */
+#define MIDI_CTL_E1_REVERB_DEPTH 0x5B /**< E1 Reverb Depth */
+#define MIDI_CTL_E2_TREMOLO_DEPTH 0x5C /**< E2 Tremolo Depth */
+#define MIDI_CTL_E3_CHORUS_DEPTH 0x5D /**< E3 Chorus Depth */
+#define MIDI_CTL_E4_DETUNE_DEPTH 0x5E /**< E4 Detune Depth */
+#define MIDI_CTL_E5_PHASER_DEPTH 0x5F /**< E5 Phaser Depth */
+#define MIDI_CTL_DATA_INCREMENT 0x60 /**< Data Increment */
+#define MIDI_CTL_DATA_DECREMENT 0x61 /**< Data Decrement */
+#define MIDI_CTL_NONREG_PARM_NUM_LSB 0x62 /**< Non-registered parameter number */
+#define MIDI_CTL_NONREG_PARM_NUM_MSB 0x63 /**< Non-registered parameter number */
+#define MIDI_CTL_REGIST_PARM_NUM_LSB 0x64 /**< Registered parameter number */
+#define MIDI_CTL_REGIST_PARM_NUM_MSB 0x65 /**< Registered parameter number */
+#define MIDI_CTL_ALL_SOUNDS_OFF 0x78 /**< All sounds off */
+#define MIDI_CTL_RESET_CONTROLLERS 0x79 /**< Reset Controllers */
+#define MIDI_CTL_LOCAL_CONTROL_SWITCH 0x7A /**< Local control switch */
+#define MIDI_CTL_ALL_NOTES_OFF 0x7B /**< All notes off */
+#define MIDI_CTL_OMNI_OFF 0x7C /**< Omni off */
+#define MIDI_CTL_OMNI_ON 0x7D /**< Omni on */
+#define MIDI_CTL_MONO1 0x7E /**< Mono1 */
+#define MIDI_CTL_MONO2 0x7F /**< Mono2 */
+//@}
+
+
+/** \} */
+
+#endif /* MIDI_H */
diff --git a/libs/ardour/ardour/midi_ring_buffer.h b/libs/ardour/ardour/midi_ring_buffer.h
index 53057dbcba..08ddfd6ca3 100644
--- a/libs/ardour/ardour/midi_ring_buffer.h
+++ b/libs/ardour/ardour/midi_ring_buffer.h
@@ -192,15 +192,20 @@ MidiRingBuffer::read(MidiBuffer& dst, jack_nframes_t start, jack_nframes_t end)
size_t count = 0;
size_t limit = read_space();
+ assert(time >= start); // FIXME: deal with skipped cycles/lost notes somehow
+
while (time <= end && limit > 0) {
MidiEvent* const read_ev = &_ev_buf[priv_read_ptr];
if (time >= start) {
dst.push_back(*read_ev);
- printf("MRB - read %xd %d %d with time %u at index %zu\n",
+ printf("MRB - read %#X %d %d with time %u at index %zu\n",
read_ev->buffer[0], read_ev->buffer[1], read_ev->buffer[2], read_ev->time,
priv_read_ptr);
} else {
- cerr << "MRB: LOST EVENT!" << endl;
+ printf("MRB - SKIPPING - %#X %d %d with time %u at index %zu\n",
+ read_ev->buffer[0], read_ev->buffer[1], read_ev->buffer[2], read_ev->time,
+ priv_read_ptr);
+ break;
}
clear_event(priv_read_ptr);
@@ -215,7 +220,8 @@ MidiRingBuffer::read(MidiBuffer& dst, jack_nframes_t start, jack_nframes_t end)
}
g_atomic_int_set(&_read_ptr, priv_read_ptr);
- printf("(R) read space: %zu\n", read_space());
+
+ //printf("(R) read space: %zu\n", read_space());
return count;
}
diff --git a/libs/ardour/ardour/midi_source.h b/libs/ardour/ardour/midi_source.h
index 2ecae5c37b..f1ede9c842 100644
--- a/libs/ardour/ardour/midi_source.h
+++ b/libs/ardour/ardour/midi_source.h
@@ -37,7 +37,7 @@ using std::string;
namespace ARDOUR {
-class MidiBuffer;
+class MidiRingBuffer;
/** Source for MIDI data */
class MidiSource : public Source
@@ -47,8 +47,8 @@ class MidiSource : public Source
MidiSource (const XMLNode&);
virtual ~MidiSource ();
- virtual jack_nframes_t read (MidiBuffer& dst, jack_nframes_t start, jack_nframes_t cnt) const;
- virtual jack_nframes_t write (MidiBuffer& src, jack_nframes_t cnt);
+ virtual jack_nframes_t read (MidiRingBuffer& dst, jack_nframes_t start, jack_nframes_t cnt) const;
+ virtual jack_nframes_t write (MidiRingBuffer& src, jack_nframes_t cnt);
virtual void mark_for_remove() = 0;
virtual void mark_streaming_write_completed () {}
@@ -70,8 +70,8 @@ class MidiSource : public Source
int set_state (const XMLNode&);
protected:
- virtual jack_nframes_t read_unlocked (MidiBuffer& dst, jack_nframes_t start, jack_nframes_t cnt) const = 0;
- virtual jack_nframes_t write_unlocked (MidiBuffer& dst, jack_nframes_t cnt) = 0;
+ virtual jack_nframes_t read_unlocked (MidiRingBuffer& dst, jack_nframes_t start, jack_nframes_t cnt) const = 0;
+ virtual jack_nframes_t write_unlocked (MidiRingBuffer& dst, jack_nframes_t cnt) = 0;
mutable Glib::Mutex _lock;
string _captured_for;
diff --git a/libs/ardour/ardour/midi_util.h b/libs/ardour/ardour/midi_util.h
new file mode 100644
index 0000000000..d85ab128fe
--- /dev/null
+++ b/libs/ardour/ardour/midi_util.h
@@ -0,0 +1,72 @@
+/*
+ Copyright (C) 2006 Paul Davis
+ Written by Dave Robillard, 2006
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+*/
+
+#ifndef __ardour_midi_util_h__
+#define __ardour_midi_util_h__
+
+#include <ardour/midi_events.h>
+
+namespace ARDOUR {
+
+/** Return the size of the given event NOT including the status byte,
+ * or -1 if unknown (eg sysex)
+ */
+int
+midi_event_size(unsigned char status)
+{
+ if (status >= 0x80 && status <= 0xE0) {
+ status &= 0xF0; // mask off the channel
+ }
+
+ switch (status) {
+ case MIDI_CMD_NOTE_OFF:
+ case MIDI_CMD_NOTE_ON:
+ case MIDI_CMD_NOTE_PRESSURE:
+ case MIDI_CMD_CONTROL:
+ case MIDI_CMD_BENDER:
+ case MIDI_CMD_COMMON_SONG_POS:
+ return 2;
+
+ case MIDI_CMD_PGM_CHANGE:
+ case MIDI_CMD_CHANNEL_PRESSURE:
+ case MIDI_CMD_COMMON_MTC_QUARTER:
+ case MIDI_CMD_COMMON_SONG_SELECT:
+ return 1;
+
+ case MIDI_CMD_COMMON_TUNE_REQUEST:
+ case MIDI_CMD_COMMON_SYSEX_END:
+ case MIDI_CMD_COMMON_CLOCK:
+ case MIDI_CMD_COMMON_START:
+ case MIDI_CMD_COMMON_CONTINUE:
+ case MIDI_CMD_COMMON_STOP:
+ case MIDI_CMD_COMMON_SENSING:
+ case MIDI_CMD_COMMON_RESET:
+ return 0;
+
+ case MIDI_CMD_COMMON_SYSEX:
+ return -1;
+ }
+
+ return -1;
+}
+
+} // namespace ARDOUR
+
+#endif /* __ardour_midi_util_h__ */
diff --git a/libs/ardour/ardour/smf_source.h b/libs/ardour/ardour/smf_source.h
index d5bcd3ee4c..31e9c2205d 100644
--- a/libs/ardour/ardour/smf_source.h
+++ b/libs/ardour/ardour/smf_source.h
@@ -21,13 +21,14 @@
#ifndef __ardour_smf_filesource_h__
#define __ardour_smf_filesource_h__
+#include <cstdio>
#include <time.h>
#include <ardour/midi_source.h>
namespace ARDOUR {
-class MidiBuffer;
+class MidiRingBuffer;
/** Standard Midi File (Type 0) Source */
class SMFSource : public MidiSource {
@@ -84,23 +85,35 @@ class SMFSource : public MidiSource {
XMLNode& get_state ();
int set_state (const XMLNode&);
- protected:
+ private:
int init (string idstr, bool must_exist);
- jack_nframes_t read_unlocked (MidiBuffer& dst, jack_nframes_t start, jack_nframes_t cn) const;
- jack_nframes_t write_unlocked (MidiBuffer& dst, jack_nframes_t cnt);
+ jack_nframes_t read_unlocked (MidiRingBuffer& dst, jack_nframes_t start, jack_nframes_t cn) const;
+ jack_nframes_t write_unlocked (MidiRingBuffer& dst, jack_nframes_t cnt);
bool find (std::string path, bool must_exist, bool& is_new);
bool removable() const;
bool writable() const { return _flags & Writable; }
- uint16_t _channel;
- string _path;
- Flag _flags;
- string _take_id;
- bool _allow_remove_if_empty;
- uint64_t _timeline_position;
+ int open();
+
+ void write_chunk_header(char id[4], uint32_t length);
+ void write_chunk(char id[4], uint32_t length, void* data);
+ size_t write_var_len(uint32_t val);
+ uint32_t read_var_len() const;
+ int read_event(MidiEvent& ev) const;
+
+ uint16_t _channel;
+ string _path;
+ Flag _flags;
+ string _take_id;
+ bool _allow_remove_if_empty;
+ uint64_t _timeline_position;
+ FILE* _fd;
+ jack_nframes_t _last_ev_time; // last frame time written, relative to source start
+ uint32_t _track_size;
+ uint32_t _header_size;
static string _search_path;
};
diff --git a/libs/ardour/midi_diskstream.cc b/libs/ardour/midi_diskstream.cc
index 223cfc15f6..b399089a27 100644
--- a/libs/ardour/midi_diskstream.cc
+++ b/libs/ardour/midi_diskstream.cc
@@ -535,6 +535,8 @@ MidiDiskstream::process (jack_nframes_t transport_frame, jack_nframes_t nframes,
// Pump entire port buffer into the ring buffer (FIXME!)
_capture_buf->write(_source_port->get_midi_buffer(), transport_frame);
+ // FIXME: hackitty hack, don't come back
+ //_write_source->ViewDataRangeReady (_write_source->length(), rec_nframes); /* EMIT SIGNAL */
/*
for (size_t i=0; i < _source_port->size(); ++i) {
cerr << "DISKSTREAM GOT EVENT(1) " << i << "!!\n";
@@ -562,7 +564,7 @@ MidiDiskstream::process (jack_nframes_t transport_frame, jack_nframes_t nframes,
/* can't do actual capture yet - waiting for latency effects to finish before we start*/
- throw; // forget all that jazz!
+ // Ummm.. well, I suppose we'll just hang out for a bit?
playback_distance = nframes;
@@ -629,19 +631,19 @@ MidiDiskstream::commit (jack_nframes_t nframes)
if (adjust_capture_position) {
_capture_buf->increment_write_ptr (adjust_capture_position);
}
-
+*/
if (adjust_capture_position != 0) {
capture_captured += adjust_capture_position;
adjust_capture_position = 0;
}
if (_slaved) {
- need_butler = _playback_buf->write_space() >= _playback_buf->bufsize() / 2;
+ need_butler = _playback_buf->write_space() >= _playback_buf->capacity() / 2;
} else {
need_butler = _playback_buf->write_space() >= disk_io_chunk_frames
|| _capture_buf->read_space() >= disk_io_chunk_frames;
}
- */
+
state_lock.unlock();
_processed = false;
@@ -849,7 +851,7 @@ MidiDiskstream::do_refill ()
}
if (file_frame == max_frames) {
- cerr << "No refill 4 (EOF)\n";
+ //cerr << "No refill 4 (EOF)\n";
/* at end: nothing to do */
@@ -905,10 +907,55 @@ out:
int
MidiDiskstream::do_flush (Session::RunContext context, bool force_flush)
{
- /* hey, so did you write that data? */
+ uint32_t to_write;
+ int32_t ret = 0;
+ // FIXME: I'd be lying if I said I knew what this thing was
+ //RingBufferNPT<CaptureTransition>::rw_vector transvec;
+ jack_nframes_t total;
- // oh yeah, you bet. wrote it good. honest.
-
+ _write_data_count = 0;
+
+ total = _capture_buf->read_space();
+
+
+ // FIXME: put this condition back in! (removed for testing)
+ if (total == 0) { // || (total < disk_io_chunk_frames && !force_flush && was_recording)) {
+ //cerr << "MDS - no flush 1\n";
+ goto out;
+ }
+
+ /* if there are 2+ chunks of disk i/o possible for
+ this track, let the caller know so that it can arrange
+ for us to be called again, ASAP.
+
+ if we are forcing a flush, then if there is* any* extra
+ work, let the caller know.
+
+ if we are no longer recording and there is any extra work,
+ let the caller know too.
+ */
+
+ if (total >= 2 * disk_io_chunk_frames || ((force_flush || !was_recording) && total > disk_io_chunk_frames)) {
+ ret = 1;
+ }
+
+ //to_write = min (disk_io_chunk_frames, (jack_nframes_t) vector.len[0]);
+ to_write = disk_io_chunk_frames;
+
+ assert(!destructive());
+
+ if ((!_write_source) || _write_source->write (*_capture_buf, to_write) != to_write) {
+ //cerr << "MDS - no flush 2\n";
+ error << string_compose(_("AudioDiskstream %1: cannot write to disk"), _id) << endmsg;
+ return -1;
+ } else {
+ //cerr << "MDS - flushed\n";
+ }
+
+ //(*chan).curr_capture_cnt += to_write;
+
+out:
+ //return ret;
return 0;
}
@@ -1081,7 +1128,7 @@ MidiDiskstream::finish_capture (bool rec_monitors_input)
CaptureInfo* ci = new CaptureInfo;
- ci->start = capture_start_frame;
+ ci->start = capture_start_frame;
ci->frames = capture_captured;
/* XXX theoretical race condition here. Need atomic exchange ?
@@ -1344,8 +1391,10 @@ MidiDiskstream::reset_write_sources (bool mark_write_complete, bool force)
if (_write_source && mark_write_complete) {
_write_source->mark_streaming_write_completed ();
}
- use_new_write_source ();
- assert(_write_source);
+
+ if (!_write_source) {
+ use_new_write_source ();
+ }
}
int
@@ -1462,6 +1511,9 @@ MidiDiskstream::get_playback(MidiBuffer& dst, jack_nframes_t start, jack_nframes
for (size_t i=0; i < dst.size(); ++i) {
assert(dst[i].time >= start);
assert(dst[i].time <= end);
+ cerr << "Translating event stamp " << dst[i].time << " to ";
dst[i].time -= start;
+ cerr << dst[i].time << endl;
+
}
}
diff --git a/libs/ardour/midi_region.cc b/libs/ardour/midi_region.cc
index 9098bf6f40..07cbd279ff 100644
--- a/libs/ardour/midi_region.cc
+++ b/libs/ardour/midi_region.cc
@@ -198,6 +198,7 @@ MidiRegion::_read_at (const SourceList& srcs, MidiRingBuffer& dst,
jack_nframes_t position, jack_nframes_t dur,
uint32_t chan_n, jack_nframes_t read_frames, jack_nframes_t skip_frames) const
{
+/*
MidiEvent ev;
RawMidi data[4];
@@ -223,7 +224,7 @@ MidiRegion::_read_at (const SourceList& srcs, MidiRingBuffer& dst,
_read_data_count += dur;
return dur;
-#if 0
+*/
jack_nframes_t internal_offset;
jack_nframes_t buf_offset;
jack_nframes_t to_read;
@@ -234,8 +235,8 @@ MidiRegion::_read_at (const SourceList& srcs, MidiRingBuffer& dst,
if (position < _position) {
internal_offset = 0;
- buf_offset = _position - position;
- cnt -= buf_offset;
+ //buf_offset = _position - position;
+ //cnt -= buf_offset;
} else {
internal_offset = position - _position;
buf_offset = 0;
@@ -246,7 +247,7 @@ MidiRegion::_read_at (const SourceList& srcs, MidiRingBuffer& dst,
}
- if ((to_read = min (cnt, _length - internal_offset)) == 0) {
+ if ((to_read = min (dur, _length - internal_offset)) == 0) {
return 0; /* read nothing */
}
@@ -260,14 +261,13 @@ MidiRegion::_read_at (const SourceList& srcs, MidiRingBuffer& dst,
_read_data_count = 0;
MidiSource& src = midi_source(chan_n);
- if (src.read (buf, _start + internal_offset, to_read) != to_read) {
+ if (src.read (dst, _start + internal_offset, to_read) != to_read) {
return 0; /* "read nothing" */
}
_read_data_count += src.read_data_count();
return to_read;
-#endif
}
XMLNode&
diff --git a/libs/ardour/midi_source.cc b/libs/ardour/midi_source.cc
index 802324e19b..1438b1fdbe 100644
--- a/libs/ardour/midi_source.cc
+++ b/libs/ardour/midi_source.cc
@@ -32,6 +32,7 @@
#include <pbd/pthread_utils.h>
#include <ardour/midi_source.h>
+#include <ardour/midi_ring_buffer.h>
#include "i18n.h"
@@ -90,14 +91,14 @@ MidiSource::set_state (const XMLNode& node)
}
jack_nframes_t
-MidiSource::read (MidiBuffer& dst, jack_nframes_t start, jack_nframes_t cnt) const
+MidiSource::read (MidiRingBuffer& dst, jack_nframes_t start, jack_nframes_t cnt) const
{
Glib::Mutex::Lock lm (_lock);
return read_unlocked (dst, start, cnt);
}
jack_nframes_t
-MidiSource::write (MidiBuffer& dst, jack_nframes_t cnt)
+MidiSource::write (MidiRingBuffer& dst, jack_nframes_t cnt)
{
Glib::Mutex::Lock lm (_lock);
return write_unlocked (dst, cnt);
diff --git a/libs/ardour/session_state.cc b/libs/ardour/session_state.cc
index 5e96a8ca8a..cefae9dd3d 100644
--- a/libs/ardour/session_state.cc
+++ b/libs/ardour/session_state.cc
@@ -430,6 +430,7 @@ Session::setup_raid_path (string path)
fspath += tape_dir_name;
AudioFileSource::set_search_path (fspath);
+ SMFSource::set_search_path (fspath); // FIXME: should be different
return;
}
@@ -488,6 +489,7 @@ Session::setup_raid_path (string path)
/* set the AudioFileSource search path */
AudioFileSource::set_search_path (fspath);
+ SMFSource::set_search_path (fspath); // FIXME: should be different
/* reset the round-robin soundfile path thingie */
diff --git a/libs/ardour/smf_source.cc b/libs/ardour/smf_source.cc
index dfe1419c4e..73d8d5f7a5 100644
--- a/libs/ardour/smf_source.cc
+++ b/libs/ardour/smf_source.cc
@@ -34,6 +34,8 @@
#include <ardour/smf_source.h>
#include <ardour/session.h>
+#include <ardour/midi_ring_buffer.h>
+#include <ardour/midi_util.h>
#include "i18n.h"
@@ -47,7 +49,14 @@ uint64_t SMFSource::header_position_offset;
*/
SMFSource::SMFSource (std::string path, Flag flags)
- : MidiSource (region_name_from_path(path)), _flags (flags)
+ : MidiSource (region_name_from_path(path))
+ , _channel(0)
+ , _flags (flags)
+ , _allow_remove_if_empty(true)
+ , _timeline_position (0)
+ , _fd (0)
+ , _last_ev_time(0)
+ , _track_size(0)
{
/* constructor used for new internal-to-session files. file cannot exist */
@@ -55,13 +64,24 @@ SMFSource::SMFSource (std::string path, Flag flags)
throw failed_constructor ();
}
+ if (open()) {
+ throw failed_constructor ();
+ }
+
assert(_name.find("/") == string::npos);
SourceCreated (this); /* EMIT SIGNAL */
}
SMFSource::SMFSource (const XMLNode& node)
- : MidiSource (node), _flags (Flag (Writable|CanRename))
+ : MidiSource (node)
+ , _channel(0)
+ , _flags (Flag (Writable|CanRename))
+ , _allow_remove_if_empty(true)
+ , _timeline_position (0)
+ , _fd (0)
+ , _last_ev_time(0)
+ , _track_size(0)
{
/* constructor used for existing internal-to-session files. file must exist */
@@ -73,6 +93,10 @@ SMFSource::SMFSource (const XMLNode& node)
throw failed_constructor ();
}
+ if (open()) {
+ throw failed_constructor ();
+ }
+
assert(_name.find("/") == string::npos);
SourceCreated (this); /* EMIT SIGNAL */
@@ -95,9 +119,8 @@ SMFSource::removable () const
int
SMFSource::init (string pathstr, bool must_exist)
{
- //bool is_new = false;
+ bool is_new = false;
- /*
if (!find (pathstr, must_exist, is_new)) {
cerr << "cannot find " << pathstr << " with me = " << must_exist << endl;
return -1;
@@ -106,38 +129,218 @@ SMFSource::init (string pathstr, bool must_exist)
if (is_new && must_exist) {
return -1;
}
- */
-
- // Yeah, we found it. Swear.
assert(_name.find("/") == string::npos);
return 0;
}
int
-SMFSource::update_header (jack_nframes_t when, struct tm&, time_t)
+SMFSource::open()
{
+ cerr << "Opening SMF file " << path() << " writeable: " << writable() << endl;
+
+ // FIXME
+ //_fd = fopen(path().c_str(), writable() ? "r+" : "r");
+ _fd = fopen(path().c_str(), "w+");
+
+ // FIXME: pad things out so writing the header later doesn't overwrite data
+ flush_header();
+
+ // FIXME
+ //return (_fd == 0) ? -1 : 0;
return 0;
}
int
+SMFSource::update_header (jack_nframes_t when, struct tm&, time_t)
+{
+ _timeline_position = when;
+ return flush_header();
+}
+
+int
SMFSource::flush_header ()
{
+ // FIXME: write timeline position somehow?
+
+ cerr << "SMF Flushing header\n";
+
+ assert(_fd);
+
+ const uint16_t type = GUINT16_TO_BE(0); // SMF Type 0 (single track)
+ const uint16_t ntracks = GUINT16_TO_BE(1); // Number of tracks (always 1 for Type 0)
+ const uint16_t division = GUINT16_TO_BE(1920); // FIXME FIXME FIXME PPQN
+
+ char data[6];
+ memcpy(data, &type, 2);
+ memcpy(data+2, &ntracks, 2);
+ memcpy(data+4, &division, 2);
+
+ _fd = freopen(path().c_str(), "r+", _fd);
+ assert(_fd);
+ fseek(_fd, 0, 0);
+ write_chunk("MThd", 6, data);
+ //if (_track_size > 0) {
+ write_chunk_header("MTrk", _track_size);
+ //}
+
+ _header_size = 22;
+
+ fflush(_fd);
+
+ return 0;
+}
+
+/** Returns the offset of the first event in the file with a time past @a start,
+ * relative to the start of the source.
+ *
+ * Returns -1 if not found.
+ */
+/*
+long
+SMFSource::find_first_event_after(jack_nframes_t start)
+{
+ // FIXME: obviously this is slooow
+
+ fseek(_fd, _header_size, 0);
+
+ while ( ! feof(_fd) ) {
+ const uint32_t delta_time = read_var_len();
+
+ if (delta_time > start)
+ return delta_time;
+ }
+
+ return -1;
+}
+*/
+
+/** Read an event from the current position in file.
+ *
+ * File position MUST be at the beginning of a delta time, or this will die very messily.
+ * ev.buffer must be of size ev.size, and large enough for the event.
+ *
+ * Returns 0 on success, -1 if EOF.
+ */
+int
+SMFSource::read_event(MidiEvent& ev) const
+{
+#if 0
+ if (feof(_fd)) {
+ return -1;
+ }
+
+ uint32_t delta_time = read_var_len();
+ int status = fgetc(_fd);
+ assert(status != EOF); // FIXME die gracefully
+ ev.buffer[0] = (unsigned char)status;
+ ev.size = midi_event_size(ev.buffer[0]) + 1;
+ fread(ev.buffer+1, 1, ev.size - 1, _fd);
+
+ printf("SMF - read event, delta = %u, size = %zu, data = ",
+ delta_time, ev.size);
+ for (size_t i=0; i < ev.size; ++i) {
+ printf("%X ", ev.buffer[i]);
+ }
+ printf("\n");
+#endif
return 0;
}
jack_nframes_t
-SMFSource::read_unlocked (MidiBuffer& dst, jack_nframes_t start, jack_nframes_t cnt) const
+SMFSource::read_unlocked (MidiRingBuffer& dst, jack_nframes_t start, jack_nframes_t cnt) const
{
- dst.clear();
+#if 0
+ cerr << "SMF - read " << start << " -- " << cnt;
+
+ // FIXME: ugh
+ unsigned char ev_buf[MidiBuffer::max_event_size()];
+ MidiEvent ev;
+ ev.time = 0;
+ ev.size = MidiBuffer::max_event_size();
+ ev.buffer = ev_buf;
+
+ while (true) {
+ int ret = read_event(ev);
+ if (ret == -1) {
+ break;
+ }
+
+ if (ev.time >= start) {
+ if (ev.time > start + cnt) {
+ break;
+ } else {
+ dst.write(ev);
+ }
+ }
+ }
+#endif
+ cerr << "SMF pretending to read" << endl;
+
+ MidiEvent ev;
+ RawMidi data[4];
+
+ const char note = rand()%30 + 30;
+
+ ev.buffer = data;
+ ev.time = start+1; // FIXME: bug at 0?
+ ev.size = 3;
+
+ data[0] = 0x90;
+ data[1] = note;
+ data[2] = 120;
+
+ dst.write(ev);
+
+ ev.buffer = data;
+ ev.time = start + (jack_nframes_t)(cnt * 8.0/10.0);
+ ev.size = 3;
+
+ data[0] = 0x80;
+ data[1] = note;
+ data[2] = 64;
+
+ dst.write(ev);
+
+ //dst.clear();
+
return cnt;
}
jack_nframes_t
-SMFSource::write_unlocked (MidiBuffer& src, jack_nframes_t cnt)
+SMFSource::write_unlocked (MidiRingBuffer& src, jack_nframes_t cnt)
{
+ cerr << "SMF WRITE -- " << _length << "--" << cnt << endl;
+
+ MidiBuffer buf(1024); // FIXME: allocation, size?
+ src.read(buf, /*_length*/0, _length + cnt); // FIXME?
+
+ fseek(_fd, 0, SEEK_END);
+
+ // FIXME: start of source time?
+
+ for (size_t i=0; i < buf.size(); ++i) {
+ const MidiEvent& ev = buf[i];
+ assert(ev.time >= _timeline_position);
+ uint32_t delta_time = (ev.time - _timeline_position) - _last_ev_time;
+
+ printf("SMF - writing event, delta = %u, size = %zu, data = ",
+ delta_time, ev.size);
+ for (size_t i=0; i < ev.size; ++i) {
+ printf("%X ", ev.buffer[i]);
+ }
+ printf("\n");
+
+ size_t stamp_size = write_var_len(delta_time);
+ fwrite(ev.buffer, 1, ev.size, _fd);
+ _last_ev_time += delta_time;
+ _track_size += stamp_size + ev.size;
+ }
+
+ fflush(_fd);
+
ViewDataRangeReady (_length, cnt); /* EMIT SIGNAL */
- _length += cnt;
+ update_length(_length, cnt);
return cnt;
}
@@ -443,3 +646,65 @@ SMFSource::is_empty (string path)
return false;
}
+
+void
+SMFSource::write_chunk_header(char id[4], uint32_t length)
+{
+ const uint32_t length_be = GUINT32_TO_BE(length);
+
+ fwrite(id, 1, 4, _fd);
+ fwrite(&length_be, 4, 1, _fd);
+}
+
+void
+SMFSource::write_chunk(char id[4], uint32_t length, void* data)
+{
+ write_chunk_header(id, length);
+
+ fwrite(data, 1, length, _fd);
+}
+
+/** Returns the size (in bytes) of the value written. */
+size_t
+SMFSource::write_var_len(uint32_t value)
+{
+ size_t ret = 0;
+
+ uint32_t buffer = value & 0x7F;
+
+ while ( (value >>= 7) ) {
+ buffer <<= 8;
+ buffer |= ((value & 0x7F) | 0x80);
+ }
+
+ while (true) {
+ printf("Writing var len byte %X\n", (unsigned char)buffer);
+ ++ret;
+ fputc(buffer, _fd);
+ if (buffer & 0x80)
+ buffer >>= 8;
+ else
+ break;
+ }
+
+ return ret;
+}
+
+uint32_t
+SMFSource::read_var_len() const
+{
+ assert(!feof(_fd));
+
+ uint32_t value;
+ unsigned char c;
+
+ if ( (value = getc(_fd)) & 0x80 ) {
+ value &= 0x7F;
+ do {
+ assert(!feof(_fd));
+ value = (value << 7) + ((c = getc(_fd)) & 0x7F);
+ } while (c & 0x80);
+ }
+
+ return value;
+}
diff --git a/libs/ardour/sndfilesource.cc b/libs/ardour/sndfilesource.cc
index 4d5f42b028..83c3c982b0 100644
--- a/libs/ardour/sndfilesource.cc
+++ b/libs/ardour/sndfilesource.cc
@@ -500,8 +500,8 @@ SndFileSource::set_header_timeline_position ()
return;
}
- _broadcast_info->time_reference_high = (timeline_position >> 32);
- _broadcast_info->time_reference_low = (timeline_position & 0xffffffff);
+ _broadcast_info->time_reference_high = (timeline_position >> 32);
+ _broadcast_info->time_reference_low = (timeline_position & 0xffffffff);
if (sf_command (sf, SFC_SET_BROADCAST_INFO, _broadcast_info, sizeof (*_broadcast_info)) != SF_TRUE) {
error << string_compose (_("cannot set broadcast info for audio file %1; Dropping broadcast info for this file"), _path) << endmsg;
diff --git a/libs/ardour/source.cc b/libs/ardour/source.cc
index bf07ede70b..0b15a8b5b8 100644
--- a/libs/ardour/source.cc
+++ b/libs/ardour/source.cc
@@ -53,6 +53,7 @@ Source::Source (string name, DataType type)
_name = name;
_use_cnt = 0;
_timestamp = 0;
+ _length = 0;
}
Source::Source (const XMLNode& node)
@@ -60,6 +61,7 @@ Source::Source (const XMLNode& node)
{
_use_cnt = 0;
_timestamp = 0;
+ _length = 0;
if (set_state (node) || _type == DataType::NIL) {
throw failed_constructor();