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

Revision 750, 25.4 kB (checked in by ppalmers, 13 years ago)

Code refactoring. Tries to simplify things and tries to put all code where it belongs.

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