Re: newbie low-level (HAL?) audio recording sample revisited
Re: newbie low-level (HAL?) audio recording sample revisited
- Subject: Re: newbie low-level (HAL?) audio recording sample revisited
- From: "Jay Ridgeway" <email@hidden>
- Date: Mon, 1 Mar 2004 17:42:34 -0500 (EST)
- Organization: AOL
I didn't see a response to this request... so here's the code snippet I
wrote to test reading from the default input (in preperation for
recording).
Jay
----------------------------------------------
#include <AudioToolbox/AudioFile.h>
#include <AudioToolbox/AudioConverter.h>
#include <AudioToolbox/AudioFormat.h>
#include <libkern/OSTypes.h>
/*
*
* Opens default input and prints input level once a second
*
* gcc printdb.c -framework AudioToolbox -framework CoreAudio -framework
CoreFoundation -o printdb
*
*/
static int nChannels;
static float continualPeak = 0.0f;
static UInt32 mSafetyOffset, mBufferSizeFrames;
static AudioStreamBasicDescription mFormat;
OSStatus
InputIOProc(AudioDeviceID &nbs p; inDevice,
const AudioTimeStamp* inNow,
const AudioBufferList* inInputData,
const AudioTimeStamp* inInputTime,
AudioBufferList* & nbsp; outOutputData,
const AudioTimeStamp* inOutputTime,
void* inClientData)
{
unsigned long i, ch;
float *in;
float inSquared;
for (ch = 0; ch < inInputData->mNumberBuffers; ch++) {
in = (float *) inInputData->mBuffers[ch].mData;
for (i = 0; i < mFormat.mFramesPerPacket; i++) {
inSquared = in[i] * in[i];
if (inSquared > continualPeak) {
; continualPeak = inSquared;
}
}
}
}
void
PrintThread(void *dummy)
{
float dBvalue;
while (1) {
dBvalue = 20.0f * (float)log10(continualPeak); // convert linear
value to dB
continualPeak = 0.0f;
printf("Peak: %.2f\r", dBvalue);
fflush(stdout);
sleep(1);
}
}
int
main(int argc, char **argv)
{
AudioDeviceID input, output;
AudioBufferList *buflist;
OSStatus status;
UInt32 size, i, propSize;
pthread_t tid;
char deviceName[1024];
// Open the default input device. Other examples use Component API's
but this seems simpler.
size = sizeof(AudioDeviceID);
input = kAudioDeviceUnknown;
status =
AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice, &size,
&input);
// Get some basic data about the input.
propSize = sizeof(UInt32);
status = AudioDeviceGetProperty(input, 0, 1,
kAudioDevicePropertySafetyOffset, &propSize, &mSafetyOffset);
propSize = sizeof(UInt32);
status = AudioDeviceGetProperty(input, 0, 1,
kAudioDevicePropertyBufferFrameSize, &propSize, &mBufferSizeFrames);
propSize = 1024;
status = AudioDeviceGetProperty(input, 0, 1,
kAudioDevicePropertyDeviceName, &propSize, &deviceName);
propSize = sizeof(AudioStreamBasicDescription);
status = AudioDeviceGetProperty(input, 0, 1,
kAudioDevicePropertyStreamFormat, &propSize, &mFormat);
// Get the number of channels. This seems cooky... 2 calls to
AudioDeviceGetProperty are required to know
// how much memory to malloc.
status = AudioDeviceGetPropertyInfo(input, 0, 1,
kAudioDevicePropertyStreamConfiguration, &propSize, NULL);
buflist = (AudioBufferList *) malloc(propSize);
status = AudioDeviceGetProperty(input, 0, 1,
kAudioDevicePropertyStreamConfiguration, &propSize, buflist);
for (i = 0; i < buflist->mNumberBuffers; ++i) {
nChannels += buflist->mBuffers[i].mNumberChannels;
}
free(buflist);
printf("Input Device: %s\n", deviceName);
printf("mBufferSizeFrames: %d\n", mBufferSizeFrames);
printf("mSafetyOffset: %d\n", mSafetyOffset);
printf("mSampleRate: %.2f\n", mFormat.mSampleRate);
printf("mBytesPerPacket: %d\n", mFormat.mBytesPerPacket);
printf("mFramesPerPacket: %d\n", mFormat.mFramesPerPacket);
// For now let's just add an IO proc and gush meaningless data to the
console.
pthread_create(&tid, NULL, PrintThread, NULL);
AudioDeviceAddIOProc(input, InputIOProc, NULL);
AudioDeviceStart(input, InputIOProc);
while (1) {
usleep(1000);
}
return 0;
}
email@hidden wrote on 2/24/04, 8:42 PM:
>
I'd like to rephrase my question I posted previously. I'm looking for a
>
simple way to open an audio device, configure it and read from it on
>
MacOS X. Basically I'm looking for a short code snippet that does just
>
this. for example, on Solaris / OpenBSD / NetBSD, the following few
>
lines of code does just this. I wouldn't expect this to be a lot more
>
complicated on MacOS X either. can someone give me a hand?
--
bStandards are a vehicle of communication for producers and users. They
serve as a common language, defining quality and establishing safety
criteria. Costs are lower if procedures are standardized. Training is
simplified. And consumers accept products more readily when they can be
judged on intrinsic merit.b
-- American Society of Testing and Materials (ASTM)
_______________________________________________
coreaudio-api mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/coreaudio-api
Do not post admin requests to the list. They will be ignored.