Debian Codegraph @

3rdparty/iaxclient/lib/portaudio/src/common/pa_process.c

raw symbols SCIP
1 /*
2 * $Id: pa_process.c,v 1.1 2006/06/10 21:30:55 dmazzoni Exp $
3 * Portable Audio I/O Library
4 * streamCallback <-> host buffer processing adapter
5 *
6 * Based on the Open Source API proposed by Ross Bencina
7 * Copyright (c) 1999-2002 Ross Bencina, Phil Burk
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining
10 * a copy of this software and associated documentation files
11 * (the "Software"), to deal in the Software without restriction,
12 * including without limitation the rights to use, copy, modify, merge,
13 * publish, distribute, sublicense, and/or sell copies of the Software,
14 * and to permit persons to whom the Software is furnished to do so,
15 * subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
19 *
20 * Any person wishing to distribute modifications to the Software is
21 * requested to send the modifications to the original developer so that
22 * they can be incorporated into the canonical version.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
27 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
28 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 */
32
33 /** @file
34 @brief Buffer Processor implementation.
35
36 The code in this file is not optimised yet - although it's not clear that
37 it needs to be. there may appear to be redundancies
38 that could be factored into common functions, but the redundanceis are left
39 intentionally as each appearance may have different optimisation possibilities.
40
41 The optimisations which are planned involve only converting data in-place
42 where possible, rather than copying to the temp buffer(s).
43
44 Note that in the extreme case of being able to convert in-place, and there
45 being no conversion necessary there should be some code which short-circuits
46 the operation.
47
48 @todo Consider cache tilings for intereave<->deinterleave.
49
50 @todo implement timeInfo->currentTime int PaUtil_BeginBufferProcessing()
51
52 @todo specify and implement some kind of logical policy for handling the
53 underflow and overflow stream flags when the underflow/overflow overlaps
54 multiple user buffers/callbacks.
55
56 @todo provide support for priming the buffers with data from the callback.
57 The client interface is now implemented through PaUtil_SetNoInput()
58 which sets bp->hostInputChannels[0][0].data to zero. However this is
59 currently only implemented in NonAdaptingProcess(). It shouldn't be
60 needed for AdaptingInputOnlyProcess() (no priming should ever be
61 requested for AdaptingInputOnlyProcess()).
62 Not sure if additional work should be required to make it work with
63 AdaptingOutputOnlyProcess, but it definitely is required for
64 AdaptingProcess.
65
66 @todo implement PaUtil_SetNoOutput for AdaptingProcess
67
68 @todo don't allocate temp buffers for blocking streams unless they are
69 needed. At the moment they are needed, but perhaps for host APIs
70 where the implementation passes a buffer to the host they could be
71 used.
72 */
73
74
75 #include <assert.h>
76 #include <string.h> /* memset() */
77
78 #include "pa_process.h"
79 #include "pa_util.h"
80
81
82 #define PA_FRAMES_PER_TEMP_BUFFER_WHEN_HOST_BUFFER_SIZE_IS_UNKNOWN_ 1024
83
84 #define PA_MIN_( a, b ) ( ((a)<(b)) ? (a) : (b) )
85
86
87 /* greatest common divisor - PGCD in French */
88 static unsigned long GCD( unsigned long a, unsigned long b )
89 {
90 return (b==0) ? a : GCD( b, a%b);
91 }
92
93 /* least common multiple - PPCM in French */
94 static unsigned long LCM( unsigned long a, unsigned long b )
95 {
96 return (a*b) / GCD(a,b);
97 }
98
99 #define PA_MAX_( a, b ) (((a) > (b)) ? (a) : (b))
100
101 static unsigned long CalculateFrameShift( unsigned long M, unsigned long N )
102 {
103 unsigned long result = 0;
104 unsigned long i;
105 unsigned long lcm;
106
107 assert( M > 0 );
108 assert( N > 0 );
109
110 lcm = LCM( M, N );
111 for( i = M; i < lcm; i += M )
112 result = PA_MAX_( result, i % N );
113
114 return result;
115 }
116
117
118 PaError PaUtil_InitializeBufferProcessor( PaUtilBufferProcessor* bp,
119 int inputChannelCount, PaSampleFormat userInputSampleFormat,
120 PaSampleFormat hostInputSampleFormat,
121 int outputChannelCount, PaSampleFormat userOutputSampleFormat,
122 PaSampleFormat hostOutputSampleFormat,
123 double sampleRate,
124 PaStreamFlags streamFlags,
125 unsigned long framesPerUserBuffer,
126 unsigned long framesPerHostBuffer,
127 PaUtilHostBufferSizeMode hostBufferSizeMode,
128 PaStreamCallback *streamCallback, void *userData )
129 {
130 PaError result = paNoError;
131 PaError bytesPerSample;
132 unsigned long tempInputBufferSize, tempOutputBufferSize;
133
134 if( streamFlags & paNeverDropInput )
135 {
136 /* paNeverDropInput is only valid for full-duplex callback streams, with an unspecified number of frames per buffer. */
137 if( !streamCallback || !(inputChannelCount > 0 && outputChannelCount > 0) ||
138 framesPerUserBuffer != paFramesPerBufferUnspecified )
139 return paInvalidFlag;
140 }
141
142 /* initialize buffer ptrs to zero so they can be freed if necessary in error */
143 bp->tempInputBuffer = 0;
144 bp->tempInputBufferPtrs = 0;
145 bp->tempOutputBuffer = 0;
146 bp->tempOutputBufferPtrs = 0;
147
148 bp->framesPerUserBuffer = framesPerUserBuffer;
149 bp->framesPerHostBuffer = framesPerHostBuffer;
150
151 bp->inputChannelCount = inputChannelCount;
152 bp->outputChannelCount = outputChannelCount;
153
154 bp->hostBufferSizeMode = hostBufferSizeMode;
155
156 bp->hostInputChannels[0] = bp->hostInputChannels[1] = 0;
157 bp->hostOutputChannels[0] = bp->hostOutputChannels[1] = 0;
158
159 if( framesPerUserBuffer == 0 ) /* streamCallback will accept any buffer size */
160 {
161 bp->useNonAdaptingProcess = 1;
162 bp->initialFramesInTempInputBuffer = 0;
163 bp->initialFramesInTempOutputBuffer = 0;
164
165 if( hostBufferSizeMode == paUtilFixedHostBufferSize
166 || hostBufferSizeMode == paUtilBoundedHostBufferSize )
167 {
168 bp->framesPerTempBuffer = framesPerHostBuffer;
169 }
170 else /* unknown host buffer size */
171 {
172 bp->framesPerTempBuffer = PA_FRAMES_PER_TEMP_BUFFER_WHEN_HOST_BUFFER_SIZE_IS_UNKNOWN_;
173 }
174 }
175 else
176 {
177 bp->framesPerTempBuffer = framesPerUserBuffer;
178
179 if( hostBufferSizeMode == paUtilFixedHostBufferSize
180 && framesPerHostBuffer % framesPerUserBuffer == 0 )
181 {
182 bp->useNonAdaptingProcess = 1;
183 bp->initialFramesInTempInputBuffer = 0;
184 bp->initialFramesInTempOutputBuffer = 0;
185 }
186 else
187 {
188 bp->useNonAdaptingProcess = 0;
189
190 if( inputChannelCount > 0 && outputChannelCount > 0 )
191 {
192 /* full duplex */
193 if( hostBufferSizeMode == paUtilFixedHostBufferSize )
194 {
195 unsigned long frameShift =
196 CalculateFrameShift( framesPerHostBuffer, framesPerUserBuffer );
197
198 if( framesPerUserBuffer > framesPerHostBuffer )
199 {
200 bp->initialFramesInTempInputBuffer = frameShift;
201 bp->initialFramesInTempOutputBuffer = 0;
202 }
203 else
204 {
205 bp->initialFramesInTempInputBuffer = 0;
206 bp->initialFramesInTempOutputBuffer = frameShift;
207 }
208 }
209 else /* variable host buffer size, add framesPerUserBuffer latency */
210 {
211 bp->initialFramesInTempInputBuffer = 0;
212 bp->initialFramesInTempOutputBuffer = framesPerUserBuffer;
213 }
214 }
215 else
216 {
217 /* half duplex */
218 bp->initialFramesInTempInputBuffer = 0;
219 bp->initialFramesInTempOutputBuffer = 0;
220 }
221 }
222 }
223
224
225 bp->framesInTempInputBuffer = bp->initialFramesInTempInputBuffer;
226 bp->framesInTempOutputBuffer = bp->initialFramesInTempOutputBuffer;
227
228
229 if( inputChannelCount > 0 )
230 {
231 bytesPerSample = Pa_GetSampleSize( hostInputSampleFormat );
232 if( bytesPerSample > 0 )
233 {
234 bp->bytesPerHostInputSample = bytesPerSample;
235 }
236 else
237 {
238 result = bytesPerSample;
239 goto error;
240 }
241
242 bytesPerSample = Pa_GetSampleSize( userInputSampleFormat );
243 if( bytesPerSample > 0 )
244 {
245 bp->bytesPerUserInputSample = bytesPerSample;
246 }
247 else
248 {
249 result = bytesPerSample;
250 goto error;
251 }
252
253 bp->inputConverter =
254 PaUtil_SelectConverter( hostInputSampleFormat, userInputSampleFormat, streamFlags );
255
256 bp->inputZeroer = PaUtil_SelectZeroer( hostInputSampleFormat );
257
258 bp->userInputIsInterleaved = (userInputSampleFormat & paNonInterleaved)?0:1;
259
260
261 tempInputBufferSize =
262 bp->framesPerTempBuffer * bp->bytesPerUserInputSample * inputChannelCount;
263
264 bp->tempInputBuffer = PaUtil_AllocateMemory( tempInputBufferSize );
265 if( bp->tempInputBuffer == 0 )
266 {
267 result = paInsufficientMemory;
268 goto error;
269 }
270
271 if( bp->framesInTempInputBuffer > 0 )
272 memset( bp->tempInputBuffer, 0, tempInputBufferSize );
273
274 if( userInputSampleFormat & paNonInterleaved )
275 {
276 bp->tempInputBufferPtrs =
277 (void **)PaUtil_AllocateMemory( sizeof(void*)*inputChannelCount );
278 if( bp->tempInputBufferPtrs == 0 )
279 {
280 result = paInsufficientMemory;
281 goto error;
282 }
283 }
284
285 bp->hostInputChannels[0] = (PaUtilChannelDescriptor*)
286 PaUtil_AllocateMemory( sizeof(PaUtilChannelDescriptor) * inputChannelCount * 2);
287 if( bp->hostInputChannels[0] == 0 )
288 {
289 result = paInsufficientMemory;
290 goto error;
291 }
292
293 bp->hostInputChannels[1] = &bp->hostInputChannels[0][inputChannelCount];
294 }
295
296 if( outputChannelCount > 0 )
297 {
298 bytesPerSample = Pa_GetSampleSize( hostOutputSampleFormat );
299 if( bytesPerSample > 0 )
300 {
301 bp->bytesPerHostOutputSample = bytesPerSample;
302 }
303 else
304 {
305 result = bytesPerSample;
306 goto error;
307 }
308
309 bytesPerSample = Pa_GetSampleSize( userOutputSampleFormat );
310 if( bytesPerSample > 0 )
311 {
312 bp->bytesPerUserOutputSample = bytesPerSample;
313 }
314 else
315 {
316 result = bytesPerSample;
317 goto error;
318 }
319
320 bp->outputConverter =
321 PaUtil_SelectConverter( userOutputSampleFormat, hostOutputSampleFormat, streamFlags );
322
323 bp->outputZeroer = PaUtil_SelectZeroer( hostOutputSampleFormat );
324
325 bp->userOutputIsInterleaved = (userOutputSampleFormat & paNonInterleaved)?0:1;
326
327 tempOutputBufferSize =
328 bp->framesPerTempBuffer * bp->bytesPerUserOutputSample * outputChannelCount;
329
330 bp->tempOutputBuffer = PaUtil_AllocateMemory( tempOutputBufferSize );
331 if( bp->tempOutputBuffer == 0 )
332 {
333 result = paInsufficientMemory;
334 goto error;
335 }
336
337 if( bp->framesInTempOutputBuffer > 0 )
338 memset( bp->tempOutputBuffer, 0, tempOutputBufferSize );
339
340 if( userOutputSampleFormat & paNonInterleaved )
341 {
342 bp->tempOutputBufferPtrs =
343 (void **)PaUtil_AllocateMemory( sizeof(void*)*outputChannelCount );
344 if( bp->tempOutputBufferPtrs == 0 )
345 {
346 result = paInsufficientMemory;
347 goto error;
348 }
349 }
350
351 bp->hostOutputChannels[0] = (PaUtilChannelDescriptor*)
352 PaUtil_AllocateMemory( sizeof(PaUtilChannelDescriptor)*outputChannelCount * 2 );
353 if( bp->hostOutputChannels[0] == 0 )
354 {
355 result = paInsufficientMemory;
356 goto error;
357 }
358
359 bp->hostOutputChannels[1] = &bp->hostOutputChannels[0][outputChannelCount];
360 }
361
362 PaUtil_InitializeTriangularDitherState( &bp->ditherGenerator );
363
364 bp->samplePeriod = 1. / sampleRate;
365
366 bp->streamCallback = streamCallback;
367 bp->userData = userData;
368
369 return result;
370
371 error:
372 if( bp->tempInputBuffer )
373 PaUtil_FreeMemory( bp->tempInputBuffer );
374
375 if( bp->tempInputBufferPtrs )
376 PaUtil_FreeMemory( bp->tempInputBufferPtrs );
377
378 if( bp->hostInputChannels[0] )
379 PaUtil_FreeMemory( bp->hostInputChannels[0] );
380
381 if( bp->tempOutputBuffer )
382 PaUtil_FreeMemory( bp->tempOutputBuffer );
383
384 if( bp->tempOutputBufferPtrs )
385 PaUtil_FreeMemory( bp->tempOutputBufferPtrs );
386
387 if( bp->hostOutputChannels[0] )
388 PaUtil_FreeMemory( bp->hostOutputChannels[0] );
389
390 return result;
391 }
392
393
394 void PaUtil_TerminateBufferProcessor( PaUtilBufferProcessor* bp )
395 {
396 if( bp->tempInputBuffer )
397 PaUtil_FreeMemory( bp->tempInputBuffer );
398
399 if( bp->tempInputBufferPtrs )
400 PaUtil_FreeMemory( bp->tempInputBufferPtrs );
401
402 if( bp->hostInputChannels[0] )
403 PaUtil_FreeMemory( bp->hostInputChannels[0] );
404
405 if( bp->tempOutputBuffer )
406 PaUtil_FreeMemory( bp->tempOutputBuffer );
407
408 if( bp->tempOutputBufferPtrs )
409 PaUtil_FreeMemory( bp->tempOutputBufferPtrs );
410
411 if( bp->hostOutputChannels[0] )
412 PaUtil_FreeMemory( bp->hostOutputChannels[0] );
413 }
414
415
416 void PaUtil_ResetBufferProcessor( PaUtilBufferProcessor* bp )
417 {
418 unsigned long tempInputBufferSize, tempOutputBufferSize;
419
420 bp->framesInTempInputBuffer = bp->initialFramesInTempInputBuffer;
421 bp->framesInTempOutputBuffer = bp->initialFramesInTempOutputBuffer;
422
423 if( bp->framesInTempInputBuffer > 0 )
424 {
425 tempInputBufferSize =
426 bp->framesPerTempBuffer * bp->bytesPerUserInputSample * bp->inputChannelCount;
427 memset( bp->tempInputBuffer, 0, tempInputBufferSize );
428 }
429
430 if( bp->framesInTempOutputBuffer > 0 )
431 {
432 tempOutputBufferSize =
433 bp->framesPerTempBuffer * bp->bytesPerUserOutputSample * bp->outputChannelCount;
434 memset( bp->tempOutputBuffer, 0, tempOutputBufferSize );
435 }
436 }
437
438
439 unsigned long PaUtil_GetBufferProcessorInputLatency( PaUtilBufferProcessor* bp )
440 {
441 return bp->initialFramesInTempInputBuffer;
442 }
443
444
445 unsigned long PaUtil_GetBufferProcessorOutputLatency( PaUtilBufferProcessor* bp )
446 {
447 return bp->initialFramesInTempOutputBuffer;
448 }
449
450
451 void PaUtil_SetInputFrameCount( PaUtilBufferProcessor* bp,
452 unsigned long frameCount )
453 {
454 if( frameCount == 0 )
455 bp->hostInputFrameCount[0] = bp->framesPerHostBuffer;
456 else
457 bp->hostInputFrameCount[0] = frameCount;
458 }
459
460
461 void PaUtil_SetNoInput( PaUtilBufferProcessor* bp )
462 {
463 assert( bp->inputChannelCount > 0 );
464
465 bp->hostInputChannels[0][0].data = 0;
466 }
467
468
469 void PaUtil_SetInputChannel( PaUtilBufferProcessor* bp,
470 unsigned int channel, void *data, unsigned int stride )
471 {
472 assert( channel < bp->inputChannelCount );
473
474 bp->hostInputChannels[0][channel].data = data;
475 bp->hostInputChannels[0][channel].stride = stride;
476 }
477
478
479 void PaUtil_SetInterleavedInputChannels( PaUtilBufferProcessor* bp,
480 unsigned int firstChannel, void *data, unsigned int channelCount )
481 {
482 unsigned int i;
483 unsigned int channel = firstChannel;
484 unsigned char *p = (unsigned char*)data;
485
486 if( channelCount == 0 )
487 channelCount = bp->inputChannelCount;
488
489 assert( firstChannel < bp->inputChannelCount );
490 assert( firstChannel + channelCount <= bp->inputChannelCount );
491
492 for( i=0; i< channelCount; ++i )
493 {
494 bp->hostInputChannels[0][channel+i].data = p;
495 p += bp->bytesPerHostInputSample;
496 bp->hostInputChannels[0][channel+i].stride = channelCount;
497 }
498 }
499
500
501 void PaUtil_SetNonInterleavedInputChannel( PaUtilBufferProcessor* bp,
502 unsigned int channel, void *data )
503 {
504 assert( channel < bp->inputChannelCount );
505
506 bp->hostInputChannels[0][channel].data = data;
507 bp->hostInputChannels[0][channel].stride = 1;
508 }
509
510
511 void PaUtil_Set2ndInputFrameCount( PaUtilBufferProcessor* bp,
512 unsigned long frameCount )
513 {
514 bp->hostInputFrameCount[1] = frameCount;
515 }
516
517
518 void PaUtil_Set2ndInputChannel( PaUtilBufferProcessor* bp,
519 unsigned int channel, void *data, unsigned int stride )
520 {
521 assert( channel < bp->inputChannelCount );
522
523 bp->hostInputChannels[1][channel].data = data;
524 bp->hostInputChannels[1][channel].stride = stride;
525 }
526
527
528 void PaUtil_Set2ndInterleavedInputChannels( PaUtilBufferProcessor* bp,
529 unsigned int firstChannel, void *data, unsigned int channelCount )
530 {
531 unsigned int i;
532 unsigned int channel = firstChannel;
533 unsigned char *p = (unsigned char*)data;
534
535 if( channelCount == 0 )
536 channelCount = bp->inputChannelCount;
537
538 assert( firstChannel < bp->inputChannelCount );
539 assert( firstChannel + channelCount <= bp->inputChannelCount );
540
541 for( i=0; i< channelCount; ++i )
542 {
543 bp->hostInputChannels[1][channel+i].data = p;
544 p += bp->bytesPerHostInputSample;
545 bp->hostInputChannels[1][channel+i].stride = channelCount;
546 }
547 }
548
549
550 void PaUtil_Set2ndNonInterleavedInputChannel( PaUtilBufferProcessor* bp,
551 unsigned int channel, void *data )
552 {
553 assert( channel < bp->inputChannelCount );
554
555 bp->hostInputChannels[1][channel].data = data;
556 bp->hostInputChannels[1][channel].stride = 1;
557 }
558
559
560 void PaUtil_SetOutputFrameCount( PaUtilBufferProcessor* bp,
561 unsigned long frameCount )
562 {
563 if( frameCount == 0 )
564 bp->hostOutputFrameCount[0] = bp->framesPerHostBuffer;
565 else
566 bp->hostOutputFrameCount[0] = frameCount;
567 }
568
569
570 void PaUtil_SetNoOutput( PaUtilBufferProcessor* bp )
571 {
572 assert( bp->outputChannelCount > 0 );
573
574 bp->hostOutputChannels[0][0].data = 0;
575 }
576
577
578 void PaUtil_SetOutputChannel( PaUtilBufferProcessor* bp,
579 unsigned int channel, void *data, unsigned int stride )
580 {
581 assert( channel < bp->outputChannelCount );
582 assert( data != NULL );
583
584 bp->hostOutputChannels[0][channel].data = data;
585 bp->hostOutputChannels[0][channel].stride = stride;
586 }
587
588
589 void PaUtil_SetInterleavedOutputChannels( PaUtilBufferProcessor* bp,
590 unsigned int firstChannel, void *data, unsigned int channelCount )
591 {
592 unsigned int i;
593 unsigned int channel = firstChannel;
594 unsigned char *p = (unsigned char*)data;
595
596 if( channelCount == 0 )
597 channelCount = bp->outputChannelCount;
598
599 assert( firstChannel < bp->outputChannelCount );
600 assert( firstChannel + channelCount <= bp->outputChannelCount );
601
602 for( i=0; i< channelCount; ++i )
603 {
604 PaUtil_SetOutputChannel( bp, channel + i, p, channelCount );
605 p += bp->bytesPerHostOutputSample;
606 }
607 }
608
609
610 void PaUtil_SetNonInterleavedOutputChannel( PaUtilBufferProcessor* bp,
611 unsigned int channel, void *data )
612 {
613 assert( channel < bp->outputChannelCount );
614
615 PaUtil_SetOutputChannel( bp, channel, data, 1 );
616 }
617
618
619 void PaUtil_Set2ndOutputFrameCount( PaUtilBufferProcessor* bp,
620 unsigned long frameCount )
621 {
622 bp->hostOutputFrameCount[1] = frameCount;
623 }
624
625
626 void PaUtil_Set2ndOutputChannel( PaUtilBufferProcessor* bp,
627 unsigned int channel, void *data, unsigned int stride )
628 {
629 assert( channel < bp->outputChannelCount );
630 assert( data != NULL );
631
632 bp->hostOutputChannels[1][channel].data = data;
633 bp->hostOutputChannels[1][channel].stride = stride;
634 }
635
636
637 void PaUtil_Set2ndInterleavedOutputChannels( PaUtilBufferProcessor* bp,
638 unsigned int firstChannel, void *data, unsigned int channelCount )
639 {
640 unsigned int i;
641 unsigned int channel = firstChannel;
642 unsigned char *p = (unsigned char*)data;
643
644 if( channelCount == 0 )
645 channelCount = bp->outputChannelCount;
646
647 assert( firstChannel < bp->outputChannelCount );
648 assert( firstChannel + channelCount <= bp->outputChannelCount );
649
650 for( i=0; i< channelCount; ++i )
651 {
652 PaUtil_Set2ndOutputChannel( bp, channel + i, p, channelCount );
653 p += bp->bytesPerHostOutputSample;
654 }
655 }
656
657
658 void PaUtil_Set2ndNonInterleavedOutputChannel( PaUtilBufferProcessor* bp,
659 unsigned int channel, void *data )
660 {
661 assert( channel < bp->outputChannelCount );
662
663 PaUtil_Set2ndOutputChannel( bp, channel, data, 1 );
664 }
665
666
667 void PaUtil_BeginBufferProcessing( PaUtilBufferProcessor* bp,
668 PaStreamCallbackTimeInfo* timeInfo, PaStreamCallbackFlags callbackStatusFlags )
669 {
670 bp->timeInfo = timeInfo;
671
672 /* the first streamCallback will be called to process samples which are
673 currently in the input buffer before the ones starting at the timeInfo time */
674
675 bp->timeInfo->inputBufferAdcTime -= bp->framesInTempInputBuffer * bp->samplePeriod;
676
677 bp->timeInfo->currentTime = 0; /** FIXME: @todo time info currentTime not implemented */
678
679 /* the first streamCallback will be called to generate samples which will be
680 outputted after the frames currently in the output buffer have been
681 outputted. */
682 bp->timeInfo->outputBufferDacTime += bp->framesInTempOutputBuffer * bp->samplePeriod;
683
684 bp->callbackStatusFlags = callbackStatusFlags;
685
686 bp->hostInputFrameCount[1] = 0;
687 bp->hostOutputFrameCount[1] = 0;
688 }
689
690
691 /*
692 NonAdaptingProcess() is a simple buffer copying adaptor that can handle
693 both full and half duplex copies. It processes framesToProcess frames,
694 broken into blocks bp->framesPerTempBuffer long.
695 This routine can be used when the streamCallback doesn't care what length
696 the buffers are, or when framesToProcess is an integer multiple of
697 bp->framesPerTempBuffer, in which case streamCallback will always be called
698 with bp->framesPerTempBuffer samples.
699 */
700 static unsigned long NonAdaptingProcess( PaUtilBufferProcessor *bp,
701 int *streamCallbackResult,
702 PaUtilChannelDescriptor *hostInputChannels,
703 PaUtilChannelDescriptor *hostOutputChannels,
704 unsigned long framesToProcess )
705 {
706 void *userInput, *userOutput;
707 unsigned char *srcBytePtr, *destBytePtr;
708 unsigned int srcSampleStrideSamples; /* stride from one sample to the next within a channel, in samples */
709 unsigned int srcChannelStrideBytes; /* stride from one channel to the next, in bytes */
710 unsigned int destSampleStrideSamples; /* stride from one sample to the next within a channel, in samples */
711 unsigned int destChannelStrideBytes; /* stride from one channel to the next, in bytes */
712 unsigned int i;
713 unsigned long frameCount;
714 unsigned long framesToGo = framesToProcess;
715 unsigned long framesProcessed = 0;
716
717
718 if( *streamCallbackResult == paContinue )
719 {
720 do
721 {
722 frameCount = PA_MIN_( bp->framesPerTempBuffer, framesToGo );
723
724 /* configure user input buffer and convert input data (host -> user) */
725 if( bp->inputChannelCount == 0 )
726 {
727 /* no input */
728 userInput = 0;
729 }
730 else /* there are input channels */
731 {
732 /*
733 could use more elaborate logic here and sometimes process
734 buffers in-place.
735 */
736
737 destBytePtr = (unsigned char *)bp->tempInputBuffer;
738
739 if( bp->userInputIsInterleaved )
740 {
741 destSampleStrideSamples = bp->inputChannelCount;
742 destChannelStrideBytes = bp->bytesPerUserInputSample;
743 userInput = bp->tempInputBuffer;
744 }
745 else /* user input is not interleaved */
746 {
747 destSampleStrideSamples = 1;
748 destChannelStrideBytes = frameCount * bp->bytesPerUserInputSample;
749
750 /* setup non-interleaved ptrs */
751 for( i=0; i<bp->inputChannelCount; ++i )
752 {
753 bp->tempInputBufferPtrs[i] = ((unsigned char*)bp->tempInputBuffer) +
754 i * bp->bytesPerUserInputSample * frameCount;
755 }
756
757 userInput = bp->tempInputBufferPtrs;
758 }
759
760 if( !bp->hostInputChannels[0][0].data )
761 {
762 /* no input was supplied (see PaUtil_SetNoInput), so
763 zero the input buffer */
764
765 for( i=0; i<bp->inputChannelCount; ++i )
766 {
767 bp->inputZeroer( destBytePtr, destSampleStrideSamples, frameCount );
768 destBytePtr += destChannelStrideBytes; /* skip to next destination channel */
769 }
770 }
771 else
772 {
773 for( i=0; i<bp->inputChannelCount; ++i )
774 {
775 bp->inputConverter( destBytePtr, destSampleStrideSamples,
776 hostInputChannels[i].data,
777 hostInputChannels[i].stride,
778 frameCount, &bp->ditherGenerator );
779
780 destBytePtr += destChannelStrideBytes; /* skip to next destination channel */
781
782 /* advance src ptr for next iteration */
783 hostInputChannels[i].data = ((unsigned char*)hostInputChannels[i].data) +
784 frameCount * hostInputChannels[i].stride * bp->bytesPerHostInputSample;
785 }
786 }
787 }
788
789 /* configure user output buffer */
790 if( bp->outputChannelCount == 0 )
791 {
792 /* no output */
793 userOutput = 0;
794 }
795 else /* there are output channels */
796 {
797 if( bp->userOutputIsInterleaved )
798 {
799 userOutput = bp->tempOutputBuffer;
800 }
801 else /* user output is not interleaved */
802 {
803 for( i = 0; i < bp->outputChannelCount; ++i )
804 {
805 bp->tempOutputBufferPtrs[i] = ((unsigned char*)bp->tempOutputBuffer) +
806 i * bp->bytesPerUserOutputSample * frameCount;
807 }
808
809 userOutput = bp->tempOutputBufferPtrs;
810 }
811 }
812
813 *streamCallbackResult = bp->streamCallback( userInput, userOutput,
814 frameCount, bp->timeInfo, bp->callbackStatusFlags, bp->userData );
815
816 if( *streamCallbackResult == paAbort )
817 {
818 /* callback returned paAbort, don't advance framesProcessed
819 and framesToGo, they will be handled below */
820 }
821 else
822 {
823 bp->timeInfo->inputBufferAdcTime += frameCount * bp->samplePeriod;
824 bp->timeInfo->outputBufferDacTime += frameCount * bp->samplePeriod;
825
826 /* convert output data (user -> host) */
827
828 if( bp->outputChannelCount != 0 && bp->hostOutputChannels[0][0].data )
829 {
830 /*
831 could use more elaborate logic here and sometimes process
832 buffers in-place.
833 */
834
835 srcBytePtr = (unsigned char *)bp->tempOutputBuffer;
836
837 if( bp->userOutputIsInterleaved )
838 {
839 srcSampleStrideSamples = bp->outputChannelCount;
840 srcChannelStrideBytes = bp->bytesPerUserOutputSample;
841 }
842 else /* user output is not interleaved */
843 {
844 srcSampleStrideSamples = 1;
845 srcChannelStrideBytes = frameCount * bp->bytesPerUserOutputSample;
846 }
847
848 for( i=0; i<bp->outputChannelCount; ++i )
849 {
850 bp->outputConverter( hostOutputChannels[i].data,
851 hostOutputChannels[i].stride,
852 srcBytePtr, srcSampleStrideSamples,
853 frameCount, &bp->ditherGenerator );
854
855 srcBytePtr += srcChannelStrideBytes; /* skip to next source channel */
856
857 /* advance dest ptr for next iteration */
858 hostOutputChannels[i].data = ((unsigned char*)hostOutputChannels[i].data) +
859 frameCount * hostOutputChannels[i].stride * bp->bytesPerHostOutputSample;
860 }
861 }
862
863 framesProcessed += frameCount;
864
865 framesToGo -= frameCount;
866 }
867 }
868 while( framesToGo > 0 && *streamCallbackResult == paContinue );
869 }
870
871 if( framesToGo > 0 )
872 {
873 /* zero any remaining frames output. There will only be remaining frames
874 if the callback has returned paComplete or paAbort */
875
876 frameCount = framesToGo;
877
878 if( bp->outputChannelCount != 0 && bp->hostOutputChannels[0][0].data )
879 {
880 for( i=0; i<bp->outputChannelCount; ++i )
881 {
882 bp->outputZeroer( hostOutputChannels[i].data,
883 hostOutputChannels[i].stride,
884 frameCount );
885
886 /* advance dest ptr for next iteration */
887 hostOutputChannels[i].data = ((unsigned char*)hostOutputChannels[i].data) +
888 frameCount * hostOutputChannels[i].stride * bp->bytesPerHostOutputSample;
889 }
890 }
891
892 framesProcessed += frameCount;
893 }
894
895 return framesProcessed;
896 }
897
898
899 /*
900 AdaptingInputOnlyProcess() is a half duplex input buffer processor. It
901 converts data from the input buffers into the temporary input buffer,
902 when the temporary input buffer is full, it calls the streamCallback.
903 */
904 static unsigned long AdaptingInputOnlyProcess( PaUtilBufferProcessor *bp,
905 int *streamCallbackResult,
906 PaUtilChannelDescriptor *hostInputChannels,
907 unsigned long framesToProcess )
908 {
909 void *userInput, *userOutput;
910 unsigned char *destBytePtr;
911 unsigned int destSampleStrideSamples; /* stride from one sample to the next within a channel, in samples */
912 unsigned int destChannelStrideBytes; /* stride from one channel to the next, in bytes */
913 unsigned int i;
914 unsigned long frameCount;
915 unsigned long framesToGo = framesToProcess;
916 unsigned long framesProcessed = 0;
917
918 userOutput = 0;
919
920 do
921 {
922 frameCount = ( bp->framesInTempInputBuffer + framesToGo > bp->framesPerUserBuffer )
923 ? ( bp->framesPerUserBuffer - bp->framesInTempInputBuffer )
924 : framesToGo;
925
926 /* convert frameCount samples into temp buffer */
927
928 if( bp->userInputIsInterleaved )
929 {
930 destBytePtr = ((unsigned char*)bp->tempInputBuffer) +
931 bp->bytesPerUserInputSample * bp->inputChannelCount *
932 bp->framesInTempInputBuffer;
933
934 destSampleStrideSamples = bp->inputChannelCount;
935 destChannelStrideBytes = bp->bytesPerUserInputSample;
936
937 userInput = bp->tempInputBuffer;
938 }
939 else /* user input is not interleaved */
940 {
941 destBytePtr = ((unsigned char*)bp->tempInputBuffer) +
942 bp->bytesPerUserInputSample * bp->framesInTempInputBuffer;
943
944 destSampleStrideSamples = 1;
945 destChannelStrideBytes = bp->framesPerUserBuffer * bp->bytesPerUserInputSample;
946
947 /* setup non-interleaved ptrs */
948 for( i=0; i<bp->inputChannelCount; ++i )
949 {
950 bp->tempInputBufferPtrs[i] = ((unsigned char*)bp->tempInputBuffer) +
951 i * bp->bytesPerUserInputSample * bp->framesPerUserBuffer;
952 }
953
954 userInput = bp->tempInputBufferPtrs;
955 }
956
957 for( i=0; i<bp->inputChannelCount; ++i )
958 {
959 bp->inputConverter( destBytePtr, destSampleStrideSamples,
960 hostInputChannels[i].data,
961 hostInputChannels[i].stride,
962 frameCount, &bp->ditherGenerator );
963
964 destBytePtr += destChannelStrideBytes; /* skip to next destination channel */
965
966 /* advance src ptr for next iteration */
967 hostInputChannels[i].data = ((unsigned char*)hostInputChannels[i].data) +
968 frameCount * hostInputChannels[i].stride * bp->bytesPerHostInputSample;
969 }
970
971 bp->framesInTempInputBuffer += frameCount;
972
973 if( bp->framesInTempInputBuffer == bp->framesPerUserBuffer )
974 {
975 /**
976 @todo (non-critical optimisation)
977 The conditional below implements the continue/complete/abort mechanism
978 simply by continuing on iterating through the input buffer, but not
979 passing the data to the callback. With care, the outer loop could be
980 terminated earlier, thus some unneeded conversion cycles would be
981 saved.
982 */
983 if( *streamCallbackResult == paContinue )
984 {
985 bp->timeInfo->outputBufferDacTime = 0;
986
987 *streamCallbackResult = bp->streamCallback( userInput, userOutput,
988 bp->framesPerUserBuffer, bp->timeInfo,
989 bp->callbackStatusFlags, bp->userData );
990
991 bp->timeInfo->inputBufferAdcTime += frameCount * bp->samplePeriod;
992 }
993
994 bp->framesInTempInputBuffer = 0;
995 }
996
997 framesProcessed += frameCount;
998
999 framesToGo -= frameCount;
1000 }while( framesToGo > 0 );
1001
1002 return framesProcessed;
1003 }
1004
1005
1006 /*
1007 AdaptingOutputOnlyProcess() is a half duplex output buffer processor.
1008 It converts data from the temporary output buffer, to the output buffers,
1009 when the temporary output buffer is empty, it calls the streamCallback.
1010 */
1011 static unsigned long AdaptingOutputOnlyProcess( PaUtilBufferProcessor *bp,
1012 int *streamCallbackResult,
1013 PaUtilChannelDescriptor *hostOutputChannels,
1014 unsigned long framesToProcess )
1015 {
1016 void *userInput, *userOutput;
1017 unsigned char *srcBytePtr;
1018 unsigned int srcSampleStrideSamples; /* stride from one sample to the next within a channel, in samples */
1019 unsigned int srcChannelStrideBytes; /* stride from one channel to the next, in bytes */
1020 unsigned int i;
1021 unsigned long frameCount;
1022 unsigned long framesToGo = framesToProcess;
1023 unsigned long framesProcessed = 0;
1024
1025 do
1026 {
1027 if( bp->framesInTempOutputBuffer == 0 && *streamCallbackResult == paContinue )
1028 {
1029 userInput = 0;
1030
1031 /* setup userOutput */
1032 if( bp->userOutputIsInterleaved )
1033 {
1034 userOutput = bp->tempOutputBuffer;
1035 }
1036 else /* user output is not interleaved */
1037 {
1038 for( i = 0; i < bp->outputChannelCount; ++i )
1039 {
1040 bp->tempOutputBufferPtrs[i] = ((unsigned char*)bp->tempOutputBuffer) +
1041 i * bp->framesPerUserBuffer * bp->bytesPerUserOutputSample;
1042 }
1043
1044 userOutput = bp->tempOutputBufferPtrs;
1045 }
1046
1047 bp->timeInfo->inputBufferAdcTime = 0;
1048
1049 *streamCallbackResult = bp->streamCallback( userInput, userOutput,
1050 bp->framesPerUserBuffer, bp->timeInfo,
1051 bp->callbackStatusFlags, bp->userData );
1052
1053 if( *streamCallbackResult == paAbort )
1054 {
1055 /* if the callback returned paAbort, we disregard its output */
1056 }
1057 else
1058 {
1059 bp->timeInfo->outputBufferDacTime += bp->framesPerUserBuffer * bp->samplePeriod;
1060
1061 bp->framesInTempOutputBuffer = bp->framesPerUserBuffer;
1062 }
1063 }
1064
1065 if( bp->framesInTempOutputBuffer > 0 )
1066 {
1067 /* convert frameCount frames from user buffer to host buffer */
1068
1069 frameCount = PA_MIN_( bp->framesInTempOutputBuffer, framesToGo );
1070
1071 if( bp->userOutputIsInterleaved )
1072 {
1073 srcBytePtr = ((unsigned char*)bp->tempOutputBuffer) +
1074 bp->bytesPerUserOutputSample * bp->outputChannelCount *
1075 (bp->framesPerUserBuffer - bp->framesInTempOutputBuffer);
1076
1077 srcSampleStrideSamples = bp->outputChannelCount;
1078 srcChannelStrideBytes = bp->bytesPerUserOutputSample;
1079 }
1080 else /* user output is not interleaved */
1081 {
1082 srcBytePtr = ((unsigned char*)bp->tempOutputBuffer) +
1083 bp->bytesPerUserOutputSample *
1084 (bp->framesPerUserBuffer - bp->framesInTempOutputBuffer);
1085
1086 srcSampleStrideSamples = 1;
1087 srcChannelStrideBytes = bp->framesPerUserBuffer * bp->bytesPerUserOutputSample;
1088 }
1089
1090 for( i=0; i<bp->outputChannelCount; ++i )
1091 {
1092 bp->outputConverter( hostOutputChannels[i].data,
1093 hostOutputChannels[i].stride,
1094 srcBytePtr, srcSampleStrideSamples,
1095 frameCount, &bp->ditherGenerator );
1096
1097 srcBytePtr += srcChannelStrideBytes; /* skip to next source channel */
1098
1099 /* advance dest ptr for next iteration */
1100 hostOutputChannels[i].data = ((unsigned char*)hostOutputChannels[i].data) +
1101 frameCount * hostOutputChannels[i].stride * bp->bytesPerHostOutputSample;
1102 }
1103
1104 bp->framesInTempOutputBuffer -= frameCount;
1105 }
1106 else
1107 {
1108 /* no more user data is available because the callback has returned
1109 paComplete or paAbort. Fill the remainder of the host buffer
1110 with zeros.
1111 */
1112
1113 frameCount = framesToGo;
1114
1115 for( i=0; i<bp->outputChannelCount; ++i )
1116 {
1117 bp->outputZeroer( hostOutputChannels[i].data,
1118 hostOutputChannels[i].stride,
1119 frameCount );
1120
1121 /* advance dest ptr for next iteration */
1122 hostOutputChannels[i].data = ((unsigned char*)hostOutputChannels[i].data) +
1123 frameCount * hostOutputChannels[i].stride * bp->bytesPerHostOutputSample;
1124 }
1125 }
1126
1127 framesProcessed += frameCount;
1128
1129 framesToGo -= frameCount;
1130
1131 }while( framesToGo > 0 );
1132
1133 return framesProcessed;
1134 }
1135
1136 /* CopyTempOutputBuffersToHostOutputBuffers is called from AdaptingProcess to copy frames from
1137 tempOutputBuffer to hostOutputChannels. This includes data conversion
1138 and interleaving.
1139 */
1140 static void CopyTempOutputBuffersToHostOutputBuffers( PaUtilBufferProcessor *bp)
1141 {
1142 unsigned long maxFramesToCopy;
1143 PaUtilChannelDescriptor *hostOutputChannels;
1144 unsigned int frameCount;
1145 unsigned char *srcBytePtr;
1146 unsigned int srcSampleStrideSamples; /* stride from one sample to the next within a channel, in samples */
1147 unsigned int srcChannelStrideBytes; /* stride from one channel to the next, in bytes */
1148 unsigned int i;
1149
1150 /* copy frames from user to host output buffers */
1151 while( bp->framesInTempOutputBuffer > 0 &&
1152 ((bp->hostOutputFrameCount[0] + bp->hostOutputFrameCount[1]) > 0) )
1153 {
1154 maxFramesToCopy = bp->framesInTempOutputBuffer;
1155
1156 /* select the output buffer set (1st or 2nd) */
1157 if( bp->hostOutputFrameCount[0] > 0 )
1158 {
1159 hostOutputChannels = bp->hostOutputChannels[0];
1160 frameCount = PA_MIN_( bp->hostOutputFrameCount[0], maxFramesToCopy );
1161 }
1162 else
1163 {
1164 hostOutputChannels = bp->hostOutputChannels[1];
1165 frameCount = PA_MIN_( bp->hostOutputFrameCount[1], maxFramesToCopy );
1166 }
1167
1168 if( bp->userOutputIsInterleaved )
1169 {
1170 srcBytePtr = ((unsigned char*)bp->tempOutputBuffer) +
1171 bp->bytesPerUserOutputSample * bp->outputChannelCount *
1172 (bp->framesPerUserBuffer - bp->framesInTempOutputBuffer);
1173
1174 srcSampleStrideSamples = bp->outputChannelCount;
1175 srcChannelStrideBytes = bp->bytesPerUserOutputSample;
1176 }
1177 else /* user output is not interleaved */
1178 {
1179 srcBytePtr = ((unsigned char*)bp->tempOutputBuffer) +
1180 bp->bytesPerUserOutputSample *
1181 (bp->framesPerUserBuffer - bp->framesInTempOutputBuffer);
1182
1183 srcSampleStrideSamples = 1;
1184 srcChannelStrideBytes = bp->framesPerUserBuffer * bp->bytesPerUserOutputSample;
1185 }
1186
1187 for( i=0; i<bp->outputChannelCount; ++i )
1188 {
1189 assert( hostOutputChannels[i].data != NULL );
1190 bp->outputConverter( hostOutputChannels[i].data,
1191 hostOutputChannels[i].stride,
1192 srcBytePtr, srcSampleStrideSamples,
1193 frameCount, &bp->ditherGenerator );
1194
1195 srcBytePtr += srcChannelStrideBytes; /* skip to next source channel */
1196
1197 /* advance dest ptr for next iteration */
1198 hostOutputChannels[i].data = ((unsigned char*)hostOutputChannels[i].data) +
1199 frameCount * hostOutputChannels[i].stride * bp->bytesPerHostOutputSample;
1200 }
1201
1202 if( bp->hostOutputFrameCount[0] > 0 )
1203 bp->hostOutputFrameCount[0] -= frameCount;
1204 else
1205 bp->hostOutputFrameCount[1] -= frameCount;
1206
1207 bp->framesInTempOutputBuffer -= frameCount;
1208 }
1209 }
1210
1211 /*
1212 AdaptingProcess is a full duplex adapting buffer processor. It converts
1213 data from the temporary output buffer into the host output buffers, then
1214 from the host input buffers into the temporary input buffers. Calling the
1215 streamCallback when necessary.
1216 When processPartialUserBuffers is 0, all available input data will be
1217 consumed and all available output space will be filled. When
1218 processPartialUserBuffers is non-zero, as many full user buffers
1219 as possible will be processed, but partial buffers will not be consumed.
1220 */
1221 static unsigned long AdaptingProcess( PaUtilBufferProcessor *bp,
1222 int *streamCallbackResult, int processPartialUserBuffers )
1223 {
1224 void *userInput, *userOutput;
1225 unsigned long framesProcessed = 0;
1226 unsigned long framesAvailable;
1227 unsigned long endProcessingMinFrameCount;
1228 unsigned long maxFramesToCopy;
1229 PaUtilChannelDescriptor *hostInputChannels, *hostOutputChannels;
1230 unsigned int frameCount;
1231 unsigned char *destBytePtr;
1232 unsigned int destSampleStrideSamples; /* stride from one sample to the next within a channel, in samples */
1233 unsigned int destChannelStrideBytes; /* stride from one channel to the next, in bytes */
1234 unsigned int i, j;
1235
1236
1237 framesAvailable = bp->hostInputFrameCount[0] + bp->hostInputFrameCount[1];/* this is assumed to be the same as the output buffer's frame count */
1238
1239 if( processPartialUserBuffers )
1240 endProcessingMinFrameCount = 0;
1241 else
1242 endProcessingMinFrameCount = (bp->framesPerUserBuffer - 1);
1243
1244 /* Fill host output with remaining frames in user output (tempOutputBuffer) */
1245 CopyTempOutputBuffersToHostOutputBuffers( bp );
1246
1247 while( framesAvailable > endProcessingMinFrameCount )
1248 {
1249
1250 if( bp->framesInTempOutputBuffer == 0 && *streamCallbackResult != paContinue )
1251 {
1252 /* the callback will not be called any more, so zero what remains
1253 of the host output buffers */
1254
1255 for( i=0; i<2; ++i )
1256 {
1257 frameCount = bp->hostOutputFrameCount[i];
1258 if( frameCount > 0 )
1259 {
1260 hostOutputChannels = bp->hostOutputChannels[i];
1261
1262 for( j=0; j<bp->outputChannelCount; ++j )
1263 {
1264 bp->outputZeroer( hostOutputChannels[j].data,
1265 hostOutputChannels[j].stride,
1266 frameCount );
1267
1268 /* advance dest ptr for next iteration */
1269 hostOutputChannels[j].data = ((unsigned char*)hostOutputChannels[j].data) +
1270 frameCount * hostOutputChannels[j].stride * bp->bytesPerHostOutputSample;
1271 }
1272 bp->hostOutputFrameCount[i] = 0;
1273 }
1274 }
1275 }
1276
1277
1278 /* copy frames from host to user input buffers */
1279 while( bp->framesInTempInputBuffer < bp->framesPerUserBuffer &&
1280 ((bp->hostInputFrameCount[0] + bp->hostInputFrameCount[1]) > 0) )
1281 {
1282 maxFramesToCopy = bp->framesPerUserBuffer - bp->framesInTempInputBuffer;
1283
1284 /* select the input buffer set (1st or 2nd) */
1285 if( bp->hostInputFrameCount[0] > 0 )
1286 {
1287 hostInputChannels = bp->hostInputChannels[0];
1288 frameCount = PA_MIN_( bp->hostInputFrameCount[0], maxFramesToCopy );
1289 }
1290 else
1291 {
1292 hostInputChannels = bp->hostInputChannels[1];
1293 frameCount = PA_MIN_( bp->hostInputFrameCount[1], maxFramesToCopy );
1294 }
1295
1296 /* configure conversion destination pointers */
1297 if( bp->userInputIsInterleaved )
1298 {
1299 destBytePtr = ((unsigned char*)bp->tempInputBuffer) +
1300 bp->bytesPerUserInputSample * bp->inputChannelCount *
1301 bp->framesInTempInputBuffer;
1302
1303 destSampleStrideSamples = bp->inputChannelCount;
1304 destChannelStrideBytes = bp->bytesPerUserInputSample;
1305 }
1306 else /* user input is not interleaved */
1307 {
1308 destBytePtr = ((unsigned char*)bp->tempInputBuffer) +
1309 bp->bytesPerUserInputSample * bp->framesInTempInputBuffer;
1310
1311 destSampleStrideSamples = 1;
1312 destChannelStrideBytes = bp->framesPerUserBuffer * bp->bytesPerUserInputSample;
1313 }
1314
1315 for( i=0; i<bp->inputChannelCount; ++i )
1316 {
1317 bp->inputConverter( destBytePtr, destSampleStrideSamples,
1318 hostInputChannels[i].data,
1319 hostInputChannels[i].stride,
1320 frameCount, &bp->ditherGenerator );
1321
1322 destBytePtr += destChannelStrideBytes; /* skip to next destination channel */
1323
1324 /* advance src ptr for next iteration */
1325 hostInputChannels[i].data = ((unsigned char*)hostInputChannels[i].data) +
1326 frameCount * hostInputChannels[i].stride * bp->bytesPerHostInputSample;
1327 }
1328
1329 if( bp->hostInputFrameCount[0] > 0 )
1330 bp->hostInputFrameCount[0] -= frameCount;
1331 else
1332 bp->hostInputFrameCount[1] -= frameCount;
1333
1334 bp->framesInTempInputBuffer += frameCount;
1335
1336 /* update framesAvailable and framesProcessed based on input consumed
1337 unless something is very wrong this will also correspond to the
1338 amount of output generated */
1339 framesAvailable -= frameCount;
1340 framesProcessed += frameCount;
1341 }
1342
1343 /* call streamCallback */
1344 if( bp->framesInTempInputBuffer == bp->framesPerUserBuffer &&
1345 bp->framesInTempOutputBuffer == 0 )
1346 {
1347 if( *streamCallbackResult == paContinue )
1348 {
1349 /* setup userInput */
1350 if( bp->userInputIsInterleaved )
1351 {
1352 userInput = bp->tempInputBuffer;
1353 }
1354 else /* user input is not interleaved */
1355 {
1356 for( i = 0; i < bp->inputChannelCount; ++i )
1357 {
1358 bp->tempInputBufferPtrs[i] = ((unsigned char*)bp->tempInputBuffer) +
1359 i * bp->framesPerUserBuffer * bp->bytesPerUserInputSample;
1360 }
1361
1362 userInput = bp->tempInputBufferPtrs;
1363 }
1364
1365 /* setup userOutput */
1366 if( bp->userOutputIsInterleaved )
1367 {
1368 userOutput = bp->tempOutputBuffer;
1369 }
1370 else /* user output is not interleaved */
1371 {
1372 for( i = 0; i < bp->outputChannelCount; ++i )
1373 {
1374 bp->tempOutputBufferPtrs[i] = ((unsigned char*)bp->tempOutputBuffer) +
1375 i * bp->framesPerUserBuffer * bp->bytesPerUserOutputSample;
1376 }
1377
1378 userOutput = bp->tempOutputBufferPtrs;
1379 }
1380
1381 /* call streamCallback */
1382
1383 *streamCallbackResult = bp->streamCallback( userInput, userOutput,
1384 bp->framesPerUserBuffer, bp->timeInfo,
1385 bp->callbackStatusFlags, bp->userData );
1386
1387 bp->timeInfo->inputBufferAdcTime += bp->framesPerUserBuffer * bp->samplePeriod;
1388 bp->timeInfo->outputBufferDacTime += bp->framesPerUserBuffer * bp->samplePeriod;
1389
1390 bp->framesInTempInputBuffer = 0;
1391
1392 if( *streamCallbackResult == paAbort )
1393 bp->framesInTempOutputBuffer = 0;
1394 else
1395 bp->framesInTempOutputBuffer = bp->framesPerUserBuffer;
1396 }
1397 else
1398 {
1399 /* paComplete or paAbort has already been called. */
1400
1401 bp->framesInTempInputBuffer = 0;
1402 }
1403 }
1404
1405 /* copy frames from user (tempOutputBuffer) to host output buffers (hostOutputChannels)
1406 Means to process the user output provided by the callback. Has to be called after
1407 each callback. */
1408 CopyTempOutputBuffersToHostOutputBuffers( bp );
1409
1410 }
1411
1412 return framesProcessed;
1413 }
1414
1415
1416 unsigned long PaUtil_EndBufferProcessing( PaUtilBufferProcessor* bp, int *streamCallbackResult )
1417 {
1418 unsigned long framesToProcess, framesToGo;
1419 unsigned long framesProcessed = 0;
1420
1421 if( bp->inputChannelCount != 0 && bp->outputChannelCount != 0
1422 && bp->hostInputChannels[0][0].data /* input was supplied (see PaUtil_SetNoInput) */
1423 && bp->hostOutputChannels[0][0].data /* output was supplied (see PaUtil_SetNoOutput) */ )
1424 {
1425 assert( (bp->hostInputFrameCount[0] + bp->hostInputFrameCount[1]) ==
1426 (bp->hostOutputFrameCount[0] + bp->hostOutputFrameCount[1]) );
1427 }
1428
1429 assert( *streamCallbackResult == paContinue
1430 || *streamCallbackResult == paComplete
1431 || *streamCallbackResult == paAbort ); /* don't forget to pass in a valid callback result value */
1432
1433 if( bp->useNonAdaptingProcess )
1434 {
1435 if( bp->inputChannelCount != 0 && bp->outputChannelCount != 0 )
1436 {
1437 /* full duplex non-adapting process, splice buffers if they are
1438 different lengths */
1439
1440 framesToGo = bp->hostOutputFrameCount[0] + bp->hostOutputFrameCount[1]; /* relies on assert above for input/output equivalence */
1441
1442 do{
1443 unsigned long noInputInputFrameCount;
1444 unsigned long *hostInputFrameCount;
1445 PaUtilChannelDescriptor *hostInputChannels;
1446 unsigned long noOutputOutputFrameCount;
1447 unsigned long *hostOutputFrameCount;
1448 PaUtilChannelDescriptor *hostOutputChannels;
1449 unsigned long framesProcessedThisIteration;
1450
1451 if( !bp->hostInputChannels[0][0].data )
1452 {
1453 /* no input was supplied (see PaUtil_SetNoInput)
1454 NonAdaptingProcess knows how to deal with this
1455 */
1456 noInputInputFrameCount = framesToGo;
1457 hostInputFrameCount = &noInputInputFrameCount;
1458 hostInputChannels = 0;
1459 }
1460 else if( bp->hostInputFrameCount[0] != 0 )
1461 {
1462 hostInputFrameCount = &bp->hostInputFrameCount[0];
1463 hostInputChannels = bp->hostInputChannels[0];
1464 }
1465 else
1466 {
1467 hostInputFrameCount = &bp->hostInputFrameCount[1];
1468 hostInputChannels = bp->hostInputChannels[1];
1469 }
1470
1471 if( !bp->hostOutputChannels[0][0].data )
1472 {
1473 /* no output was supplied (see PaUtil_SetNoOutput)
1474 NonAdaptingProcess knows how to deal with this
1475 */
1476 noOutputOutputFrameCount = framesToGo;
1477 hostOutputFrameCount = &noOutputOutputFrameCount;
1478 hostOutputChannels = 0;
1479 }
1480 if( bp->hostOutputFrameCount[0] != 0 )
1481 {
1482 hostOutputFrameCount = &bp->hostOutputFrameCount[0];
1483 hostOutputChannels = bp->hostOutputChannels[0];
1484 }
1485 else
1486 {
1487 hostOutputFrameCount = &bp->hostOutputFrameCount[1];
1488 hostOutputChannels = bp->hostOutputChannels[1];
1489 }
1490
1491 framesToProcess = PA_MIN_( *hostInputFrameCount,
1492 *hostOutputFrameCount );
1493
1494 assert( framesToProcess != 0 );
1495
1496 framesProcessedThisIteration = NonAdaptingProcess( bp, streamCallbackResult,
1497 hostInputChannels, hostOutputChannels,
1498 framesToProcess );
1499
1500 *hostInputFrameCount -= framesProcessedThisIteration;
1501 *hostOutputFrameCount -= framesProcessedThisIteration;
1502
1503 framesProcessed += framesProcessedThisIteration;
1504 framesToGo -= framesProcessedThisIteration;
1505
1506 }while( framesToGo > 0 );
1507 }
1508 else
1509 {
1510 /* half duplex non-adapting process, just process 1st and 2nd buffer */
1511 /* process first buffer */
1512
1513 framesToProcess = (bp->inputChannelCount != 0)
1514 ? bp->hostInputFrameCount[0]
1515 : bp->hostOutputFrameCount[0];
1516
1517 framesProcessed = NonAdaptingProcess( bp, streamCallbackResult,
1518 bp->hostInputChannels[0], bp->hostOutputChannels[0],
1519 framesToProcess );
1520
1521 /* process second buffer if provided */
1522
1523 framesToProcess = (bp->inputChannelCount != 0)
1524 ? bp->hostInputFrameCount[1]
1525 : bp->hostOutputFrameCount[1];
1526 if( framesToProcess > 0 )
1527 {
1528 framesProcessed += NonAdaptingProcess( bp, streamCallbackResult,
1529 bp->hostInputChannels[1], bp->hostOutputChannels[1],
1530 framesToProcess );
1531 }
1532 }
1533 }
1534 else /* block adaption necessary*/
1535 {
1536
1537 if( bp->inputChannelCount != 0 && bp->outputChannelCount != 0 )
1538 {
1539 /* full duplex */
1540
1541 if( bp->hostBufferSizeMode == paUtilVariableHostBufferSizePartialUsageAllowed )
1542 {
1543 framesProcessed = AdaptingProcess( bp, streamCallbackResult,
1544 0 /* dont process partial user buffers */ );
1545 }
1546 else
1547 {
1548 framesProcessed = AdaptingProcess( bp, streamCallbackResult,
1549 1 /* process partial user buffers */ );
1550 }
1551 }
1552 else if( bp->inputChannelCount != 0 )
1553 {
1554 /* input only */
1555 framesToProcess = bp->hostInputFrameCount[0];
1556
1557 framesProcessed = AdaptingInputOnlyProcess( bp, streamCallbackResult,
1558 bp->hostInputChannels[0], framesToProcess );
1559
1560 framesToProcess = bp->hostInputFrameCount[1];
1561 if( framesToProcess > 0 )
1562 {
1563 framesProcessed += AdaptingInputOnlyProcess( bp, streamCallbackResult,
1564 bp->hostInputChannels[1], framesToProcess );
1565 }
1566 }
1567 else
1568 {
1569 /* output only */
1570 framesToProcess = bp->hostOutputFrameCount[0];
1571
1572 framesProcessed = AdaptingOutputOnlyProcess( bp, streamCallbackResult,
1573 bp->hostOutputChannels[0], framesToProcess );
1574
1575 framesToProcess = bp->hostOutputFrameCount[1];
1576 if( framesToProcess > 0 )
1577 {
1578 framesProcessed += AdaptingOutputOnlyProcess( bp, streamCallbackResult,
1579 bp->hostOutputChannels[1], framesToProcess );
1580 }
1581 }
1582 }
1583
1584 return framesProcessed;
1585 }
1586
1587
1588 int PaUtil_IsBufferProcessorOutputEmpty( PaUtilBufferProcessor* bp )
1589 {
1590 return (bp->framesInTempOutputBuffer) ? 0 : 1;
1591 }
1592
1593
1594 unsigned long PaUtil_CopyInput( PaUtilBufferProcessor* bp,
1595 void **buffer, unsigned long frameCount )
1596 {
1597 PaUtilChannelDescriptor *hostInputChannels;
1598 unsigned int framesToCopy;
1599 unsigned char *destBytePtr;
1600 void **nonInterleavedDestPtrs;
1601 unsigned int destSampleStrideSamples; /* stride from one sample to the next within a channel, in samples */
1602 unsigned int destChannelStrideBytes; /* stride from one channel to the next, in bytes */
1603 unsigned int i;
1604
1605 hostInputChannels = bp->hostInputChannels[0];
1606 framesToCopy = PA_MIN_( bp->hostInputFrameCount[0], frameCount );
1607
1608 if( bp->userInputIsInterleaved )
1609 {
1610 destBytePtr = (unsigned char*)*buffer;
1611
1612 destSampleStrideSamples = bp->inputChannelCount;
1613 destChannelStrideBytes = bp->bytesPerUserInputSample;
1614
1615 for( i=0; i<bp->inputChannelCount; ++i )
1616 {
1617 bp->inputConverter( destBytePtr, destSampleStrideSamples,
1618 hostInputChannels[i].data,
1619 hostInputChannels[i].stride,
1620 framesToCopy, &bp->ditherGenerator );
1621
1622 destBytePtr += destChannelStrideBytes; /* skip to next source channel */
1623
1624 /* advance dest ptr for next iteration */
1625 hostInputChannels[i].data = ((unsigned char*)hostInputChannels[i].data) +
1626 framesToCopy * hostInputChannels[i].stride * bp->bytesPerHostInputSample;
1627 }
1628
1629 /* advance callers dest pointer (buffer) */
1630 *buffer = ((unsigned char *)*buffer) +
1631 framesToCopy * bp->inputChannelCount * bp->bytesPerUserInputSample;
1632 }
1633 else
1634 {
1635 /* user input is not interleaved */
1636
1637 nonInterleavedDestPtrs = (void**)*buffer;
1638
1639 destSampleStrideSamples = 1;
1640
1641 for( i=0; i<bp->inputChannelCount; ++i )
1642 {
1643 destBytePtr = (unsigned char*)nonInterleavedDestPtrs[i];
1644
1645 bp->inputConverter( destBytePtr, destSampleStrideSamples,
1646 hostInputChannels[i].data,
1647 hostInputChannels[i].stride,
1648 framesToCopy, &bp->ditherGenerator );
1649
1650 /* advance callers dest pointer (nonInterleavedDestPtrs[i]) */
1651 destBytePtr += bp->bytesPerUserInputSample * framesToCopy;
1652 nonInterleavedDestPtrs[i] = destBytePtr;
1653
1654 /* advance dest ptr for next iteration */
1655 hostInputChannels[i].data = ((unsigned char*)hostInputChannels[i].data) +
1656 framesToCopy * hostInputChannels[i].stride * bp->bytesPerHostInputSample;
1657 }
1658 }
1659
1660 bp->hostInputFrameCount[0] -= framesToCopy;
1661
1662 return framesToCopy;
1663 }
1664
1665 unsigned long PaUtil_CopyOutput( PaUtilBufferProcessor* bp,
1666 const void ** buffer, unsigned long frameCount )
1667 {
1668 PaUtilChannelDescriptor *hostOutputChannels;
1669 unsigned int framesToCopy;
1670 unsigned char *srcBytePtr;
1671 void **nonInterleavedSrcPtrs;
1672 unsigned int srcSampleStrideSamples; /* stride from one sample to the next within a channel, in samples */
1673 unsigned int srcChannelStrideBytes; /* stride from one channel to the next, in bytes */
1674 unsigned int i;
1675
1676 hostOutputChannels = bp->hostOutputChannels[0];
1677 framesToCopy = PA_MIN_( bp->hostOutputFrameCount[0], frameCount );
1678
1679 if( bp->userOutputIsInterleaved )
1680 {
1681 srcBytePtr = (unsigned char*)*buffer;
1682
1683 srcSampleStrideSamples = bp->outputChannelCount;
1684 srcChannelStrideBytes = bp->bytesPerUserOutputSample;
1685
1686 for( i=0; i<bp->outputChannelCount; ++i )
1687 {
1688 bp->outputConverter( hostOutputChannels[i].data,
1689 hostOutputChannels[i].stride,
1690 srcBytePtr, srcSampleStrideSamples,
1691 framesToCopy, &bp->ditherGenerator );
1692
1693 srcBytePtr += srcChannelStrideBytes; /* skip to next source channel */
1694
1695 /* advance dest ptr for next iteration */
1696 hostOutputChannels[i].data = ((unsigned char*)hostOutputChannels[i].data) +
1697 framesToCopy * hostOutputChannels[i].stride * bp->bytesPerHostOutputSample;
1698 }
1699
1700 /* advance callers source pointer (buffer) */
1701 *buffer = ((unsigned char *)*buffer) +
1702 framesToCopy * bp->outputChannelCount * bp->bytesPerUserOutputSample;
1703
1704 }
1705 else
1706 {
1707 /* user output is not interleaved */
1708
1709 nonInterleavedSrcPtrs = (void**)*buffer;
1710
1711 srcSampleStrideSamples = 1;
1712
1713 for( i=0; i<bp->outputChannelCount; ++i )
1714 {
1715 srcBytePtr = (unsigned char*)nonInterleavedSrcPtrs[i];
1716
1717 bp->outputConverter( hostOutputChannels[i].data,
1718 hostOutputChannels[i].stride,
1719 srcBytePtr, srcSampleStrideSamples,
1720 framesToCopy, &bp->ditherGenerator );
1721
1722
1723 /* advance callers source pointer (nonInterleavedSrcPtrs[i]) */
1724 srcBytePtr += bp->bytesPerUserOutputSample * framesToCopy;
1725 nonInterleavedSrcPtrs[i] = srcBytePtr;
1726
1727 /* advance dest ptr for next iteration */
1728 hostOutputChannels[i].data = ((unsigned char*)hostOutputChannels[i].data) +
1729 framesToCopy * hostOutputChannels[i].stride * bp->bytesPerHostOutputSample;
1730 }
1731 }
1732
1733 bp->hostOutputFrameCount[0] += framesToCopy;
1734
1735 return framesToCopy;
1736 }
1737
1738
1739 unsigned long PaUtil_ZeroOutput( PaUtilBufferProcessor* bp, unsigned long frameCount )
1740 {
1741 PaUtilChannelDescriptor *hostOutputChannels;
1742 unsigned int framesToZero;
1743 unsigned int i;
1744
1745 hostOutputChannels = bp->hostOutputChannels[0];
1746 framesToZero = PA_MIN_( bp->hostOutputFrameCount[0], frameCount );
1747
1748 for( i=0; i<bp->outputChannelCount; ++i )
1749 {
1750 bp->outputZeroer( hostOutputChannels[i].data,
1751 hostOutputChannels[i].stride,
1752 framesToZero );
1753
1754
1755 /* advance dest ptr for next iteration */
1756 hostOutputChannels[i].data = ((unsigned char*)hostOutputChannels[i].data) +
1757 framesToZero * hostOutputChannels[i].stride * bp->bytesPerHostOutputSample;
1758 }
1759
1760 bp->hostOutputFrameCount[0] += framesToZero;
1761
1762 return framesToZero;
1763 }