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