root/trunk/libffado/src/libstreaming/motu/MotuTransmitStreamProcessor.cpp

Revision 905, 24.3 kB (checked in by ppalmers, 16 years ago)

convert some const values into defines

Line 
1 /*
2  * Copyright (C) 2005-2008 by Jonathan Woithe
3  * Copyright (C) 2005-2008 by Pieter Palmers
4  *
5  * This file is part of FFADO
6  * FFADO = Free Firewire (pro-)audio drivers for linux
7  *
8  * FFADO is based upon FreeBoB.
9  *
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.
14  *
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.
19  *
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/>.
22  *
23  */
24
25 #include "config.h"
26
27 #include "MotuTransmitStreamProcessor.h"
28 #include "MotuPort.h"
29 #include "../StreamProcessorManager.h"
30 #include "devicemanager.h"
31
32 #include "libieee1394/ieee1394service.h"
33 #include "libieee1394/IsoHandlerManager.h"
34 #include "libieee1394/cycletimer.h"
35
36 #include <netinet/in.h>
37 #include <assert.h>
38
39 // Set to 1 to enable the generation of a 1 kHz test tone in analog output 1
40 #define TESTTONE 1
41
42 #if TESTTONE
43 #include <math.h>
44 #endif
45
46 namespace Streaming
47 {
48
49 // A macro to extract specific bits from a native endian quadlet
50 #define get_bits(_d,_start,_len) (((_d)>>((_start)-(_len)+1)) & ((1<<(_len))-1))
51
52 // Convert a full timestamp into an SPH timestamp as required by the MOTU
53 static inline uint32_t fullTicksToSph(int64_t timestamp) {
54     return TICKS_TO_CYCLE_TIMER(timestamp) & 0x1ffffff;
55 }
56
57 /* transmit */
58 MotuTransmitStreamProcessor::MotuTransmitStreamProcessor(FFADODevice &parent, unsigned int event_size )
59         : StreamProcessor(parent, ePT_Transmit )
60         , m_event_size( event_size )
61         , m_tx_dbc( 0 )
62 {}
63
64 unsigned int
65 MotuTransmitStreamProcessor::getMaxPacketSize() {
66     int framerate = m_Parent.getDeviceManager().getStreamProcessorManager().getNominalRate();
67     return framerate<=48000?616:(framerate<=96000?1032:1160);
68 }
69
70 unsigned int
71 MotuTransmitStreamProcessor::getNominalFramesPerPacket() {
72     int framerate = m_Parent.getDeviceManager().getStreamProcessorManager().getNominalRate();
73     return framerate<=48000?8:(framerate<=96000?16:32);
74 }
75
76 enum StreamProcessor::eChildReturnValue
77 MotuTransmitStreamProcessor::generatePacketHeader (
78     unsigned char *data, unsigned int *length,
79     unsigned char *tag, unsigned char *sy,
80     int cycle, unsigned int dropped, unsigned int max_length )
81 {
82     // The number of events per packet expected by the MOTU is solely
83     // dependent on the current sample rate.  An 'event' is one sample from
84     // all channels plus possibly other midi and control data.
85     signed n_events = getNominalFramesPerPacket();
86
87     // Do housekeeping expected for all packets sent to the MOTU, even
88     // for packets containing no audio data.
89     *sy = 0x00;
90     *tag = 1;      // All MOTU packets have a CIP-like header
91     *length = n_events*m_event_size + 8;
92
93     signed int fc;
94     uint64_t presentation_time;
95     unsigned int presentation_cycle;
96     int cycles_until_presentation;
97
98     uint64_t transmit_at_time;
99     unsigned int transmit_at_cycle;
100     int cycles_until_transmit;
101
102     debugOutput ( DEBUG_LEVEL_ULTRA_VERBOSE, "Try for cycle %d\n", cycle );
103     // check whether the packet buffer has packets for us to send.
104     // the base timestamp is the one of the next sample in the buffer
105     ffado_timestamp_t ts_head_tmp;
106     m_data_buffer->getBufferHeadTimestamp ( &ts_head_tmp, &fc ); // thread safe
107
108     // the timestamp gives us the time at which we want the sample block
109     // to be output by the device
110     presentation_time = ( uint64_t ) ts_head_tmp;
111     m_last_timestamp = presentation_time;
112
113     // now we calculate the time when we have to transmit the sample block
114     transmit_at_time = substractTicks ( presentation_time, MOTU_TRANSMIT_TRANSFER_DELAY );
115
116     // calculate the cycle this block should be presented in
117     // (this is just a virtual calculation since at that time it should
118     //  already be in the device's buffer)
119     presentation_cycle = ( unsigned int ) ( TICKS_TO_CYCLES ( presentation_time ) );
120
121     // calculate the cycle this block should be transmitted in
122     transmit_at_cycle = ( unsigned int ) ( TICKS_TO_CYCLES ( transmit_at_time ) );
123
124     // we can check whether this cycle is within the 'window' we have
125     // to send this packet.
126     // first calculate the number of cycles left before presentation time
127     cycles_until_presentation = diffCycles ( presentation_cycle, cycle );
128
129     // we can check whether this cycle is within the 'window' we have
130     // to send this packet.
131     // first calculate the number of cycles left before presentation time
132     cycles_until_transmit = diffCycles ( transmit_at_cycle, cycle );
133
134     if (dropped) {
135         debugOutput ( DEBUG_LEVEL_VERBOSE,
136                     "Gen HDR: CY=%04u, TC=%04u, CUT=%04d, TST=%011llu (%04u), TSP=%011llu (%04u)\n",
137                     cycle,
138                     transmit_at_cycle, cycles_until_transmit,
139                     transmit_at_time, ( unsigned int ) TICKS_TO_CYCLES ( transmit_at_time ),
140                     presentation_time, ( unsigned int ) TICKS_TO_CYCLES ( presentation_time ) );
141     }
142     // two different options:
143     // 1) there are not enough frames for one packet
144     //      => determine wether this is a problem, since we might still
145     //         have some time to send it
146     // 2) there are enough packets
147     //      => determine whether we have to send them in this packet
148     if ( fc < ( signed int ) getNominalFramesPerPacket() )
149     {
150         // not enough frames in the buffer,
151
152         // we can still postpone the queueing of the packets
153         // if we are far enough ahead of the presentation time
154         if ( cycles_until_presentation <= MOTU_MIN_CYCLES_BEFORE_PRESENTATION )
155         {
156             debugOutput ( DEBUG_LEVEL_VERBOSE,
157                         "Insufficient frames (P): N=%02d, CY=%04u, TC=%04u, CUT=%04d\n",
158                         fc, cycle, transmit_at_cycle, cycles_until_transmit );
159             // we are too late
160             return eCRV_XRun;
161         }
162         else
163         {
164             debugOutput ( DEBUG_LEVEL_VERY_VERBOSE,
165                         "Insufficient frames (NP): N=%02d, CY=%04u, TC=%04u, CUT=%04d\n",
166                         fc, cycle, transmit_at_cycle, cycles_until_transmit );
167             // there is still time left to send the packet
168             // we want the system to give this packet another go at a later time instant
169             return eCRV_Again;
170         }
171     }
172     else
173     {
174         // there are enough frames, so check the time they are intended for
175         // all frames have a certain 'time window' in which they can be sent
176         // this corresponds to the range of the timestamp mechanism:
177         // we can send a packet 15 cycles in advance of the 'presentation time'
178         // in theory we can send the packet up till one cycle before the presentation time,
179         // however this is not very smart.
180
181         // There are 3 options:
182         // 1) the frame block is too early
183         //      => send an empty packet
184         // 2) the frame block is within the window
185         //      => send it
186         // 3) the frame block is too late
187         //      => discard (and raise xrun?)
188         //         get next block of frames and repeat
189
190         if(cycles_until_transmit < 0)
191         {
192             // we are too late
193             debugOutput(DEBUG_LEVEL_VERBOSE,
194                         "Too late: CY=%04u, TC=%04u, CUT=%04d, TSP=%011llu (%04u)\n",
195                         cycle,
196                         transmit_at_cycle, cycles_until_transmit,
197                         presentation_time, (unsigned int)TICKS_TO_CYCLES(presentation_time) );
198
199             // however, if we can send this sufficiently before the presentation
200             // time, it could be harmless.
201             // NOTE: dangerous since the device has no way of reporting that it didn't get
202             //       this packet on time.
203             if(cycles_until_presentation >= MOTU_MIN_CYCLES_BEFORE_PRESENTATION)
204             {
205                 // we are not that late and can still try to transmit the packet
206                 m_tx_dbc += fillDataPacketHeader((quadlet_t *)data, length, m_last_timestamp);
207                 if (m_tx_dbc > 0xff)
208                     m_tx_dbc -= 0x100;
209                 return eCRV_Packet;
210             }
211             else   // definitely too late
212             {
213                 return eCRV_XRun;
214             }
215         }
216         else if(cycles_until_transmit <= MOTU_MAX_CYCLES_TO_TRANSMIT_EARLY)
217         {
218             // it's time send the packet
219             m_tx_dbc += fillDataPacketHeader((quadlet_t *)data, length, m_last_timestamp);
220             if (m_tx_dbc > 0xff)
221                 m_tx_dbc -= 0x100;
222             return eCRV_Packet;
223         }
224         else
225         {
226             debugOutput ( DEBUG_LEVEL_VERY_VERBOSE,
227                         "Too early: CY=%04u, TC=%04u, CUT=%04d, TST=%011llu (%04u), TSP=%011llu (%04u)\n",
228                         cycle,
229                         transmit_at_cycle, cycles_until_transmit,
230                         transmit_at_time, ( unsigned int ) TICKS_TO_CYCLES ( transmit_at_time ),
231                         presentation_time, ( unsigned int ) TICKS_TO_CYCLES ( presentation_time ) );
232 #ifdef DEBUG
233             if ( cycles_until_transmit > MOTU_MAX_CYCLES_TO_TRANSMIT_EARLY + 1 )
234             {
235                 debugOutput ( DEBUG_LEVEL_VERY_VERBOSE,
236                             "Way too early: CY=%04u, TC=%04u, CUT=%04d, TST=%011llu (%04u), TSP=%011llu (%04u)\n",
237                             cycle,
238                             transmit_at_cycle, cycles_until_transmit,
239                             transmit_at_time, ( unsigned int ) TICKS_TO_CYCLES ( transmit_at_time ),
240                             presentation_time, ( unsigned int ) TICKS_TO_CYCLES ( presentation_time ) );
241             }
242 #endif
243             // we are too early, send only an empty packet
244             return eCRV_EmptyPacket;
245         }
246     }
247     return eCRV_Invalid;
248 }
249
250 enum StreamProcessor::eChildReturnValue
251 MotuTransmitStreamProcessor::generatePacketData (
252     unsigned char *data, unsigned int *length,
253     unsigned char *tag, unsigned char *sy,
254     int cycle, unsigned int dropped, unsigned int max_length )
255 {
256     quadlet_t *quadlet = (quadlet_t *)data;
257     quadlet += 2; // skip the header
258     // Size of a single data frame in quadlets
259     unsigned dbs = m_event_size / 4;
260
261     // The number of events per packet expected by the MOTU is solely
262     // dependent on the current sample rate.  An 'event' is one sample from
263     // all channels plus possibly other midi and control data.
264     signed n_events = getNominalFramesPerPacket();
265
266     if (m_data_buffer->readFrames(n_events, (char *)(data + 8))) {
267         float ticks_per_frame = m_Parent.getDeviceManager().getStreamProcessorManager().getSyncSource().getActualRate();
268
269 #if TESTTONE
270         // FIXME: remove this hacked in 1 kHz test signal to
271         // analog-1 when testing is complete.
272         signed int i, int_tpf = (int)ticks_per_frame;
273         unsigned char *sample = data+8+16;
274         for (i=0; i<n_events; i++, sample+=m_event_size) {
275             static signed int a_cx = 0;
276             // Each sample is 3 bytes with MSB in lowest address (ie:
277             // network byte order).  After byte order swap, the 24-bit
278             // MSB is in the second byte of val.
279             signed int val = htonl((int)(0x7fffff*sin((1000.0*2.0*M_PI/24576000.0)*a_cx)));
280             memcpy(sample,((char *)&val)+1,3);
281             if ((a_cx+=int_tpf) >= 24576000) {
282                 a_cx -= 24576000;
283             }
284         }
285 #endif
286
287         // Set up each frames's SPH.
288         for (int i=0; i < n_events; i++, quadlet += dbs) {
289 //FIXME: not sure which is best for the MOTU.  Should be consistent with generateSilentPacketData().
290 //            int64_t ts_frame = addTicks(ts, (unsigned int)(i * ticks_per_frame));
291             int64_t ts_frame = addTicks(m_last_timestamp, (unsigned int)(i * ticks_per_frame));
292             *quadlet = htonl(fullTicksToSph(ts_frame));
293         }
294
295         return eCRV_OK;
296     }
297     else return eCRV_XRun;
298
299 }
300
301 enum StreamProcessor::eChildReturnValue
302 MotuTransmitStreamProcessor::generateEmptyPacketHeader (
303     unsigned char *data, unsigned int *length,
304     unsigned char *tag, unsigned char *sy,
305     int cycle, unsigned int dropped, unsigned int max_length )
306 {
307     debugOutput ( DEBUG_LEVEL_VERY_VERBOSE, "XMIT EMPTY: CY=%04u, TSP=%011llu (%04u)\n",
308                 cycle, m_last_timestamp, ( unsigned int ) TICKS_TO_CYCLES ( m_last_timestamp ) );
309
310     // Do housekeeping expected for all packets sent to the MOTU, even
311     // for packets containing no audio data.
312     *sy = 0x00;
313     *tag = 1;      // All MOTU packets have a CIP-like header
314     *length = 8;
315
316     m_tx_dbc += fillNoDataPacketHeader ( (quadlet_t *)data, length );
317     return eCRV_OK;
318 }
319
320 enum StreamProcessor::eChildReturnValue
321 MotuTransmitStreamProcessor::generateEmptyPacketData (
322     unsigned char *data, unsigned int *length,
323     unsigned char *tag, unsigned char *sy,
324     int cycle, unsigned int dropped, unsigned int max_length )
325 {
326     return eCRV_OK; // no need to do anything
327 }
328
329 enum StreamProcessor::eChildReturnValue
330 MotuTransmitStreamProcessor::generateSilentPacketHeader (
331     unsigned char *data, unsigned int *length,
332     unsigned char *tag, unsigned char *sy,
333     int cycle, unsigned int dropped, unsigned int max_length )
334 {
335     debugOutput ( DEBUG_LEVEL_VERY_VERBOSE, "XMIT SILENT: CY=%04u, TSP=%011llu (%04u)\n",
336                 cycle, m_last_timestamp, ( unsigned int ) TICKS_TO_CYCLES ( m_last_timestamp ) );
337
338     // A "silent" packet is identical to a regular data packet except
339     // all audio data is set to zero.  Therefore we can just use
340     // generatePacketHeader() to do the work here.
341     return generatePacketHeader(data, length, tag, sy, cycle, dropped, max_length);
342 }
343
344 enum StreamProcessor::eChildReturnValue
345 MotuTransmitStreamProcessor::generateSilentPacketData (
346     unsigned char *data, unsigned int *length,
347     unsigned char *tag, unsigned char *sy,
348     int cycle, unsigned int dropped, unsigned int max_length )
349 {
350     // Simply set all audio data to zero since that's what's meant by
351     // a "silent" packet.  Note that m_event_size is in bytes for MOTU.
352
353     quadlet_t *quadlet = (quadlet_t *)data;
354     quadlet += 2; // skip the header
355     // Size of a single data frame in quadlets
356     unsigned dbs = m_event_size / 4;
357
358     // The number of events per packet expected by the MOTU is solely
359     // dependent on the current sample rate.  An 'event' is one sample from
360     // all channels plus possibly other midi and control data.
361     signed n_events = getNominalFramesPerPacket();
362
363     memset(quadlet, 0, n_events*m_event_size);
364     float ticks_per_frame = m_Parent.getDeviceManager().getStreamProcessorManager().getSyncSource().getActualRate();
365
366     // Set up each frames's SPH.
367     for (int i=0; i < n_events; i++, quadlet += dbs) {
368 //FIXME: not sure which is best for the MOTU.  Should be consistent with generatePacketData().
369 //        int64_t ts_frame = addTicks(ts, (unsigned int)(i * ticks_per_frame));
370         int64_t ts_frame = addTicks(m_last_timestamp, (unsigned int)(i * ticks_per_frame));
371         *quadlet = htonl(fullTicksToSph(ts_frame));
372     }
373
374     return eCRV_OK;
375 }
376
377 unsigned int MotuTransmitStreamProcessor::fillDataPacketHeader (
378     quadlet_t *data, unsigned int* length,
379     uint32_t ts )
380 {
381     quadlet_t *quadlet = (quadlet_t *)data;
382     // Size of a single data frame in quadlets
383     unsigned dbs = m_event_size / 4;
384
385     // The number of events per packet expected by the MOTU is solely
386     // dependent on the current sample rate.  An 'event' is one sample from
387     // all channels plus possibly other midi and control data.
388     signed n_events = getNominalFramesPerPacket();
389
390     // construct the packet CIP-like header.  Even if this is a data-less
391     // packet the dbs field is still set as if there were data blocks
392     // present.  For data-less packets the dbc is the same as the previously
393     // transmitted block.
394     *quadlet = htonl(0x00000400 | ((m_Parent.get1394Service().getLocalNodeId()&0x3f)<<24) | m_tx_dbc | (dbs<<16));
395     quadlet++;
396     *quadlet = htonl(0x8222ffff);
397     quadlet++;
398     return n_events;
399 }
400
401 unsigned int MotuTransmitStreamProcessor::fillNoDataPacketHeader (
402     quadlet_t *data, unsigned int* length )
403 {
404     quadlet_t *quadlet = (quadlet_t *)data;
405     // Size of a single data frame in quadlets
406     unsigned dbs = m_event_size / 4;
407     // construct the packet CIP-like header.  Even if this is a data-less
408     // packet the dbs field is still set as if there were data blocks
409     // present.  For data-less packets the dbc is the same as the previously
410     // transmitted block.
411     *quadlet = htonl(0x00000400 | ((m_Parent.get1394Service().getLocalNodeId()&0x3f)<<24) | m_tx_dbc | (dbs<<16));
412     quadlet++;
413     *quadlet = htonl(0x8222ffff);
414     quadlet++;
415     *length = 8;
416     return 0;
417 }
418
419 bool MotuTransmitStreamProcessor::prepareChild()
420 {
421     debugOutput ( DEBUG_LEVEL_VERBOSE, "Preparing (%p)...\n", this );
422     return true;
423 }
424
425 /*
426 * compose the event streams for the packets from the port buffers
427 */
428 bool MotuTransmitStreamProcessor::processWriteBlock(char *data,
429                        unsigned int nevents, unsigned int offset) {
430     bool no_problem=true;
431     unsigned int i;
432
433     // FIXME: ensure the MIDI and control streams are all zeroed until
434     // such time as they are fully implemented.
435     for (i=0; i<nevents; i++) {
436         memset(data+4+i*m_event_size, 0x00, 6);
437     }
438
439     for ( PortVectorIterator it = m_Ports.begin();
440       it != m_Ports.end();
441       ++it ) {
442         // If this port is disabled, don't process it
443         if((*it)->isDisabled()) {continue;};
444
445         Port *port=(*it);
446
447         switch(port->getPortType()) {
448
449         case Port::E_Audio:
450             if (encodePortToMotuEvents(static_cast<MotuAudioPort *>(*it), (quadlet_t *)data, offset, nevents)) {
451                 debugWarning("Could not encode port %s to Motu events",(*it)->getName().c_str());
452                 no_problem=false;
453             }
454             break;
455         case Port::E_Midi:
456              if (encodePortToMotuMidiEvents(static_cast<MotuMidiPort *>(*it), (quadlet_t *)data, offset, nevents)) {
457                  debugWarning("Could not encode port %s to Midi events",(*it)->getName().c_str());
458                  no_problem=false;
459              }
460             break;
461         default: // ignore
462             break;
463         }
464     }
465     return no_problem;
466 }
467
468 bool
469 MotuTransmitStreamProcessor::transmitSilenceBlock(char *data,
470                        unsigned int nevents, unsigned int offset) {
471     // This is the same as the non-silence version, except that is
472     // doesn't read from the port buffers.
473     bool no_problem = true;
474     for ( PortVectorIterator it = m_Ports.begin();
475       it != m_Ports.end();
476       ++it ) {
477         Port *port=(*it);
478
479         switch(port->getPortType()) {
480
481         case Port::E_Audio:
482             if (encodeSilencePortToMotuEvents(static_cast<MotuAudioPort *>(*it), (quadlet_t *)data, offset, nevents)) {
483                 debugWarning("Could not encode port %s to MBLA events",(*it)->getName().c_str());
484                 no_problem = false;
485             }
486             break;
487         case Port::E_Midi:
488             if (encodeSilencePortToMotuMidiEvents(static_cast<MotuMidiPort *>(*it), (quadlet_t *)data, offset, nevents)) {
489                 debugWarning("Could not encode port %s to Midi events",(*it)->getName().c_str());
490                 no_problem = false;
491             }
492             break;
493         default: // ignore
494             break;
495         }
496     }
497     return no_problem;
498 }
499
500 int MotuTransmitStreamProcessor::encodePortToMotuEvents(MotuAudioPort *p, quadlet_t *data,
501                        unsigned int offset, unsigned int nevents) {
502 // Encodes nevents worth of data from the given port into the given buffer.  The
503 // format of the buffer is precisely that which will be sent to the MOTU.
504 // The basic idea:
505 //   iterate over the ports
506 //     * get port buffer address
507 //     * loop over events
508 //         - pick right sample in event based upon PortInfo
509 //         - convert sample from Port format (E_Int24, E_Float, ..) to MOTU
510 //           native format
511 //
512 // We include the ability to start the transfer from the given offset within
513 // the port (expressed in frames) so the 'efficient' transfer method can be
514 // utilised.
515
516     unsigned int j=0;
517
518     // Use char here since the target address won't necessarily be
519     // aligned; use of an unaligned quadlet_t may cause issues on certain
520     // architectures.  Besides, the target (data going directly to the MOTU)
521     // isn't structured in quadlets anyway; it mainly consists of packed
522     // 24-bit integers.
523     unsigned char *target;
524     target = (unsigned char *)data + p->getPosition();
525
526     switch(m_StreamProcessorManager.getAudioDataType()) {
527         default:
528         case StreamProcessorManager::eADT_Int24:
529             {
530                 quadlet_t *buffer=(quadlet_t *)(p->getBufferAddress());
531
532                 assert(nevents + offset <= p->getBufferSize());
533
534                 // Offset is in frames, but each port is only a single
535                 // channel, so the number of frames is the same as the
536                 // number of quadlets to offset (assuming the port buffer
537                 // uses one quadlet per sample, which is the case currently).
538                 buffer+=offset;
539
540                 for(j = 0; j < nevents; j += 1) { // Decode nsamples
541                     *target = (*buffer >> 16) & 0xff;
542                     *(target+1) = (*buffer >> 8) & 0xff;
543                     *(target+2) = (*buffer) & 0xff;
544
545                     buffer++;
546                     target+=m_event_size;
547                 }
548             }
549             break;
550         case StreamProcessorManager::eADT_Float:
551             {
552                 const float multiplier = (float)(0x7FFFFF);
553                 float *buffer=(float *)(p->getBufferAddress());
554
555                 assert(nevents + offset <= p->getBufferSize());
556
557                 buffer+=offset;
558
559                 for(j = 0; j < nevents; j += 1) { // decode max nsamples
560                     unsigned int v = (int)(*buffer * multiplier);
561                     *target = (v >> 16) & 0xff;
562                     *(target+1) = (v >> 8) & 0xff;
563                     *(target+2) = v & 0xff;
564
565                     buffer++;
566                     target+=m_event_size;
567                 }
568             }
569             break;
570     }
571
572     return 0;
573 }
574
575 int MotuTransmitStreamProcessor::encodeSilencePortToMotuEvents(MotuAudioPort *p, quadlet_t *data,
576                        unsigned int offset, unsigned int nevents) {
577     unsigned int j=0;
578     unsigned char *target = (unsigned char *)data + p->getPosition();
579
580     switch (m_StreamProcessorManager.getAudioDataType()) {
581     default:
582         case StreamProcessorManager::eADT_Int24:
583         case StreamProcessorManager::eADT_Float:
584         for (j = 0; j < nevents; j++) {
585             *target = *(target+1) = *(target+2) = 0;
586             target += m_event_size;
587         }
588         break;
589     }
590
591     return 0;
592 }
593
594 int MotuTransmitStreamProcessor::encodePortToMotuMidiEvents(
595                        MotuMidiPort *p, quadlet_t *data,
596                        unsigned int offset, unsigned int nevents) {
597
598     unsigned int j;
599     quadlet_t *src = (quadlet_t *)p->getBufferAddress();
600     src += offset;
601
602     unsigned char *target = (unsigned char *)data + p->getPosition();
603
604     // Send a MIDI byte if there is one to send.  MOTU MIDI data is sent using
605     // a 3-byte sequence within a frame starting at the port's position.  For
606     // now we assume that a zero within the port's buffer means there is no
607     // MIDI data for the corresponding frame, but this may need refining.
608
609     for (j=0; j<nevents; j++, src++, target+=m_event_size) {
610         if (*src != 0) {
611             *(target) = 0x01;
612             *(target+1) = 0x00;
613             *(target+2) = (*src & 0xff);
614         } else
615           memset(target, 0, 3);
616     }
617
618     return 0;
619 }
620
621 int MotuTransmitStreamProcessor::encodeSilencePortToMotuMidiEvents(
622                        MotuMidiPort *p, quadlet_t *data,
623                        unsigned int offset, unsigned int nevents) {
624
625     unsigned int j;
626     unsigned char *target = (unsigned char *)data + p->getPosition();
627
628     // For now, a "silent" MIDI event contains nothing but zeroes.  This
629     // may have to change if we find this isn't for some reason appropriate.
630     for (j=0; j<nevents; j++, target+=m_event_size) {
631        memset(target, 0, 3);
632     }
633
634     return 0;
635 }
636
637 } // end of namespace Streaming
Note: See TracBrowser for help on using the browser.