2 * Copyright (C) 2005-2009 by Jonathan Woithe
3 * Copyright (C) 2005-2008 by Pieter Palmers
5 * This file is part of FFADO
6 * FFADO = Free Firewire (pro-)audio drivers for linux
8 * FFADO is based upon FreeBoB.
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 2 of the License, or
13 * (at your option) version 3 of the License.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 /* CAUTION: this module is under active development. It has been templated
26 * from the MOTU driver and many MOTU-specific details remain. Do not use
27 * this file as a reference for RME devices until initial development has
33 #include "libutil/float_cast.h"
35 #include "RmeTransmitStreamProcessor.h"
37 #include "../StreamProcessorManager.h"
38 #include "devicemanager.h"
40 #include "libieee1394/ieee1394service.h"
41 #include "libieee1394/IsoHandlerManager.h"
42 #include "libieee1394/cycletimer.h"
44 #include "libutil/ByteSwap.h"
46 #include "../../rme/rme_avdevice.h"
51 // Set to 1 to enable the generation of a 1 kHz test tone in analog output 1. Even with
52 // this defined to 1 the test tone will now only be produced if run with a non-zero
60 /* Provide more intuitive access to GCC's branch predition built-ins */
61 #define likely(x) __builtin_expect((x),1)
62 #define unlikely(x) __builtin_expect((x),0)
67 // A macro to extract specific bits from a native endian quadlet
68 #define get_bits(_d,_start,_len) (((_d)>>((_start)-(_len)+1)) & ((1<<(_len))-1))
71 RmeTransmitStreamProcessor::RmeTransmitStreamProcessor(FFADODevice
&parent
,
72 unsigned int model
, unsigned int event_size
)
73 : StreamProcessor(parent
, ePT_Transmit
)
75 , m_event_size( event_size
)
80 , streaming_has_run ( 0 )
81 , streaming_has_dryrun ( 0 )
82 , streaming_start_count ( 0 )
84 int srate
= m_Parent
.getDeviceManager().getStreamProcessorManager().getNominalRate();
85 /* Work out how many audio samples should be left between MIDI data bytes
86 * in order to stay under the MIDI hardware baud rate of 31250. MIDI data
87 * is transmitted using 10 bits per byte (including the start/stop bit) so
88 * this gives us 3125 bytes per second.
90 midi_tx_period
= lrintf(ceil((float)srate
/ 3125));
94 RmeTransmitStreamProcessor::getMaxPacketSize() {
95 Rme::Device
*dev
= static_cast<Rme::Device
*>(&m_Parent
);
96 // Each channel comprises a single 32-bit quadlet. Note that the return
98 // FIXME: getFramesPerPacket() is fixed by the sample rate class. It
99 // needs to be confirmed that the values in use are still ok even
100 // when the DDS is set to run 4% fast (something that's not presently
101 // implemented by FFADO, but should be at some point).
102 return dev
->getFramesPerPacket() * dev
->getNumChannels() * 4;
106 RmeTransmitStreamProcessor::getNominalFramesPerPacket() {
107 return static_cast<Rme::Device
*>(&m_Parent
)->getFramesPerPacket();
111 RmeTransmitStreamProcessor::resetForStreaming()
113 streaming_has_run
= 0;
114 streaming_has_dryrun
= 0;
115 streaming_start_count
= 0;
119 enum StreamProcessor::eChildReturnValue
120 RmeTransmitStreamProcessor::generatePacketHeader (
121 unsigned char *data
, unsigned int *length
,
122 unsigned char *tag
, unsigned char *sy
,
125 unsigned int cycle
= CYCLE_TIMER_GET_CYCLES(pkt_ctr
);
126 signed n_events
= getNominalFramesPerPacket();
128 // Called once per packet. Need to work out whether data should be sent
129 // in this cycle or not and then act accordingly. Need to deal with
130 // the condition where the buffers don't contain sufficient data to fill
131 // the packet. FIXME: this function is incomplete.
133 // Do housekeeping expected for all packets, irrespective of whether
134 // they will contain data.
139 uint64_t presentation_time
;
140 unsigned int presentation_cycle
;
141 int cycles_until_presentation
;
143 uint64_t transmit_at_time
;
144 unsigned int transmit_at_cycle
;
145 int cycles_until_transmit
;
147 debugOutput ( DEBUG_LEVEL_ULTRA_VERBOSE
, "Try for cycle %d\n", cycle
);
148 // check whether the packet buffer has packets for us to send.
149 // the base timestamp is the one of the next sample in the buffer
150 ffado_timestamp_t ts_head_tmp
;
151 m_data_buffer
->getBufferHeadTimestamp ( &ts_head_tmp
, &fc
); // thread safe
153 // the timestamp gives us the time at which we want the sample block
154 // to be output by the device
155 presentation_time
= ( uint64_t ) ts_head_tmp
;
157 // now we calculate the time when we have to transmit the sample block
158 transmit_at_time
= substractTicks ( presentation_time
, RME_TRANSMIT_TRANSFER_DELAY
);
160 // calculate the cycle this block should be presented in
161 // (this is just a virtual calculation since at that time it should
162 // already be in the device's buffer)
163 presentation_cycle
= ( unsigned int ) ( TICKS_TO_CYCLES ( presentation_time
) );
165 // calculate the cycle this block should be transmitted in
166 transmit_at_cycle
= ( unsigned int ) ( TICKS_TO_CYCLES ( transmit_at_time
) );
168 // we can check whether this cycle is within the 'window' we have
169 // to send this packet.
170 // first calculate the number of cycles left before presentation time
171 cycles_until_presentation
= diffCycles ( presentation_cycle
, cycle
);
173 // we can check whether this cycle is within the 'window' we have
174 // to send this packet.
175 // first calculate the number of cycles left before presentation time
176 cycles_until_transmit
= diffCycles ( transmit_at_cycle
, cycle
);
178 // two different options:
179 // 1) there are not enough frames for one packet
180 // => determine wether this is a problem, since we might still
181 // have some time to send it
182 // 2) there are enough packets
183 // => determine whether we have to send them in this packet
184 if ( fc
< ( signed int ) getNominalFramesPerPacket() )
186 // not enough frames in the buffer,
188 // we can still postpone the queueing of the packets
189 // if we are far enough ahead of the presentation time
190 if ( cycles_until_presentation
<= RME_MIN_CYCLES_BEFORE_PRESENTATION
)
192 debugOutput ( DEBUG_LEVEL_VERBOSE
,
193 "Insufficient frames (P): N=%02d, CY=%04u, TC=%04u, CUT=%04d\n",
194 fc
, cycle
, transmit_at_cycle
, cycles_until_transmit
);
200 debugOutput ( DEBUG_LEVEL_VERY_VERBOSE
,
201 "Insufficient frames (NP): N=%02d, CY=%04u, TC=%04u, CUT=%04d\n",
202 fc
, cycle
, transmit_at_cycle
, cycles_until_transmit
);
203 // there is still time left to send the packet
204 // we want the system to give this packet another go at a later time instant
210 // There are enough frames, so check the time they are intended for.
211 // All frames have a certain 'time window' in which they can be sent
212 // this corresponds to the range of the timestamp mechanism: we can
213 // send a packet 15 cycles in advance of the 'presentation time' in
214 // theory we can send the packet up till one cycle before the
215 // presentation time, however this is not very smart.
217 // There are 3 options:
218 // 1) the frame block is too early
219 // => send an empty packet
220 // 2) the frame block is within the window
222 // 3) the frame block is too late
223 // => discard (and raise xrun?)
224 // get next block of frames and repeat
226 if(cycles_until_transmit
< 0)
229 debugOutput(DEBUG_LEVEL_VERBOSE
,
230 "Too late: CY=%04u, TC=%04u, CUT=%04d, TSP=%011"PRIu64
" (%04u)\n",
232 transmit_at_cycle
, cycles_until_transmit
,
233 presentation_time
, (unsigned int)TICKS_TO_CYCLES(presentation_time
) );
235 // however, if we can send this sufficiently before the
236 // presentation time, it could be harmless.
237 // NOTE: dangerous since the device has no way of reporting that
238 // it didn't get this packet on time.
239 if(cycles_until_presentation
>= RME_MIN_CYCLES_BEFORE_PRESENTATION
)
241 // we are not that late and can still try to transmit the
243 *length
= n_events
*m_event_size
;
244 m_tx_dbc
+= fillDataPacketHeader((quadlet_t
*)data
, length
, presentation_time
);
245 m_last_timestamp
= presentation_time
;
250 else // definitely too late
255 else if(cycles_until_transmit
<= RME_MAX_CYCLES_TO_TRANSMIT_EARLY
)
257 // it's time send the packet
258 *length
= n_events
*m_event_size
;
259 m_tx_dbc
+= fillDataPacketHeader((quadlet_t
*)data
, length
, presentation_time
);
260 m_last_timestamp
= presentation_time
;
267 debugOutput ( DEBUG_LEVEL_VERY_VERBOSE
,
268 "Too early: CY=%04u, TC=%04u, CUT=%04d, TST=%011"PRIu64
" (%04u), TSP=%011"PRIu64
" (%04u)\n",
270 transmit_at_cycle
, cycles_until_transmit
,
271 transmit_at_time
, ( unsigned int ) TICKS_TO_CYCLES ( transmit_at_time
),
272 presentation_time
, ( unsigned int ) TICKS_TO_CYCLES ( presentation_time
) );
274 if ( cycles_until_transmit
> RME_MAX_CYCLES_TO_TRANSMIT_EARLY
+ 1 )
276 debugOutput ( DEBUG_LEVEL_VERY_VERBOSE
,
277 "Way too early: CY=%04u, TC=%04u, CUT=%04d, TST=%011"PRIu64
" (%04u), TSP=%011"PRIu64
" (%04u)\n",
279 transmit_at_cycle
, cycles_until_transmit
,
280 transmit_at_time
, ( unsigned int ) TICKS_TO_CYCLES ( transmit_at_time
),
281 presentation_time
, ( unsigned int ) TICKS_TO_CYCLES ( presentation_time
) );
284 // we are too early, send only an empty packet
285 return eCRV_EmptyPacket
;
291 enum StreamProcessor::eChildReturnValue
292 RmeTransmitStreamProcessor::generatePacketData (
293 unsigned char *data
, unsigned int *length
)
295 // Flag the successful start of streaming so generateEmptyPacketHeader()
296 // knows that true empty packets are now required.
299 // The number of events per packet expected by the RME is solely
300 // dependent on the current sample rate. An 'event' is one sample from
301 // all channels plus possibly other midi and control data.
302 signed n_events
= getNominalFramesPerPacket();
304 if (m_data_buffer
->readFrames(n_events
, (char *)(data
))) {
306 // 1 kHz tone into ch7 (phones L) for testing, but only if a debug
309 if (getDebugLevel() > 0) {
310 float ticks_per_frame
= m_Parent
.getDeviceManager().getStreamProcessorManager().getSyncSource().getTicksPerFrame();
311 signed int int_tpf
= lrintf(ticks_per_frame
);
312 quadlet_t
*sample
= (quadlet_t
*)data
+ 6;
314 for (i
=0; i
<n_events
; i
++, sample
+=m_event_size
/4) {
315 static signed int a_cx
= 0;
316 signed int val
= lrintf(0x7fffff*sin((1000.0*2.0*M_PI
/24576000.0)*a_cx
));
318 if ((a_cx
+=int_tpf
) >= 24576000) {
329 // FIXME: debugOutput() for initial testing only
330 debugOutput(DEBUG_LEVEL_VERBOSE
, "readFrames() failure\n");
331 // If there's an xrun, ensure the data doesn't contain junk just
332 // in case it gets sent to the interface.
333 memset(data
, 0, *length
);
338 enum StreamProcessor::eChildReturnValue
339 RmeTransmitStreamProcessor::generateEmptyPacketHeader (
340 unsigned char *data
, unsigned int *length
,
341 unsigned char *tag
, unsigned char *sy
,
344 debugOutput ( DEBUG_LEVEL_VERY_VERBOSE
, "XMIT EMPTY: CY=%04lu, TSP=%011"PRIu64
" (%04u)\n",
345 CYCLE_TIMER_GET_CYCLES(pkt_ctr
), m_last_timestamp
,
346 ( unsigned int ) TICKS_TO_CYCLES ( m_last_timestamp
) );
348 // Do housekeeping expected for all packets, even for packets containing
354 // During the dryRunning state used during startup FFADO will request
355 // "empty" packets. However, the fireface will only "start" (ie:
356 // sending data to the DACs and sending data from the ADCs)
357 // packets once it has received a certain amount of data from the PC.
358 // Since the receive stream processor won't register as dry running
359 // until data is received, we therefore need to send some data while in
360 // the initial states during startup in order to kick the process into
363 // Non-empty "empty" packets are not desired during the dry-running
364 // state encountered during any other stage (eg: during streaming or
365 // closedown), however, so take steps to ensure they are only sent
366 // during the startup sequence.
368 // Experimentation showed that it was necessary to send "silent" packets
369 // for the duration of the pre-streaming setup. Otherwise the FF800
370 // especially would give a burst of digital noise out of some or all
371 // outputs during the first FFADO streaming startup following the
372 // powering up of the interface (the duration of the burst was estimated
373 // to be of the order of 250 ms at 48 kHz sampling rate). Therefore if
374 // streaming has not yet run assume we're in the pre-streaming stages
375 // and send a silent data packet.
377 // While a single packet was sufficient to get data flowing from the
378 // fireface, the noise bursts would almost always occur in this case.
380 // Sending during the ePS_DryRunning seemed to reduce the probability
381 // of getting the bursts slightly.
383 // Sending during the ePS_DryRunning and ePS_WaitingForStreamEnable
384 // states was almost enough to prevent the noise burst: the bursts were
385 // far less frequent and when they did happen they were very short.
387 // Further notes about the noise burst:
388 // * it was not due to bad data from readFrames().
389 // * it seemed to be aligned to the time around the transition to
390 // the ePS_DryRunning state, although this was never explicitly
392 // * once streaming had been run once the bursts were very unlikely
393 // to occur on second and subsequent runs until the Fireface was
394 // powercycled. Bursts after the initial run were observed on very
395 // isolated occasions, but they could have been a side effect of
396 // testing at the time.
398 if (streaming_has_run
== 0) {
399 signed n_events
= getNominalFramesPerPacket();
401 streaming_has_dryrun
= 1;
403 streaming_start_count
+= n_events
;
404 *length
= n_events
* m_event_size
;
410 enum StreamProcessor::eChildReturnValue
411 RmeTransmitStreamProcessor::generateEmptyPacketData (
412 unsigned char *data
, unsigned int *length
)
414 /* If dry-running data is being sent, zero the data */
416 memset(data
, 0, *length
);
418 return eCRV_OK
; // no need to do anything
421 enum StreamProcessor::eChildReturnValue
422 RmeTransmitStreamProcessor::generateSilentPacketHeader (
423 unsigned char *data
, unsigned int *length
,
424 unsigned char *tag
, unsigned char *sy
,
427 unsigned int cycle
= CYCLE_TIMER_GET_CYCLES(pkt_ctr
);
429 debugOutput( DEBUG_LEVEL_VERY_VERBOSE
, "XMIT SILENT: CY=%04u, TSP=%011"PRIu64
" (%04u)\n",
430 cycle
, m_last_timestamp
,
431 ( unsigned int ) TICKS_TO_CYCLES ( m_last_timestamp
) );
433 // A "silent" packet is identical to a regular data packet except all
434 // audio data is set to zero. The requirements of the silent packet
435 // for RME devices is still being confirmed.
437 // The number of events per packet expected by the RME is solely
438 // dependent on the current sample rate. An 'event' is one sample from
439 // all channels plus possibly other midi and control data.
440 signed n_events
= getNominalFramesPerPacket();
442 // Do housekeeping expected for all packets, even for packets containing
446 /* Assume the packet will be empty unless proven otherwise */
449 uint64_t presentation_time
;
451 uint64_t transmit_at_time
;
452 unsigned int transmit_at_cycle
;
453 int cycles_until_transmit
;
455 /* The sample buffer is not necessarily running when silent packets are
456 * needed, so use m_last_timestamp (the timestamp of the previously sent
457 * data packet) as the basis for the presentation time of the next
458 * packet. Since we're only writing zeros we don't have to deal with
461 float ticks_per_frame
= m_Parent
.getDeviceManager().getStreamProcessorManager().getSyncSource().getTicksPerFrame();
462 presentation_time
= addTicks(m_last_timestamp
, (unsigned int)lrintf(n_events
* ticks_per_frame
));
464 transmit_at_time
= substractTicks(presentation_time
, RME_TRANSMIT_TRANSFER_DELAY
);
465 transmit_at_cycle
= (unsigned int)(TICKS_TO_CYCLES(transmit_at_time
));
466 // Not currently used for silent packets:
467 // unsigned int presentation_cycle = (unsigned int)(TICKS_TO_CYCLES(presentation_time));
468 // int cycles_until_presentation = diffCycles(presentation_cycle, cycle);
469 cycles_until_transmit
= diffCycles(transmit_at_cycle
, cycle
);
471 if (cycles_until_transmit
< 0)
473 // At this point we've theoretically missed the cycle at which
474 // the data needs to be transmitted. Technically, if
475 // cycles_until_presentation is greater than or equal to
476 // RME_MIN_CYCLES_BEFORE_PRESENTATION we have an xrun. However,
477 // since this is a silent packet there's no real harm in indicating
478 // that a silent packet can still be sent; it's not as if a silent
479 // packet consumes data. Furthermore, due to the fact that
480 // presentation time is estimated from m_last_timestamp it's possible
481 // that any xrun indication here is a false trigger.
483 // In any case, when silent packets are utilised during shutdown
484 // an eCRV_XRun return is "invalid" (see StreamProcessor, function
485 // getPacket(). Since silent packets are usually used during startup
486 // and/or shutdown there's little to be gained by flagging xruns; all
487 // they seem to do is prevent an otherwise clean shutdown.
488 m_last_timestamp
= presentation_time
;
489 m_tx_dbc
+= fillDataPacketHeader((quadlet_t
*)data
, length
, m_last_timestamp
);
494 else if (cycles_until_transmit
<= RME_MAX_CYCLES_TO_TRANSMIT_EARLY
)
496 m_last_timestamp
= presentation_time
;
497 m_tx_dbc
+= fillDataPacketHeader((quadlet_t
*)data
, length
, m_last_timestamp
);
504 return eCRV_EmptyPacket
;
509 enum StreamProcessor::eChildReturnValue
510 RmeTransmitStreamProcessor::generateSilentPacketData (
511 unsigned char *data
, unsigned int *length
)
513 // Simply set all audio data to zero since that's what's meant by
514 // a "silent" packet.
515 memset(data
, 0, *length
);
520 unsigned int RmeTransmitStreamProcessor::fillDataPacketHeader (
521 quadlet_t
*data
, unsigned int* length
,
524 // quadlet_t *quadlet = (quadlet_t *)data;
525 // Size of a single data frame in quadlets.
526 // unsigned dbs = m_event_size / 4;
528 // The number of events per packet expected by the RME is solely
529 // dependent on the current sample rate. An 'event' is one sample from
530 // all channels plus possibly other midi and control data.
531 signed n_events
= getNominalFramesPerPacket();
532 *length
= n_events
*m_event_size
;
537 unsigned int RmeTransmitStreamProcessor::fillNoDataPacketHeader (
538 quadlet_t
*data
, unsigned int* length
)
544 bool RmeTransmitStreamProcessor::prepareChild()
546 debugOutput ( DEBUG_LEVEL_VERBOSE
, "Preparing (%p)...\n", this );
547 m_max_fs_diff_norm
= 10.0;
548 m_max_diff_ticks
= 30720;
550 // Unsure whether this helps yet. Testing continues.
551 m_dll_bandwidth_hz
= 1.0; // 0.1;
556 * compose the event streams for the packets from the port buffers
558 bool RmeTransmitStreamProcessor::processWriteBlock(char *data
,
559 unsigned int nevents
, unsigned int offset
) {
560 bool no_problem
=true;
562 for ( PortVectorIterator it
= m_Ports
.begin();
565 // If this port is disabled, unconditionally send it silence.
566 if((*it
)->isDisabled()) {
567 if (encodeSilencePortToRmeEvents(static_cast<RmeAudioPort
*>(*it
), (quadlet_t
*)data
, offset
, nevents
)) {
568 debugWarning("Could not encode silence for disabled port %s to Rme events\n",(*it
)->getName().c_str());
569 // Don't treat this as a fatal error at this point
576 switch(port
->getPortType()) {
579 if (encodePortToRmeEvents(static_cast<RmeAudioPort
*>(*it
), (quadlet_t
*)data
, offset
, nevents
)) {
580 debugWarning("Could not encode port %s to Rme events\n",(*it
)->getName().c_str());
585 if (encodePortToRmeMidiEvents(static_cast<RmeMidiPort
*>(*it
), (quadlet_t
*)data
, offset
, nevents
)) {
586 debugWarning("Could not encode port %s to Midi events\n",(*it
)->getName().c_str());
598 RmeTransmitStreamProcessor::transmitSilenceBlock(char *data
,
599 unsigned int nevents
, unsigned int offset
) {
600 // This is the same as the non-silence version, except that is
601 // doesn't read from the port buffers.
602 bool no_problem
= true;
603 for ( PortVectorIterator it
= m_Ports
.begin();
608 switch(port
->getPortType()) {
611 if (encodeSilencePortToRmeEvents(static_cast<RmeAudioPort
*>(*it
), (quadlet_t
*)data
, offset
, nevents
)) {
612 debugWarning("Could not encode port %s to MBLA events\n",(*it
)->getName().c_str());
617 if (encodeSilencePortToRmeMidiEvents(static_cast<RmeMidiPort
*>(*it
), (quadlet_t
*)data
, offset
, nevents
)) {
618 debugWarning("Could not encode port %s to Midi events\n",(*it
)->getName().c_str());
629 int RmeTransmitStreamProcessor::encodePortToRmeEvents(RmeAudioPort
*p
, quadlet_t
*data
,
630 unsigned int offset
, unsigned int nevents
) {
631 // Encodes nevents worth of data from the given port into the given buffer. The
632 // format of the buffer is precisely that which will be sent to the RME.
634 // iterate over the ports
635 // * get port buffer address
636 // * loop over events
637 // - pick right sample in event based upon PortInfo
638 // - convert sample from Port format (E_Int24, E_Float, ..) to RME
641 // We include the ability to start the transfer from the given offset within
642 // the port (expressed in frames) so the 'efficient' transfer method can be
648 target
= data
+ p
->getPosition()/4;
650 switch(m_StreamProcessorManager
.getAudioDataType()) {
652 case StreamProcessorManager::eADT_Int24
:
654 quadlet_t
*buffer
=(quadlet_t
*)(p
->getBufferAddress());
656 assert(nevents
+ offset
<= p
->getBufferSize());
658 // Offset is in frames, but each port is only a single
659 // channel, so the number of frames is the same as the
660 // number of quadlets to offset (assuming the port buffer
661 // uses one quadlet per sample, which is the case currently).
664 for(j
= 0; j
< nevents
; j
+= 1) { // Decode nsamples
665 *target
= (*buffer
& 0x00ffffff) << 8;
667 target
+=m_event_size
/4;
671 case StreamProcessorManager::eADT_Float
:
673 const float multiplier
= (float)(0x7FFFFF);
674 float *buffer
=(float *)(p
->getBufferAddress());
676 assert(nevents
+ offset
<= p
->getBufferSize());
680 for(j
= 0; j
< nevents
; j
+= 1) { // decode max nsamples
683 if (unlikely(in
> 1.0)) in
= 1.0;
684 if (unlikely(in
< -1.0)) in
= -1.0;
686 unsigned int v
= lrintf(in
* multiplier
);
689 target
+=m_event_size
/4;
698 int RmeTransmitStreamProcessor::encodeSilencePortToRmeEvents(RmeAudioPort
*p
, quadlet_t
*data
,
699 unsigned int offset
, unsigned int nevents
) {
701 quadlet_t
*target
= data
+ p
->getPosition()/4;
703 switch (m_StreamProcessorManager
.getAudioDataType()) {
705 case StreamProcessorManager::eADT_Int24
:
706 case StreamProcessorManager::eADT_Float
:
707 for (j
= 0; j
< nevents
; j
++) {
709 target
+= m_event_size
/4;
717 int RmeTransmitStreamProcessor::encodePortToRmeMidiEvents(
718 RmeMidiPort
*p
, quadlet_t
*data
,
719 unsigned int offset
, unsigned int nevents
) {
722 quadlet_t
*src
= (quadlet_t
*)p
->getBufferAddress();
724 unsigned char *target
= (unsigned char *)data
+ p
->getPosition();
726 // Send a MIDI byte if there is one to send. RME MIDI data is sent using
727 // a 3-byte sequence within a frame starting at the port's position.
728 // A non-zero MSB indicates there is MIDI data to send.
730 for (j
=0; j
<nevents
; j
++, src
++, target
+=m_event_size
) {
734 /* FFADO's MIDI subsystem dictates that at the most there will be one
735 * MIDI byte every 8th's sample, making a MIDI byte "unlikely".
737 if (unlikely(*src
& 0xff000000)) {
738 /* A MIDI byte is ready to send - buffer it */
739 midibuffer
[mb_head
++] = *src
;
740 mb_head
&= MIDIBUFFER_SIZE
-1;
741 if (unlikely(mb_head
== mb_tail
)) {
742 /* Buffer overflow - dump oldest byte. */
743 /* Ideally this would dump an entire MIDI message, but this is only
744 * feasible if it's possible to determine the message size easily.
746 mb_tail
= (mb_tail
+1) & (MIDIBUFFER_SIZE
-1);
747 debugWarning("RME MIDI buffer overflow\n");
749 debugOutput(DEBUG_LEVEL_VERY_VERBOSE
,"Buffered MIDI byte %d\n", *src
& 0xff);
752 /* Send the MIDI byte at the tail of the buffer if enough time has elapsed
753 * since the last MIDI byte was sent. For most iterations through the loop
754 * this condition will be false.
756 if (unlikely(mb_head
!=mb_tail
&& !midi_lock
)) {
759 *(target
+2) = midibuffer
[mb_tail
] & 0xff;
760 debugOutput(DEBUG_LEVEL_VERY_VERBOSE
,"Sent MIDI byte %d (j=%d)\n", midibuffer
[mb_tail
], j
);
761 mb_tail
= (mb_tail
+1) & (MIDIBUFFER_SIZE
-1);
762 midi_lock
= midi_tx_period
;
769 int RmeTransmitStreamProcessor::encodeSilencePortToRmeMidiEvents(
770 RmeMidiPort
*p
, quadlet_t
*data
,
771 unsigned int offset
, unsigned int nevents
) {
774 unsigned char *target
= (unsigned char *)data
+ p
->getPosition();
776 // For now, a "silent" MIDI event contains nothing but zeroes. This
777 // may have to change if we find this isn't for some reason appropriate.
778 for (j
=0; j
<nevents
; j
++, target
+=m_event_size
) {
779 memset(target
, 0, 3);
785 } // end of namespace Streaming