root/trunk/libffado/src/libstreaming/amdtp/AmdtpTransmitStreamProcessor.cpp

Revision 776, 24.0 kB (checked in by ppalmers, 13 years ago)

try to fix deadlock / performace issues

Line 
1 /*
2  * Copyright (C) 2005-2007 by Pieter Palmers
3  *
4  * This file is part of FFADO
5  * FFADO = Free Firewire (pro-)audio drivers for linux
6  *
7  * FFADO is based upon FreeBoB.
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  */
23
24 #include "AmdtpTransmitStreamProcessor.h"
25 #include "AmdtpPort.h"
26 #include "../StreamProcessorManager.h"
27 #include "devicemanager.h"
28
29 #include "libieee1394/ieee1394service.h"
30 #include "libieee1394/IsoHandlerManager.h"
31 #include "libieee1394/cycletimer.h"
32
33 #include <netinet/in.h>
34 #include <assert.h>
35
36 // in ticks
37 // as per AMDTP2.1:
38 // 354.17us + 125us @ 24.576ticks/usec = 11776.08192 ticks
39 #define DEFAULT_TRANSFER_DELAY (11776U)
40
41 #define TRANSMIT_TRANSFER_DELAY DEFAULT_TRANSFER_DELAY
42
43 namespace Streaming
44 {
45
46 /* transmit */
47 AmdtpTransmitStreamProcessor::AmdtpTransmitStreamProcessor(FFADODevice &parent, int dimension)
48         : StreamProcessor(parent, ePT_Transmit)
49         , m_dimension( dimension )
50         , m_dbc( 0 )
51 {}
52
53 enum StreamProcessor::eChildReturnValue
54 AmdtpTransmitStreamProcessor::generatePacketHeader (
55     unsigned char *data, unsigned int *length,
56     unsigned char *tag, unsigned char *sy,
57     int cycle, unsigned int dropped, unsigned int max_length )
58 {
59     struct iec61883_packet *packet = ( struct iec61883_packet * ) data;
60     /* Our node ID can change after a bus reset, so it is best to fetch
61     * our node ID for each packet. */
62     packet->sid = m_1394service.getLocalNodeId() & 0x3f;
63
64     packet->dbs = m_dimension;
65     packet->fn = 0;
66     packet->qpc = 0;
67     packet->sph = 0;
68     packet->reserved = 0;
69     packet->dbc = m_dbc;
70     packet->eoh1 = 2;
71     packet->fmt = IEC61883_FMT_AMDTP;
72
73     *tag = IEC61883_TAG_WITH_CIP;
74     *sy = 0;
75
76     signed int fc;
77     uint64_t presentation_time;
78     unsigned int presentation_cycle;
79     int cycles_until_presentation;
80
81     uint64_t transmit_at_time;
82     unsigned int transmit_at_cycle;
83     int cycles_until_transmit;
84
85     // FIXME: should become a define
86     // the absolute minimum number of cycles we want to transmit
87     // a packet ahead of the presentation time. The nominal time
88     // the packet is transmitted ahead of the presentation time is
89     // given by TRANSMIT_TRANSFER_DELAY (in ticks), but in case we
90     // are too late for that, this constant defines how late we can
91     // be.
92     const int min_cycles_before_presentation = 1;
93     // FIXME: should become a define
94     // the absolute maximum number of cycles we want to transmit
95     // a packet ahead of the ideal transmit time. The nominal time
96     // the packet is transmitted ahead of the presentation time is
97     // given by TRANSMIT_TRANSFER_DELAY (in ticks), but we can send
98     // packets early if we want to. (not completely according to spec)
99     const int max_cycles_to_transmit_early = 2;
100
101 try_block_of_frames:
102     debugOutput ( DEBUG_LEVEL_ULTRA_VERBOSE, "Try for cycle %d\n", cycle );
103     // check whether the packet buffer has packets for us to send.
104     // the base timestamp is the one of the next sample in the buffer
105     ffado_timestamp_t ts_head_tmp;
106     m_data_buffer->getBufferHeadTimestamp ( &ts_head_tmp, &fc ); // thread safe
107
108     // the timestamp gives us the time at which we want the sample block
109     // to be output by the device
110     presentation_time = ( uint64_t ) ts_head_tmp;
111     m_last_timestamp = presentation_time;
112
113     // now we calculate the time when we have to transmit the sample block
114     transmit_at_time = substractTicks ( presentation_time, TRANSMIT_TRANSFER_DELAY );
115
116     // calculate the cycle this block should be presented in
117     // (this is just a virtual calculation since at that time it should
118     //  already be in the device's buffer)
119     presentation_cycle = ( unsigned int ) ( TICKS_TO_CYCLES ( presentation_time ) );
120
121     // calculate the cycle this block should be transmitted in
122     transmit_at_cycle = ( unsigned int ) ( TICKS_TO_CYCLES ( transmit_at_time ) );
123
124     // we can check whether this cycle is within the 'window' we have
125     // to send this packet.
126     // first calculate the number of cycles left before presentation time
127     cycles_until_presentation = diffCycles ( presentation_cycle, cycle );
128
129     // we can check whether this cycle is within the 'window' we have
130     // to send this packet.
131     // first calculate the number of cycles left before presentation time
132     cycles_until_transmit = diffCycles ( transmit_at_cycle, cycle );
133
134     if (dropped) {
135         debugOutput ( DEBUG_LEVEL_VERBOSE,
136                     "Gen HDR: CY=%04u, TC=%04u, CUT=%04d, TST=%011llu (%04u), TSP=%011llu (%04u)\n",
137                     cycle,
138                     transmit_at_cycle, cycles_until_transmit,
139                     transmit_at_time, ( unsigned int ) TICKS_TO_CYCLES ( transmit_at_time ),
140                     presentation_time, ( unsigned int ) TICKS_TO_CYCLES ( presentation_time ) );
141     }
142     // two different options:
143     // 1) there are not enough frames for one packet
144     //      => determine wether this is a problem, since we might still
145     //         have some time to send it
146     // 2) there are enough packets
147     //      => determine whether we have to send them in this packet
148     if ( fc < ( signed int ) m_syt_interval )
149     {
150         // not enough frames in the buffer,
151
152         // we can still postpone the queueing of the packets
153         // if we are far enough ahead of the presentation time
154         if ( cycles_until_presentation <= min_cycles_before_presentation )
155         {
156             debugOutput ( DEBUG_LEVEL_VERBOSE,
157                         "Insufficient frames (P): N=%02d, CY=%04u, TC=%04u, CUT=%04d\n",
158                         fc, cycle, transmit_at_cycle, cycles_until_transmit );
159             // we are too late
160             return eCRV_XRun;
161         }
162         else
163         {
164             unsigned int now_cycle = ( unsigned int ) ( TICKS_TO_CYCLES ( m_1394service.getCycleTimerTicks() ) );
165
166             debugOutput ( DEBUG_LEVEL_VERBOSE,
167                         "Insufficient frames (NP): N=%02d, CY=%04u, TC=%04u, CUT=%04d, NOW=%04d\n",
168                         fc, cycle, transmit_at_cycle, cycles_until_transmit, now_cycle );
169             debugWarning("Insufficient frames (NP): N=%02d, CY=%04u, TC=%04u, CUT=%04d, NOW=%04d\n",
170                          fc, cycle, transmit_at_cycle, cycles_until_transmit, now_cycle );
171
172             // there is still time left to send the packet
173             // we want the system to give this packet another go at a later time instant
174             return eCRV_Again; // note that the raw1394 again system doesn't work as expected
175
176             // we could wait here for a certain time before trying again. However, this
177             // is not going to work since we then block the iterator thread, hence also
178             // the receiving code, meaning that we are not processing received packets,
179             // and hence there is no progression in the number of frames available.
180
181             // for example:
182             // usleep(125); // one cycle
183             // goto try_block_of_frames;
184
185             // or more advanced, calculate how many cycles we are ahead of 'now' and
186             // base the sleep on that.
187
188             // note that this requires that there is one thread for each IsoHandler,
189             // otherwise we're in the deadlock described above.
190         }
191     }
192     else
193     {
194         // there are enough frames, so check the time they are intended for
195         // all frames have a certain 'time window' in which they can be sent
196         // this corresponds to the range of the timestamp mechanism:
197         // we can send a packet 15 cycles in advance of the 'presentation time'
198         // in theory we can send the packet up till one cycle before the presentation time,
199         // however this is not very smart.
200
201         // There are 3 options:
202         // 1) the frame block is too early
203         //      => send an empty packet
204         // 2) the frame block is within the window
205         //      => send it
206         // 3) the frame block is too late
207         //      => discard (and raise xrun?)
208         //         get next block of frames and repeat
209
210         if(cycles_until_transmit < 0)
211         {
212             // we are too late
213             debugOutput(DEBUG_LEVEL_VERBOSE,
214                         "Too late: CY=%04u, TC=%04u, CUT=%04d, TSP=%011llu (%04u)\n",
215                         cycle,
216                         transmit_at_cycle, cycles_until_transmit,
217                         presentation_time, (unsigned int)TICKS_TO_CYCLES(presentation_time) );
218             debugShowBackLogLines(200);
219             flushDebugOutput();
220             // however, if we can send this sufficiently before the presentation
221             // time, it could be harmless.
222             // NOTE: dangerous since the device has no way of reporting that it didn't get
223             //       this packet on time.
224             if(cycles_until_presentation >= min_cycles_before_presentation)
225             {
226                 // we are not that late and can still try to transmit the packet
227                 m_dbc += fillDataPacketHeader(packet, length, m_last_timestamp);
228                 return (fc < (signed)(2*m_syt_interval) ? eCRV_Defer : eCRV_Packet);
229             }
230             else   // definitely too late
231             {
232                 return eCRV_XRun;
233             }
234         }
235         else if(cycles_until_transmit <= max_cycles_to_transmit_early)
236         {
237             // it's time send the packet
238             m_dbc += fillDataPacketHeader(packet, length, m_last_timestamp);
239             return (fc < (signed)(2*m_syt_interval) ? eCRV_Defer : 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 AmdtpTransmitStreamProcessor::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     struct iec61883_packet *packet = ( struct iec61883_packet * ) data;
274     if ( m_data_buffer->readFrames ( m_syt_interval, ( char * ) ( data + 8 ) ) )
275     {
276         // process all ports that should be handled on a per-packet base
277         // this is MIDI for AMDTP (due to the need of DBC)
278         if ( !encodePacketPorts ( ( quadlet_t * ) ( data+8 ), m_syt_interval, packet->dbc ) )
279         {
280             debugWarning ( "Problem encoding Packet Ports\n" );
281         }
282         debugOutput ( DEBUG_LEVEL_VERY_VERBOSE, "XMIT DATA: TSP=%011llu (%04u)\n",
283                     cycle, m_last_timestamp, ( unsigned int ) TICKS_TO_CYCLES ( m_last_timestamp ) );
284         return eCRV_OK;
285     }
286     else return eCRV_XRun;
287
288 }
289
290 enum StreamProcessor::eChildReturnValue
291 AmdtpTransmitStreamProcessor::generateSilentPacketHeader (
292     unsigned char *data, unsigned int *length,
293     unsigned char *tag, unsigned char *sy,
294     int cycle, unsigned int dropped, unsigned int max_length )
295 {
296     struct iec61883_packet *packet = ( struct iec61883_packet * ) data;
297     debugOutput ( DEBUG_LEVEL_VERY_VERBOSE, "XMIT NONE: CY=%04u, TSP=%011llu (%04u)\n",
298                 cycle, m_last_timestamp, ( unsigned int ) TICKS_TO_CYCLES ( m_last_timestamp ) );
299
300     /* Our node ID can change after a bus reset, so it is best to fetch
301     * our node ID for each packet. */
302     packet->sid = m_1394service.getLocalNodeId() & 0x3f;
303
304     packet->dbs = m_dimension;
305     packet->fn = 0;
306     packet->qpc = 0;
307     packet->sph = 0;
308     packet->reserved = 0;
309     packet->dbc = m_dbc;
310     packet->eoh1 = 2;
311     packet->fmt = IEC61883_FMT_AMDTP;
312
313     *tag = IEC61883_TAG_WITH_CIP;
314     *sy = 0;
315
316     m_dbc += fillNoDataPacketHeader ( packet, length );
317     return eCRV_OK;
318 }
319
320 enum StreamProcessor::eChildReturnValue
321 AmdtpTransmitStreamProcessor::generateSilentPacketData (
322     unsigned char *data, unsigned int *length,
323     unsigned char *tag, unsigned char *sy,
324     int cycle, unsigned int dropped, unsigned int max_length )
325 {
326     return eCRV_OK; // no need to do anything
327 }
328
329 unsigned int AmdtpTransmitStreamProcessor::fillDataPacketHeader (
330     struct iec61883_packet *packet, unsigned int* length,
331     uint32_t ts )
332 {
333
334     packet->fdf = m_fdf;
335
336     // convert the timestamp to SYT format
337     uint16_t timestamp_SYT = TICKS_TO_SYT ( ts );
338     packet->syt = ntohs ( timestamp_SYT );
339
340     *length = m_syt_interval*sizeof ( quadlet_t ) *m_dimension + 8;
341
342     return m_syt_interval;
343 }
344
345 unsigned int AmdtpTransmitStreamProcessor::fillNoDataPacketHeader (
346     struct iec61883_packet *packet, unsigned int* length )
347 {
348
349     // no-data packets have syt=0xFFFF
350     // and have the usual amount of events as dummy data (?)
351     packet->fdf = IEC61883_FDF_NODATA;
352     packet->syt = 0xffff;
353
354     // FIXME: either make this a setting or choose
355     bool send_payload=true;
356     if ( send_payload )
357     {
358         // this means no-data packets with payload (DICE doesn't like that)
359         *length = 2*sizeof ( quadlet_t ) + m_syt_interval * m_dimension * sizeof ( quadlet_t );
360         return m_syt_interval;
361     }
362     else
363     {
364         // dbc is not incremented
365         // this means no-data packets without payload
366         *length = 2*sizeof ( quadlet_t );
367         return 0;
368     }
369 }
370
371 unsigned int
372 AmdtpTransmitStreamProcessor::getSytInterval() {
373     switch (m_StreamProcessorManager.getNominalRate()) {
374         case 32000:
375         case 44100:
376         case 48000:
377             return 8;
378         case 88200:
379         case 96000:
380             return 16;
381         case 176400:
382         case 192000:
383             return 32;
384         default:
385             debugError("Unsupported rate: %d\n", m_StreamProcessorManager.getNominalRate());
386             return 0;
387     }
388 }
389 unsigned int
390 AmdtpTransmitStreamProcessor::getFDF() {
391     switch (m_StreamProcessorManager.getNominalRate()) {
392         case 32000: return IEC61883_FDF_SFC_32KHZ;
393         case 44100: return IEC61883_FDF_SFC_44K1HZ;
394         case 48000: return IEC61883_FDF_SFC_48KHZ;
395         case 88200: return IEC61883_FDF_SFC_88K2HZ;
396         case 96000: return IEC61883_FDF_SFC_96KHZ;
397         case 176400: return IEC61883_FDF_SFC_176K4HZ;
398         case 192000: return IEC61883_FDF_SFC_192KHZ;
399         default:
400             debugError("Unsupported rate: %d\n", m_StreamProcessorManager.getNominalRate());
401             return 0;
402     }
403 }
404
405 bool AmdtpTransmitStreamProcessor::prepareChild()
406 {
407     debugOutput ( DEBUG_LEVEL_VERBOSE, "Preparing (%p)...\n", this );
408     m_syt_interval = getSytInterval();
409     m_fdf = getFDF();
410
411     iec61883_cip_init (
412         &m_cip_status,
413         IEC61883_FMT_AMDTP,
414         m_fdf,
415         m_StreamProcessorManager.getNominalRate(),
416         m_dimension,
417         m_syt_interval );
418
419     for ( PortVectorIterator it = m_Ports.begin();
420             it != m_Ports.end();
421             ++it )
422     {
423         if ( ( *it )->getPortType() == Port::E_Midi )
424         {
425             // we use a timing unit of 10ns
426             // this makes sure that for the max syt interval
427             // we don't have rounding, and keeps the numbers low
428             // we have 1 slot every 8 events
429             // we have syt_interval events per packet
430             // => syt_interval/8 slots per packet
431             // packet rate is 8000pkt/sec => interval=125us
432             // so the slot interval is (1/8000)/(syt_interval/8)
433             // or: 1/(1000 * syt_interval) sec
434             // which is 1e9/(1000*syt_interval) nsec
435             // or 100000/syt_interval 'units'
436             // the event interval is fixed to 320us = 32000 'units'
437             if ( ! ( *it )->useRateControl ( true, ( 100000/m_syt_interval ),32000, false ) )
438             {
439                 debugFatal ( "Could not set signal type to PeriodSignalling" );
440                 return false;
441             }
442             break;
443         }
444     }
445     return true;
446 }
447
448 /*
449 * compose the event streams for the packets from the port buffers
450 */
451 bool AmdtpTransmitStreamProcessor::processWriteBlock ( char *data,
452         unsigned int nevents, unsigned int offset )
453 {
454     bool no_problem = true;
455
456     for ( PortVectorIterator it = m_PeriodPorts.begin();
457           it != m_PeriodPorts.end();
458           ++it )
459     {
460         if ( (*it)->isDisabled() ) { continue; };
461
462         //FIXME: make this into a static_cast when not DEBUG?
463         AmdtpPortInfo *pinfo = dynamic_cast<AmdtpPortInfo *> ( *it );
464         assert ( pinfo ); // this should not fail!!
465
466         switch( pinfo->getFormat() )
467         {
468             case AmdtpPortInfo::E_MBLA:
469                 if( encodePortToMBLAEvents(static_cast<AmdtpAudioPort *>(*it), (quadlet_t *)data, offset, nevents) )
470                 {
471                     debugWarning ( "Could not encode port %s to MBLA events", (*it)->getName().c_str() );
472                     no_problem = false;
473                 }
474                 break;
475             case AmdtpPortInfo::E_SPDIF: // still unimplemented
476                 break;
477             default: // ignore
478                 break;
479         }
480     }
481     return no_problem;
482 }
483
484 bool
485 AmdtpTransmitStreamProcessor::transmitSilenceBlock(
486     char *data, unsigned int nevents, unsigned int offset)
487 {
488     bool no_problem = true;
489     for(PortVectorIterator it = m_PeriodPorts.begin();
490         it != m_PeriodPorts.end();
491         ++it )
492     {
493         //FIXME: make this into a static_cast when not DEBUG?
494         AmdtpPortInfo *pinfo=dynamic_cast<AmdtpPortInfo *>(*it);
495         assert(pinfo); // this should not fail!!
496
497         switch( pinfo->getFormat() )
498         {
499             case AmdtpPortInfo::E_MBLA:
500                 if ( encodeSilencePortToMBLAEvents(static_cast<AmdtpAudioPort *>(*it), (quadlet_t *)data, offset, nevents) )
501                 {
502                     debugWarning("Could not encode port %s to MBLA events", (*it)->getName().c_str());
503                     no_problem = false;
504                 }
505                 break;
506             case AmdtpPortInfo::E_SPDIF: // still unimplemented
507                 break;
508             default: // ignore
509                 break;
510         }
511     }
512     return no_problem;
513 }
514
515 /**
516 * @brief decode 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 AmdtpTransmitStreamProcessor::encodePacketPorts ( quadlet_t *data, unsigned int nevents, unsigned int dbc )
524 {
525     bool ok=true;
526     quadlet_t byte;
527
528     quadlet_t *target_event=NULL;
529     unsigned int j;
530
531     for ( PortVectorIterator it = m_PacketPorts.begin();
532             it != m_PacketPorts.end();
533             ++it )
534     {
535
536 #ifdef DEBUG
537         AmdtpPortInfo *pinfo=dynamic_cast<AmdtpPortInfo *> ( *it );
538         assert ( pinfo ); // this should not fail!!
539
540         // the only packet type of events for AMDTP is MIDI in mbla
541         assert ( pinfo->getFormat() ==AmdtpPortInfo::E_Midi );
542 #endif
543
544         AmdtpMidiPort *mp=static_cast<AmdtpMidiPort *> ( *it );
545
546         // we encode this directly (no function call) due to the high frequency
547         /* idea:
548         spec says: current_midi_port=(dbc+j)%8;
549         => if we start at (dbc+stream->location-1)%8,
550         we'll start at the right event for the midi port.
551         => if we increment j with 8, we stay at the right event.
552         */
553         // FIXME: as we know in advance how big a packet is (syt_interval) we can
554         //        predict how much loops will be present here
555         // first prefill the buffer with NO_DATA's on all time muxed channels
556
557         for ( j = ( dbc & 0x07 ) +mp->getLocation(); j < nevents; j += 8 )
558         {
559
560             quadlet_t tmpval;
561
562             target_event= ( quadlet_t * ) ( data + ( ( j * m_dimension ) + mp->getPosition() ) );
563
564             if ( mp->canRead() )   // we can send a byte
565             {
566                 mp->readEvent ( &byte );
567                 byte &= 0xFF;
568                 tmpval=htonl (
569                         IEC61883_AM824_SET_LABEL ( ( byte ) <<16,
570                                                     IEC61883_AM824_LABEL_MIDI_1X ) );
571
572                 debugOutput ( DEBUG_LEVEL_ULTRA_VERBOSE, "MIDI port %s, pos=%d, loc=%d, dbc=%d, nevents=%d, dim=%d\n",
573                             mp->getName().c_str(), mp->getPosition(), mp->getLocation(), dbc, nevents, m_dimension );
574                 debugOutput ( DEBUG_LEVEL_ULTRA_VERBOSE, "base=%p, target=%p, value=%08X\n",
575                             data, target_event, tmpval );
576
577             }
578             else
579             {
580                 // can't send a byte, either because there is no byte,
581                 // or because this would exceed the maximum rate
582                 tmpval=htonl (
583                         IEC61883_AM824_SET_LABEL ( 0,IEC61883_AM824_LABEL_MIDI_NO_DATA ) );
584             }
585
586             *target_event=tmpval;
587         }
588
589     }
590     return ok;
591 }
592
593
594 int AmdtpTransmitStreamProcessor::encodePortToMBLAEvents ( AmdtpAudioPort *p, quadlet_t *data,
595         unsigned int offset, unsigned int nevents )
596 {
597     unsigned int j=0;
598
599     quadlet_t *target_event;
600
601     target_event= ( quadlet_t * ) ( data + p->getPosition() );
602
603     switch ( p->getDataType() )
604     {
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             buffer+=offset;
613
614             for ( j = 0; j < nevents; j += 1 )   // decode max nsamples
615             {
616                 *target_event = htonl ( ( * ( buffer ) & 0x00FFFFFF ) | 0x40000000 );
617                 buffer++;
618                 target_event += m_dimension;
619             }
620         }
621         break;
622         case Port::E_Float:
623         {
624             const float multiplier = ( float ) ( 0x7FFFFF00 );
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             {
633
634                 // don't care for overflow
635                 float v = *buffer * multiplier;  // v: -231 .. 231
636                 unsigned int tmp = ( ( int ) v );
637                 *target_event = htonl ( ( tmp >> 8 ) | 0x40000000 );
638
639                 buffer++;
640                 target_event += m_dimension;
641             }
642         }
643         break;
644     }
645
646     return 0;
647 }
648 int AmdtpTransmitStreamProcessor::encodeSilencePortToMBLAEvents ( AmdtpAudioPort *p, quadlet_t *data,
649         unsigned int offset, unsigned int nevents )
650 {
651     unsigned int j=0;
652
653     quadlet_t *target_event;
654
655     target_event= ( quadlet_t * ) ( data + p->getPosition() );
656
657     switch ( p->getDataType() )
658     {
659         default:
660         case Port::E_Int24:
661         case Port::E_Float:
662         {
663             for ( j = 0; j < nevents; j += 1 )   // decode max nsamples
664             {
665                 *target_event = htonl ( 0x40000000 );
666                 target_event += m_dimension;
667             }
668         }
669         break;
670     }
671
672     return 0;
673 }
674
675 } // end of namespace Streaming
Note: See TracBrowser for help on using the browser.