summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--raul/JackDriver.h2
-rw-r--r--raul/MIDIRingBuffer.h110
-rw-r--r--raul/Semaphore.h4
-rw-r--r--raul/types.h4
-rw-r--r--tests/midi_ringbuffer_test.cpp10
5 files changed, 28 insertions, 102 deletions
diff --git a/raul/JackDriver.h b/raul/JackDriver.h
index cc42c02..844f109 100644
--- a/raul/JackDriver.h
+++ b/raul/JackDriver.h
@@ -62,7 +62,7 @@ public:
jack_nframes_t buffer_size();
bool set_buffer_size(jack_nframes_t size);
- inline float sample_rate() { return jack_get_sample_rate(_client); }
+ inline jack_nframes_t sample_rate() { return jack_get_sample_rate(_client); }
inline size_t xruns() { return _xruns; }
void reset_xruns();
diff --git a/raul/MIDIRingBuffer.h b/raul/MIDIRingBuffer.h
index d054bbc..3e216e1 100644
--- a/raul/MIDIRingBuffer.h
+++ b/raul/MIDIRingBuffer.h
@@ -24,137 +24,55 @@
#include <raul/types.h>
#include <raul/RingBuffer.h>
-#include <iostream>
-
namespace Raul {
/** A MIDI RingBuffer
*/
-class MIDIRingBuffer : private Raul::RingBuffer<char> {
+class MIDIRingBuffer : private Raul::RingBuffer<Byte> {
public:
- struct MidiEvent {
- TickTime time;
- size_t size;
- unsigned char* buf;
- };
-
-
/** @param size Size in bytes.
*/
MIDIRingBuffer(size_t size)
- : RingBuffer<char>(size)
- {
- }
+ : RingBuffer<Byte>(size)
+ {}
size_t capacity() const { return _size; }
- /** Read one event and appends it to @a out. */
- //size_t read(MidiBuffer& out);
-
- /** Read events all events up to time @a end into @a out, leaving stamps intact.
- * Any events before @a start will be dropped. */
- //size_t read(MidiBuffer& out, TickTime start, TickTime end);
-
- /** Write one event */
- //size_t write(const MidiEvent& in); // deep copies in
-
- size_t write(TickTime time, size_t size, const char* buf);
- bool read(TickTime* time, size_t* size, char* buf);
-
+ size_t write(TickTime time, size_t size, const Byte* buf);
+ bool read(TickTime* time, size_t* size, Byte* buf);
};
-bool
-MIDIRingBuffer::read(TickTime* time, size_t* size, char* buf)
+inline bool
+MIDIRingBuffer::read(TickTime* time, size_t* size, Byte* buf)
{
- bool success = RingBuffer<char>::full_read(sizeof(TickTime), (char*)time);
+ bool success = RingBuffer<Byte>::full_read(sizeof(TickTime), (Byte*)time);
if (success)
- success = RingBuffer<char>::full_read(sizeof(size_t), (char*)size);
+ success = RingBuffer<Byte>::full_read(sizeof(size_t), (Byte*)size);
if (success)
- success = RingBuffer<char>::full_read(*size, buf);
+ success = RingBuffer<Byte>::full_read(*size, buf);
return success;
}
inline size_t
-MIDIRingBuffer::write(TickTime time, size_t size, const char* buf)
+MIDIRingBuffer::write(TickTime time, size_t size, const Byte* buf)
{
assert(size > 0);
if (write_space() < (sizeof(TickTime) + sizeof(size_t) + size)) {
return 0;
} else {
- RingBuffer<char>::write(sizeof(TickTime), (char*)&time);
- RingBuffer<char>::write(sizeof(size_t), (char*)&size);
- RingBuffer<char>::write(size, buf);
+ RingBuffer<Byte>::write(sizeof(TickTime), (Byte*)&time);
+ RingBuffer<Byte>::write(sizeof(size_t), (Byte*)&size);
+ RingBuffer<Byte>::write(size, buf);
return size;
}
}
-#if 0
-inline size_t
-MIDIRingBuffer::read(MidiBuffer& dst, TickTime start, TickTime end)
-{
- if (read_space() == 0)
- return 0;
-
- size_t priv_read_ptr = g_atomic_int_get(&_read_ptr);
- TickTime time = _ev_buf[priv_read_ptr].time;
- size_t count = 0;
- size_t limit = read_space();
-
- 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 %#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 {
- 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);
-
- ++count;
- --limit;
-
- priv_read_ptr =(priv_read_ptr + 1) % _size;
-
- assert(read_ev->time <= end);
- time = _ev_buf[priv_read_ptr].time;
- }
-
- g_atomic_int_set(&_read_ptr, priv_read_ptr);
-
- //printf("(R) read space: %zu\n", read_space());
-
- return count;
-}
-
-inline size_t
-MIDIRingBuffer::write(const MidiBuffer& in, TickTime time)
-{
- const size_t num_events = in.size();
- const size_t to_write = std::min(write_space(), num_events);
-
- // FIXME: double copy :/
- for (size_t i=0; i < to_write; ++i) {
- MidiEvent ev = in[i];
- ev.time += offset;
- write(ev);
- }
-
- return to_write;
-}
-#endif
-
} // namespace Raul
diff --git a/raul/Semaphore.h b/raul/Semaphore.h
index 649d148..28bd93e 100644
--- a/raul/Semaphore.h
+++ b/raul/Semaphore.h
@@ -41,6 +41,9 @@ public:
inline ~Semaphore() { sem_destroy(&_sem); }
+ inline void reset(unsigned int initial)
+ { sem_destroy(&_sem); sem_init(&_sem, 0, initial); }
+
/** Increment (and signal any waiters).
*
* Realtime safe.
@@ -61,6 +64,7 @@ public:
* Realtime safe?
*/
inline int try_wait() { return sem_trywait(&_sem); }
+
private:
sem_t _sem;
};
diff --git a/raul/types.h b/raul/types.h
index 2d96e09..00d6479 100644
--- a/raul/types.h
+++ b/raul/types.h
@@ -18,6 +18,8 @@
#ifndef RAUL_TYPES_H
#define RAUL_TYPES_H
+#include <stdint.h>
+
namespace Raul {
typedef uint32_t TickTime; ///< absolute time in ticks
@@ -26,6 +28,8 @@ typedef double BeatTime;
typedef double BeatCount;
typedef double Seconds;
+typedef unsigned char Byte;
+
}
#endif // RAUL_TYPES_H
diff --git a/tests/midi_ringbuffer_test.cpp b/tests/midi_ringbuffer_test.cpp
index c8fc508..bb20ea7 100644
--- a/tests/midi_ringbuffer_test.cpp
+++ b/tests/midi_ringbuffer_test.cpp
@@ -7,12 +7,12 @@ using namespace Raul;
void
read_write_test(MIDIRingBuffer& rb, unsigned offset)
{
- TickTime t;
- size_t size;
- char buf[5];
+ TickTime t;
+ size_t size;
+ unsigned char buf[5];
- snprintf(buf, 5, "%d", offset);
- size = strlen(buf);
+ snprintf((char*)buf, 5, "%d", offset);
+ size = strlen((char*)buf);
size_t written = rb.write(offset, size, buf);
assert(written == size);