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

Revision 798, 25.2 kB (checked in by ppalmers, 14 years ago)

cleanup and reliability fixes

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