-
-inline size_t
-MidiRingBuffer::write(double time, size_t size, const Byte* buf)
-{
- //printf("MRB - write %#X %d %d with time %lf\n",
- // buf[0], buf[1], buf[2], time);
-
- assert(size > 0);
-
- if (write_space() < (sizeof(double) + sizeof(size_t) + size)) {
- return 0;
- } else {
- MidiRingBufferBase<Byte>::write(sizeof(double), (Byte*)&time);
- MidiRingBufferBase<Byte>::write(sizeof(size_t), (Byte*)&size);
- MidiRingBufferBase<Byte>::write(size, buf);
- return size;
- }
-}
-
-
-/** Read a block of MIDI events from buffer.
- *
- * Timestamps of events returned are relative to start (ie event with stamp 0
- * occurred at start), with offset added.
- */
-inline size_t
-MidiRingBuffer::read(MidiBuffer& dst, nframes_t start, nframes_t end, nframes_t offset)
-{
- if (read_space() == 0)
- return 0;
-
- MidiEvent ev;
-
- size_t count = 0;
-
- //printf("MRB - read %u .. %u + %u\n", start, end, offset);
-
- while (read_space() > sizeof(double) + sizeof(size_t)) {
-
- full_peek(sizeof(double), (Byte*)&ev.time);
-
- if (ev.time > end)
- break;
-
- bool success = MidiRingBufferBase<Byte>::full_read(sizeof(double), (Byte*)&ev.time);
- if (success)
- success = MidiRingBufferBase<Byte>::full_read(sizeof(size_t), (Byte*)&ev.size);
-
- if (!success) {
- std::cerr << "MRB: READ ERROR (time/size)" << std::endl;
- continue;
- }
-
- if (ev.time >= start) {
- ev.time -= start;
- Byte* write_loc = dst.reserve(ev.time, ev.size);
- success = MidiRingBufferBase<Byte>::full_read(ev.size, write_loc);
-
- if (success) {
- ++count;
- //printf("MRB - read event at time %lf\n", ev.time);
- } else {
- std::cerr << "MRB: READ ERROR (data)" << std::endl;
- }
-
- } else {
- printf("MRB - SKIPPING EVENT (with time %f)\n", ev.time);
- }
- }
-
- //printf("(R) read space: %zu\n", read_space());
-
- return count;
-}
-
-