root/branches/api-cleanup/src/libstreaming/amdtp/AmdtpTransmitStreamProcessor.cpp

Revision 812, 26.0 kB (checked in by ppalmers, 13 years ago)

more fixes

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 "config.h"
25 #include "AmdtpTransmitStreamProcessor.h"
26 #include "AmdtpPort.h"
27 #include "../StreamProcessorManager.h"
28 #include "devicemanager.h"
29
30 #include "libutil/Time.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 namespace Streaming
40 {
41
42 /* transmit */
43 AmdtpTransmitStreamProcessor::AmdtpTransmitStreamProcessor(FFADODevice &parent, int dimension)
44         : StreamProcessor(parent, ePT_Transmit)
45         , m_dimension( dimension )
46         , m_dbc( 0 )
47 {}
48
49 enum StreamProcessor::eChildReturnValue
50 AmdtpTransmitStreamProcessor::generatePacketHeader (
51     unsigned char *data, unsigned int *length,
52     unsigned char *tag, unsigned char *sy,
53     int cycle, unsigned int dropped, unsigned int max_length )
54 {
55     struct iec61883_packet *packet = ( struct iec61883_packet * ) data;
56     /* Our node ID can change after a bus reset, so it is best to fetch
57     * our node ID for each packet. */
58     packet->sid = m_1394service.getLocalNodeId() & 0x3f;
59
60     packet->dbs = m_dimension;
61     packet->fn = 0;
62     packet->qpc = 0;
63     packet->sph = 0;
64     packet->reserved = 0;
65     packet->dbc = m_dbc;
66     packet->eoh1 = 2;
67     packet->fmt = IEC61883_FMT_AMDTP;
68
69     *tag = IEC61883_TAG_WITH_CIP;
70     *sy = 0;
71
72     signed int fc;
73     uint64_t presentation_time;
74     unsigned int presentation_cycle;
75     int cycles_until_presentation;
76
77     uint64_t transmit_at_time;
78     unsigned int transmit_at_cycle;
79     int cycles_until_transmit;
80
81     debugOutput ( DEBUG_LEVEL_ULTRA_VERBOSE, "Try for cycle %d\n", cycle );
82     // check whether the packet buffer has packets for us to send.
83     // the base timestamp is the one of the next sample in the buffer
84     ffado_timestamp_t ts_head_tmp;
85     m_data_buffer->getBufferHeadTimestamp ( &ts_head_tmp, &fc ); // thread safe
86
87     // the timestamp gives us the time at which we want the sample block
88     // to be output by the device
89     presentation_time = ( uint64_t ) ts_head_tmp;
90     m_last_timestamp = presentation_time;
91
92     // now we calculate the time when we have to transmit the sample block
93     transmit_at_time = substractTicks ( presentation_time, AMDTP_TRANSMIT_TRANSFER_DELAY );
94
95     // calculate the cycle this block should be presented in
96     // (this is just a virtual calculation since at that time it should
97     //  already be in the device's buffer)
98     presentation_cycle = ( unsigned int ) ( TICKS_TO_CYCLES ( presentation_time ) );
99
100     // calculate the cycle this block should be transmitted in
101     transmit_at_cycle = ( unsigned int ) ( TICKS_TO_CYCLES ( transmit_at_time ) );
102
103     // we can check whether this cycle is within the 'window' we have
104     // to send this packet.
105     // first calculate the number of cycles left before presentation time
106     cycles_until_presentation = diffCycles ( presentation_cycle, cycle );
107
108     // we can check whether this cycle is within the 'window' we have
109     // to send this packet.
110     // first calculate the number of cycles left before presentation time
111     cycles_until_transmit = diffCycles ( transmit_at_cycle, cycle );
112
113     if (dropped) {
114         debugOutput ( DEBUG_LEVEL_VERBOSE,
115                     "Gen HDR: CY=%04u, TC=%04u, CUT=%04d, TST=%011llu (%04u), TSP=%011llu (%04u)\n",
116                     cycle,
117                     transmit_at_cycle, cycles_until_transmit,
118                     transmit_at_time, ( unsigned int ) TICKS_TO_CYCLES ( transmit_at_time ),
119                     presentation_time, ( unsigned int ) TICKS_TO_CYCLES ( presentation_time ) );
120     }
121     // two different options:
122     // 1) there are not enough frames for one packet
123     //      => determine wether this is a problem, since we might still
124     //         have some time to send it
125     // 2) there are enough packets
126     //      => determine whether we have to send them in this packet
127     if ( fc < ( signed int ) m_syt_interval )
128     {
129         // not enough frames in the buffer,
130
131         // we can still postpone the queueing of the packets
132         // if we are far enough ahead of the presentation time
133         if ( cycles_until_presentation <= AMDTP_MIN_CYCLES_BEFORE_PRESENTATION )
134         {
135             debugOutput ( DEBUG_LEVEL_VERBOSE,
136                         "Insufficient frames (P): N=%02d, CY=%04u, TC=%04u, CUT=%04d\n",
137                         fc, cycle, transmit_at_cycle, cycles_until_transmit );
138             // we are too late
139             return eCRV_XRun;
140         }
141         else
142         {
143             unsigned int now_cycle = ( unsigned int ) ( TICKS_TO_CYCLES ( m_1394service.getCycleTimerTicks() ) );
144
145             debugOutput ( DEBUG_LEVEL_VERBOSE,
146                         "Insufficient frames (NP): N=%02d, CY=%04u, TC=%04u, CUT=%04d, NOW=%04d\n",
147                         fc, cycle, transmit_at_cycle, cycles_until_transmit, now_cycle );
148             debugWarning("Insufficient frames (NP): N=%02d, CY=%04u, TC=%04u, CUT=%04d, NOW=%04d\n",
149                          fc, cycle, transmit_at_cycle, cycles_until_transmit, now_cycle );
150
151             // there is still time left to send the packet
152             // we want the system to give this packet another go at a later time instant
153             return eCRV_Again; // note that the raw1394 again system doesn't work as expected
154
155             // we could wait here for a certain time before trying again. However, this
156             // is not going to work since we then block the iterator thread, hence also
157             // the receiving code, meaning that we are not processing received packets,
158             // and hence there is no progression in the number of frames available.
159
160             // for example:
161             // SleepRelativeUsec(125); // one cycle
162             // goto try_block_of_frames;
163
164             // or more advanced, calculate how many cycles we are ahead of 'now' and
165             // base the sleep on that.
166
167             // note that this requires that there is one thread for each IsoHandler,
168             // otherwise we're in the deadlock described above.
169         }
170     }
171     else
172     {
173         // there are enough frames, so check the time they are intended for
174         // all frames have a certain 'time window' in which they can be sent
175         // this corresponds to the range of the timestamp mechanism:
176         // we can send a packet 15 cycles in advance of the 'presentation time'
177         // in theory we can send the packet up till one cycle before the presentation time,
178         // however this is not very smart.
179
180         // There are 3 options:
181         // 1) the frame block is too early
182         //      => send an empty packet
183         // 2) the frame block is within the window
184         //      => send it
185         // 3) the frame block is too late
186         //      => discard (and raise xrun?)
187         //         get next block of frames and repeat
188
189         if(cycles_until_transmit < 0)
190         {
191             // we are too late
192             debugOutput(DEBUG_LEVEL_VERBOSE,
193                         "Too late: CY=%04u, TC=%04u, CUT=%04d, TSP=%011llu (%04u)\n",
194                         cycle,
195                         transmit_at_cycle, cycles_until_transmit,
196                         presentation_time, (unsigned int)TICKS_TO_CYCLES(presentation_time) );
197             //debugShowBackLogLines(200);
198 //             // however, if we can send this sufficiently before the presentation
199 //             // time, it could be harmless.
200 //             // NOTE: dangerous since the device has no way of reporting that it didn't get
201 //             //       this packet on time.
202 //             if(cycles_until_presentation >= AMDTP_MIN_CYCLES_BEFORE_PRESENTATION)
203 //             {
204 //                 // we are not that late and can still try to transmit the packet
205 //                 m_dbc += fillDataPacketHeader(packet, length, m_last_timestamp);
206 //                 return (fc < (signed)(2*m_syt_interval) ? eCRV_Defer : eCRV_Packet);
207 //             }
208 //             else   // definitely too late
209 //             {
210                 return eCRV_XRun;
211 //             }
212         }
213         else if(cycles_until_transmit <= AMDTP_MAX_CYCLES_TO_TRANSMIT_EARLY)
214         {
215             // it's time send the packet
216             m_dbc += fillDataPacketHeader(packet, length, m_last_timestamp);
217             return (fc < (signed)(2*m_syt_interval) ? eCRV_Defer : eCRV_Packet);
218         }
219         else
220         {
221             debugOutput ( DEBUG_LEVEL_VERY_VERBOSE,
222                         "Too early: CY=%04u, TC=%04u, CUT=%04d, TST=%011llu (%04u), TSP=%011llu (%04u)\n",
223                         cycle,
224                         transmit_at_cycle, cycles_until_transmit,
225                         transmit_at_time, ( unsigned int ) TICKS_TO_CYCLES ( transmit_at_time ),
226                         presentation_time, ( unsigned int ) TICKS_TO_CYCLES ( presentation_time ) );
227 #ifdef DEBUG
228             if ( cycles_until_transmit > AMDTP_MAX_CYCLES_TO_TRANSMIT_EARLY + 1 )
229             {
230                 debugOutput ( DEBUG_LEVEL_VERY_VERBOSE,
231                             "Way too early: CY=%04u, TC=%04u, CUT=%04d, TST=%011llu (%04u), TSP=%011llu (%04u)\n",
232                             cycle,
233                             transmit_at_cycle, cycles_until_transmit,
234                             transmit_at_time, ( unsigned int ) TICKS_TO_CYCLES ( transmit_at_time ),
235                             presentation_time, ( unsigned int ) TICKS_TO_CYCLES ( presentation_time ) );
236             }
237 #endif
238             // we are too early, send only an empty packet
239             return eCRV_EmptyPacket;
240         }
241     }
242     return eCRV_Invalid;
243 }
244
245 enum StreamProcessor::eChildReturnValue
246 AmdtpTransmitStreamProcessor::generatePacketData (
247     unsigned char *data, unsigned int *length,
248     unsigned char *tag, unsigned char *sy,
249     int cycle, unsigned int dropped, unsigned int max_length )
250 {
251     if ( m_data_buffer->readFrames ( m_syt_interval, ( char * ) ( data + 8 ) ) )
252     {
253         debugOutput ( DEBUG_LEVEL_ULTRA_VERBOSE, "XMIT DATA (cy %04d): TSP=%011llu (%04u)\n",
254                     cycle, m_last_timestamp, ( unsigned int ) TICKS_TO_CYCLES ( m_last_timestamp ) );
255         return eCRV_OK;
256     }
257     else return eCRV_XRun;
258
259 }
260
261 enum StreamProcessor::eChildReturnValue
262 AmdtpTransmitStreamProcessor::generateSilentPacketHeader (
263     unsigned char *data, unsigned int *length,
264     unsigned char *tag, unsigned char *sy,
265     int cycle, unsigned int dropped, unsigned int max_length )
266 {
267     struct iec61883_packet *packet = ( struct iec61883_packet * ) data;
268     debugOutput ( DEBUG_LEVEL_ULTRA_VERBOSE, "XMIT NONE (cy %04d): CY=%04u, TSP=%011llu (%04u)\n",
269                 cycle, m_last_timestamp, ( unsigned int ) TICKS_TO_CYCLES ( m_last_timestamp ) );
270
271     /* Our node ID can change after a bus reset, so it is best to fetch
272     * our node ID for each packet. */
273     packet->sid = m_1394service.getLocalNodeId() & 0x3f;
274
275     packet->dbs = m_dimension;
276     packet->fn = 0;
277     packet->qpc = 0;
278     packet->sph = 0;
279     packet->reserved = 0;
280     packet->dbc = m_dbc;
281     packet->eoh1 = 2;
282     packet->fmt = IEC61883_FMT_AMDTP;
283
284     *tag = IEC61883_TAG_WITH_CIP;
285     *sy = 0;
286
287     m_dbc += fillNoDataPacketHeader ( packet, length );
288     return eCRV_OK;
289 }
290
291 enum StreamProcessor::eChildReturnValue
292 AmdtpTransmitStreamProcessor::generateSilentPacketData (
293     unsigned char *data, unsigned int *length,
294     unsigned char *tag, unsigned char *sy,
295     int cycle, unsigned int dropped, unsigned int max_length )
296 {
297     return eCRV_OK; // no need to do anything
298 }
299
300 unsigned int AmdtpTransmitStreamProcessor::fillDataPacketHeader (
301     struct iec61883_packet *packet, unsigned int* length,
302     uint32_t ts )
303 {
304
305     packet->fdf = m_fdf;
306
307     // convert the timestamp to SYT format
308     uint16_t timestamp_SYT = TICKS_TO_SYT ( ts );
309     packet->syt = ntohs ( timestamp_SYT );
310
311     *length = m_syt_interval*sizeof ( quadlet_t ) *m_dimension + 8;
312
313     return m_syt_interval;
314 }
315
316 unsigned int AmdtpTransmitStreamProcessor::fillNoDataPacketHeader (
317     struct iec61883_packet *packet, unsigned int* length )
318 {
319
320     // no-data packets have syt=0xFFFF
321     // and have the usual amount of events as dummy data (?)
322     packet->fdf = IEC61883_FDF_NODATA;
323     packet->syt = 0xffff;
324
325     // FIXME: either make this a setting or choose
326     bool send_payload=true;
327     if ( send_payload )
328     {
329         // this means no-data packets with payload (DICE doesn't like that)
330         *length = 2*sizeof ( quadlet_t ) + m_syt_interval * m_dimension * sizeof ( quadlet_t );
331         return m_syt_interval;
332     }
333     else
334     {
335         // dbc is not incremented
336         // this means no-data packets without payload
337         *length = 2*sizeof ( quadlet_t );
338         return 0;
339     }
340 }
341
342 unsigned int
343 AmdtpTransmitStreamProcessor::getSytInterval() {
344     switch (m_StreamProcessorManager.getNominalRate()) {
345         case 32000:
346         case 44100:
347         case 48000:
348             return 8;
349         case 88200:
350         case 96000:
351             return 16;
352         case 176400:
353         case 192000:
354             return 32;
355         default:
356             debugError("Unsupported rate: %d\n", m_StreamProcessorManager.getNominalRate());
357             return 0;
358     }
359 }
360 unsigned int
361 AmdtpTransmitStreamProcessor::getFDF() {
362     switch (m_StreamProcessorManager.getNominalRate()) {
363         case 32000: return IEC61883_FDF_SFC_32KHZ;
364         case 44100: return IEC61883_FDF_SFC_44K1HZ;
365         case 48000: return IEC61883_FDF_SFC_48KHZ;
366         case 88200: return IEC61883_FDF_SFC_88K2HZ;
367         case 96000: return IEC61883_FDF_SFC_96KHZ;
368         case 176400: return IEC61883_FDF_SFC_176K4HZ;
369         case 192000: return IEC61883_FDF_SFC_192KHZ;
370         default:
371             debugError("Unsupported rate: %d\n", m_StreamProcessorManager.getNominalRate());
372             return 0;
373     }
374 }
375
376 bool AmdtpTransmitStreamProcessor::prepareChild()
377 {
378     debugOutput ( DEBUG_LEVEL_VERBOSE, "Preparing (%p)...\n", this );
379     m_syt_interval = getSytInterval();
380     m_fdf = getFDF();
381
382     iec61883_cip_init (
383         &m_cip_status,
384         IEC61883_FMT_AMDTP,
385         m_fdf,
386         m_StreamProcessorManager.getNominalRate(),
387         m_dimension,
388         m_syt_interval );
389
390     return true;
391 }
392
393 /*
394 * compose the event streams for the packets from the port buffers
395 */
396 bool AmdtpTransmitStreamProcessor::processWriteBlock ( char *data,
397         unsigned int nevents, unsigned int offset )
398 {
399     bool no_problem = true;
400
401     for ( PortVectorIterator it = m_Ports.begin();
402           it != m_Ports.end();
403           ++it )
404     {
405         if ( (*it)->isDisabled() ) { continue; };
406
407         //FIXME: make this into a static_cast when not DEBUG?
408         AmdtpPortInfo *pinfo = dynamic_cast<AmdtpPortInfo *> ( *it );
409         assert ( pinfo ); // this should not fail!!
410
411         switch( pinfo->getFormat() )
412         {
413             case AmdtpPortInfo::E_MBLA:
414                 if( encodePortToMBLAEvents(static_cast<AmdtpAudioPort *>(*it), (quadlet_t *)data, offset, nevents) )
415                 {
416                     debugWarning ( "Could not encode port %s to MBLA events", (*it)->getName().c_str() );
417                     no_problem = false;
418                 }
419                 break;
420             case AmdtpPortInfo::E_SPDIF: // still unimplemented
421                 break;
422             case AmdtpPortInfo::E_Midi:
423                 if( encodePortToMidiEvents(static_cast<AmdtpMidiPort *>(*it), (quadlet_t *)data, offset, nevents) )
424                 {
425                     debugWarning ( "Could not encode port %s to Midi events", (*it)->getName().c_str() );
426                     no_problem = false;
427                 }
428                 break;
429             default: // ignore
430                 break;
431         }
432     }
433     return no_problem;
434 }
435
436 bool
437 AmdtpTransmitStreamProcessor::transmitSilenceBlock(
438     char *data, unsigned int nevents, unsigned int offset)
439 {
440     bool no_problem = true;
441     for(PortVectorIterator it = m_Ports.begin();
442         it != m_Ports.end();
443         ++it )
444     {
445         //FIXME: make this into a static_cast when not DEBUG?
446         AmdtpPortInfo *pinfo=dynamic_cast<AmdtpPortInfo *>(*it);
447         assert(pinfo); // this should not fail!!
448
449         switch( pinfo->getFormat() )
450         {
451             case AmdtpPortInfo::E_MBLA:
452                 if ( encodeSilencePortToMBLAEvents(static_cast<AmdtpAudioPort *>(*it), (quadlet_t *)data, offset, nevents) )
453                 {
454                     debugWarning("Could not encode silence for port %s to MBLA events", (*it)->getName().c_str());
455                     no_problem = false;
456                 }
457                 break;
458             case AmdtpPortInfo::E_SPDIF: // still unimplemented
459                 break;
460             case AmdtpPortInfo::E_Midi:
461                 if( encodeSilencePortToMidiEvents(static_cast<AmdtpMidiPort *>(*it), (quadlet_t *)data, offset, nevents) )
462                 {
463                     debugWarning ( "Could not encode silence for port %s to Midi events", (*it)->getName().c_str() );
464                     no_problem = false;
465                 }
466                 break;
467             default: // ignore
468                 break;
469         }
470     }
471     return no_problem;
472 }
473
474 #if USE_SSE
475 #error Broken
476 typedef float v4sf __attribute__ ((vector_size (16)));
477 typedef int v4si __attribute__ ((vector_size (16)));
478 typedef int v2si __attribute__ ((vector_size (8)));
479
480 int AmdtpTransmitStreamProcessor::encodePortToMBLAEvents ( AmdtpAudioPort *p, quadlet_t *data,
481         unsigned int offset, unsigned int nevents )
482 {
483     static const float sse_multiplier[4] __attribute__((aligned(16))) = {
484         (float)(0x7FFFFF00),
485         (float)(0x7FFFFF00),
486         (float)(0x7FFFFF00),
487         (float)(0x7FFFFF00)
488     };
489
490     static const int sse_mask[4] __attribute__((aligned(16))) = {
491         0x40000000,  0x40000000,  0x40000000,  0x40000000
492     };
493
494     unsigned int out[4];
495
496     unsigned int j=0;
497     unsigned int read=0;
498
499     quadlet_t *target_event;
500
501     target_event= ( quadlet_t * ) ( data + p->getPosition() );
502
503     switch ( p->getDataType() )
504     {
505         default:
506             debugError("bad type: %d\n", p->getDataType());
507             return -1;
508         case Port::E_Int24:
509         {
510             quadlet_t *buffer= ( quadlet_t * ) ( p->getBufferAddress() );
511
512             assert ( nevents + offset <= p->getBufferSize() );
513
514             buffer+=offset;
515
516             for ( j = 0; j < nevents; j += 1 )   // decode max nsamples
517             {
518                 *target_event = htonl ( ( * ( buffer ) & 0x00FFFFFF ) | 0x40000000 );
519                 buffer++;
520                 target_event += m_dimension;
521             }
522         }
523         break;
524         case Port::E_Float:
525         {
526             const float multiplier = ( float ) ( 0x7FFFFF00 );
527             float *buffer= ( float * ) ( p->getBufferAddress() );
528
529             assert ( nevents + offset <= p->getBufferSize() );
530
531             buffer+=offset;
532
533             j=0;
534             if(read>3) {
535                 for (j = 0; j < read-3; j += 4) {
536                     asm("movups %[floatbuff], %%xmm0\n\t"
537                             "mulps %[ssemult], %%xmm0\n\t"
538                             "cvttps2pi %%xmm0, %[out1]\n\t"
539                             "movhlps %%xmm0, %%xmm0\n\t"
540                             "psrld $8, %[out1]\n\t"
541                             "cvttps2pi %%xmm0, %[out2]\n\t"
542                             "por %[mmxmask], %[out1]\n\t"
543                             "psrld $8, %[out2]\n\t"
544                             "por %[mmxmask], %[out2]\n\t"
545                         : [out1] "=&y" (*(v2si*)&out[0]),
546                     [out2] "=&y" (*(v2si*)&out[2])
547                         : [floatbuff] "m" (*(v4sf*)buffer),
548                     [ssemult] "x" (*(v4sf*)sse_multiplier),
549                     [mmxmask] "y" (*(v2si*)sse_mask)
550                         : "xmm0");
551                     buffer += 4;
552                     *target_event = htonl(out[0]);
553                     target_event += m_dimension;
554                     *target_event = htonl(out[1]);
555                     target_event += m_dimension;
556                     *target_event = htonl(out[2]);
557                     target_event += m_dimension;
558                     *target_event = htonl(out[3]);
559                     target_event += m_dimension;
560                 }
561             }
562             for(; j < read; ++j) {
563             // don't care for overflow
564                 float v = *buffer * multiplier;  // v: -231 .. 231
565                 unsigned int tmp = (int)v;
566                 *target_event = htonl((tmp >> 8) | 0x40000000);
567    
568                 buffer++;
569                 target_event += m_dimension;
570             }
571
572             asm volatile("emms");
573             break;
574         }
575         break;
576     }
577
578     return 0;
579 }
580
581 #else
582
583 int AmdtpTransmitStreamProcessor::encodePortToMBLAEvents ( AmdtpAudioPort *p, quadlet_t *data,
584         unsigned int offset, unsigned int nevents )
585 {
586     unsigned int j=0;
587
588     quadlet_t *target_event;
589
590     target_event= ( quadlet_t * ) ( data + p->getPosition() );
591
592     switch ( p->getDataType() )
593     {
594         default:
595             debugError("bad type: %d\n", p->getDataType());
596             return -1;
597         case Port::E_Int24:
598         {
599             quadlet_t *buffer= ( quadlet_t * ) ( p->getBufferAddress() );
600
601             assert ( nevents + offset <= p->getBufferSize() );
602
603             buffer+=offset;
604
605             for ( j = 0; j < nevents; j += 1 )   // decode max nsamples
606             {
607                 *target_event = htonl ( ( * ( buffer ) & 0x00FFFFFF ) | 0x40000000 );
608                 buffer++;
609                 target_event += m_dimension;
610             }
611         }
612         break;
613         case Port::E_Float:
614         {
615             const float multiplier = ( float ) ( 0x7FFFFF00 );
616             float *buffer= ( float * ) ( p->getBufferAddress() );
617
618             assert ( nevents + offset <= p->getBufferSize() );
619
620             buffer+=offset;
621
622             for ( j = 0; j < nevents; j += 1 )   // decode max nsamples
623             {
624
625                 // don't care for overflow
626                 float v = *buffer * multiplier;  // v: -231 .. 231
627                 unsigned int tmp = ( ( int ) v );
628                 *target_event = htonl ( ( tmp >> 8 ) | 0x40000000 );
629
630                 buffer++;
631                 target_event += m_dimension;
632             }
633         }
634         break;
635     }
636
637     return 0;
638 }
639
640 // note: make sure that the midi events in the port buffer are only placed on (buffer+offset+x)%8 == 0
641 // i.e. 8 frame aligned
642 int AmdtpTransmitStreamProcessor::encodePortToMidiEvents ( AmdtpMidiPort *p, quadlet_t *data,
643         unsigned int offset, unsigned int nevents )
644 {
645     unsigned int j=0;
646     unsigned int position = p->getPosition();
647     unsigned int location = p->getLocation();
648
649     quadlet_t *target_event;
650     quadlet_t tmpval;
651
652     switch ( p->getDataType() )
653     {
654         default:
655             debugError("bad type: %d\n", p->getDataType());
656             return -1;
657         case Port::E_MidiEvent:
658         {
659             quadlet_t *buffer = (quadlet_t *)(p->getBufferAddress());
660
661             assert(nevents + offset <= p->getBufferSize());
662
663             buffer+=offset;
664
665             for ( j = location; j < nevents; j += 8 )
666             {
667                 target_event = (quadlet_t *) (data + ((j * m_dimension) + position));
668
669                 if ( *buffer & 0xFF000000 )   // we can send a byte
670                 {
671                     tmpval = ((*buffer)<<16) & 0x00FF0000;
672                     tmpval=IEC61883_AM824_SET_LABEL(tmpval, IEC61883_AM824_LABEL_MIDI_1X);
673                     *target_event = htonl(tmpval);
674
675                     // debugOutput ( DEBUG_LEVEL_VERBOSE, "MIDI port %s, pos=%u, loc=%u, nevents=%u, dim=%d\n",
676                     //            p->getName().c_str(), position, location, nevents, m_dimension );
677                     // debugOutput ( DEBUG_LEVEL_VERBOSE, "base=%p, target=%p, value=%08X\n",
678                     //            data, target_event, tmpval );
679                 } else {
680                     // can't send a byte, either because there is no byte,
681                     // or because this would exceed the maximum rate
682                     // FIXME: this can be ifdef optimized since it's a constant
683                     *target_event = htonl(IEC61883_AM824_SET_LABEL(0, IEC61883_AM824_LABEL_MIDI_NO_DATA));
684                 }
685                 buffer+=8;
686             }
687         }
688         break;
689     }
690     return 0;
691 }
692
693 #endif
694
695 int AmdtpTransmitStreamProcessor::encodeSilencePortToMBLAEvents ( AmdtpAudioPort *p, quadlet_t *data,
696         unsigned int offset, unsigned int nevents )
697 {
698     unsigned int j=0;
699
700     quadlet_t *target_event;
701
702     target_event= ( quadlet_t * ) ( data + p->getPosition() );
703
704     switch ( p->getDataType() )
705     {
706         default:
707         case Port::E_Int24:
708         case Port::E_Float:
709         {
710             for ( j = 0; j < nevents; j += 1 )   // decode max nsamples
711             {
712                 *target_event = htonl ( 0x40000000 );
713                 target_event += m_dimension;
714             }
715         }
716         break;
717     }
718
719     return 0;
720 }
721
722 int AmdtpTransmitStreamProcessor::encodeSilencePortToMidiEvents ( AmdtpMidiPort *p, quadlet_t *data,
723         unsigned int offset, unsigned int nevents )
724 {
725     unsigned int j=0;
726     unsigned int position = p->getPosition();
727     unsigned int location = p->getLocation();
728
729     quadlet_t *target_event;
730
731     switch ( p->getDataType() )
732     {
733         default:
734             debugError("bad type: %d\n", p->getDataType());
735             return -1;
736         case Port::E_MidiEvent:
737         {
738             for ( j = location; j < nevents; j += 8 )
739             {
740                 target_event = (quadlet_t *) (data + ((j * m_dimension) + position));
741                 *target_event=htonl(IEC61883_AM824_SET_LABEL(0, IEC61883_AM824_LABEL_MIDI_NO_DATA));
742             }
743         }
744         break;
745     }
746     return 0;
747 }
748
749 } // end of namespace Streaming
Note: See TracBrowser for help on using the browser.