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

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

- Remove some obsolete support files and dirs

- Clean up the license statements in the source files. Everything is

GPL version 3 now.

- Add license and copyright notices to scons scripts

- Clean up some other text files

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