root/branches/ppalmers-streaming/src/libstreaming/motu/MotuTransmitStreamProcessor.cpp

Revision 733, 25.3 kB (checked in by ppalmers, 13 years ago)

adapt motu code to new SP base class (compiles, needs real testing)

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