wait until there is enough space in the ring buffer v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 03 Oct 2020 12:50:31 +0200
branchv_0
changeset 12 f56620b95180
parent 11 1c0b9981eb6d
child 13 a85c191709e6
wait until there is enough space in the ring buffer
src/JackHandler.h
--- a/src/JackHandler.h	Sat Oct 03 11:37:07 2020 +0200
+++ b/src/JackHandler.h	Sat Oct 03 12:50:31 2020 +0200
@@ -194,16 +194,12 @@
 
 	void attribute(const relpipe::common::type::StringX& value) override {
 		// TODO: switch to RelationalReaderStringHandler
-		// TODO: append to current message + if this is last attribute, put whole message to the ring buffer
 		// TODO: if (continueProcessing) {} ?
 
 		// memcpy(currentMidiMessage.buffer, ….buffer, ….size);
 		// currentMidiMessage.size = …;
 		// currentMidiMessage.time = …;
 
-		// TODO: correct timing?
-		// TODO: real data
-
 		{
 			RelationContext& rel = relationContext;
 			RelationContext::RecordContext& rec = rel.recordContext;
@@ -221,20 +217,20 @@
 		relationContext.recordContext.attributeIndex++;
 
 		if (relationContext.recordContext.attributeIndex == relationContext.attributeCount) {
-			if (jack_ringbuffer_write_space(realTimeContext.ringBuffer) >= sizeof (MidiMessage)) {
-				MidiMessage m;
+
+			while (jack_ringbuffer_write_space(realTimeContext.ringBuffer) < sizeof (MidiMessage)) usleep(1000); // should not happen, the real-time thread should be faster
+
+			MidiMessage m;
 
-				// TODO: convert relationContext.recordContext to m
-				m.time = 0;
-				m.size = 3;
-				m.buffer[0] = 0x90;
-				m.buffer[1] = 0x24;
-				m.buffer[2] = 0x40;
+			// TODO: convert relationContext.recordContext to m
+			// TODO: correct timing?
+			m.time = 0;
+			m.size = 3;
+			m.buffer[0] = 0x90;
+			m.buffer[1] = 0x24;
+			m.buffer[2] = 0x40;
 
-				jack_ringbuffer_write(realTimeContext.ringBuffer, (const char *) &m, sizeof (m));
-			} else {
-				fwprintf(stderr, L"Error: ring buffer is full → skipping event.\n");
-			}
+			jack_ringbuffer_write(realTimeContext.ringBuffer, (const char *) &m, sizeof (m));
 
 			relationContext.recordContext = RelationContext::RecordContext();
 		}