[Portaudio] a few ?'s about patest_record

Ross Bencina rbencina@iprimus.com.au
Mon, 25 Nov 2002 18:47:33 +1100

Hi Nick,

I'm not really familiar with patest_record but I'll have a go at answering
your questions.

First thing to note is that the files with patest_ in the name are more
aimed at testing than as examples, so they're likeley to contain slightly
confusing stuff associated with testing. Perhaps one day we'll have separate
test and example directories.

> Working with audio is a little new to me, so please be gentle.  I'm
> working on building a real time spectrum analyzer.  I've downloaded
> portaudio and played with the test files and it seems to work pretty
> great.  The open source community never ceases to amaze me.  Anyway...
> I'm thinking that I can rewrite the simple patest_record to continually
> pull samples into a big circular buffer via "recordCallback" and perform
> analysis on the buffer as quickly as possible via "main."  (I hope that
> makes sense)

Sounds like a reasonable strategy. At some stage you'll probably want to set
up another thread to do the analysis.

> Here are my questions...
> I'm a little confused about the sample format,  these sample types are
> at the top of the file
> #if 0
> #define PA_SAMPLE_TYPE  paFloat32
> typedef float SAMPLE;
> #define SAMPLE_SILENCE  (0.0f)
> #elif 0
> #define PA_SAMPLE_TYPE  paInt16
> typedef short SAMPLE;
> #define SAMPLE_SILENCE  (0)
> #elif 0
> #define PA_SAMPLE_TYPE  paInt8
> typedef char SAMPLE;
> #define SAMPLE_SILENCE  (0)
> #else
> #define PA_SAMPLE_TYPE  paUInt8
> typedef unsigned char SAMPLE;
> #define SAMPLE_SILENCE  (128)
> #endif
> But I don't see one of the sample types explicitly chosen in the code.
> Is this chosen somewhere for you?

I think the chail of if/elifs falls through to the last case sinxe the rest
are all "if 0". Depending on whether your analysis code works on floats or
ints I'd suggest replacing the above code with either:

#define PA_SAMPLE_TYPE  paFloat32
typedef float SAMPLE;


#define PA_SAMPLE_TYPE  paInt16
typedef short SAMPLE;

or even just removing the #define and typdef and using the values directly
in your code. This indirection is just there to make testing different
sample formats easier i think.

> I'd also like to verify that the callback function gets called once and
> (assuming everything goes ok) returns when it has obtained it's complete
> sample as specified in maxFrameIndex...  It doesn't get called, update
> "framesLeft," get called again, etc...  Correct?

You are correct. The callback gets called once for each block of samples.

> A frame in the raw audio format that portaudio is recording in is merely
> consists of a single sample for each channel, is this correct too?

That's correct. In V18, the frames are always stored in an interleaved
format, so for stereo (L-left, R-right):

one frame:

a buffer of 10 interleaved frames:

In V19 we also support non-interleaved buffers as an option (interleaved
frames are still supported):

Best wishes,