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

Revision 796, 25.2 kB (checked in by ppalmers, 13 years ago)

- move #define constants to config.h.in
- switch receive handler over to packet-per-buffer mode to improve latency performance

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