Re: Write a sound file with sounds generated within the app
Re: Write a sound file with sounds generated within the app
- Subject: Re: Write a sound file with sounds generated within the app
- From: Brad Ford <email@hidden>
- Date: Tue, 6 Dec 2005 22:18:55 -0800
On Dec 6, 2005, at 8:49 PM, James Udo Ludtke wrote:
<SNIP>
// ===== Audio processing callback, without the code to set
frequency, tone duration, amplitude, etc. =====
OSStatus appIOProc (AudioDeviceID inDevice, const
AudioTimeStamp* inNow,
const AudioBufferList* inInputData, const AudioTimeStamp*
inInputTime,
AudioBufferList* outOutputData, const AudioTimeStamp*
inOutputTime,
void* defptr) {
sinewavedef* def = defptr;
double phase = def->phase;
double amp = def->amp;
double pan = def->pan;
freq = def->freq;
double ampz = def->ampz;
double panz = def->panz;
double freqz = def->freqz;
// more declarations go here
int numSamples = def->deviceBufferSize / def-
>deviceFormat.mBytesPerFrame;
float *out = outOutputData->mBuffers[0].mData;
// code to manipulate sweep, phase, ampz and panz goes here
float wave = sin(phase) * ampz; // generate sine wave
*out++ = wave * (1.0-panz); // left channel
*out++ = wave * panz; // right channel
}
Because, as I learned, a file can only be written from CoreAudio
from an input device, I obviously must get the sound sequences I
generate into an input device.
This is not a correct statement. You can write a file from a number
of different sources (i.e. an export from another format, a transcode
or mix-down from another file, etc.). Since your sounds are not
created in the analog world, the use of an input device to record
them to disk is unnecessary, and would introduce quality degradation.
I'm sure Bill or Doug will chime in with better ideas if they've got
'em, but I'd suggest you use the AudioConverter + AudioFile API's.
• Use your current algorithm to fill a buffer with deinterleaved
stereo samples [ Don't involve AudioDevice api's at all. Just
refactor this code ].
• pass this buffer to an audio converter which will convert it to,
say, AAC (which would be suitable for iPods) [ see AudioConverter.h:
AudioConverterNew(), AudioConverterFillComplexBuffer(), etc.]
• then the output from the AudioConverter is passed to the AudioFile
API's, where the packets are written to disk in your specified format
(i.e. .m4a - suitable for iPods). [see AudioFile.h, AudioFileCreate
(), AudioFileWritePackets(), etc.]
Using this method, you don't involve devices at all. Which is good.
You'll be able to generate a 10-minute file many times faster than
real-time.
-Brad Ford
QuickTime Engineering
1. It might be possible to set up a default input device, and the
change the default input device buffer address to be the same as
the default output device buffer address. However, I really do not
know enough about the similarities and dissimilarities of the input
and output device to know if that could be done. (Might also
stretch my coding ability beyond its limits.)
2. To repeatedly copy the output buffer content to the input
buffer, but this would use a lot of processor time. (Coding will
likely also be more complex than option 1.)
3. To modify the existing callback procedure to write to the input
buffer in addition to the output buffer, which is done now. (This
might be simplest approach, assuming it can be done.)
4. When writing a tone sequence to a file, the user does not really
have to monitor the sound. I could provide some kind of progress
indicator (which I should do anyhow) to keep the user informed that
recording is going as planned. In this case I could code an
alternate callback procedure, which writes only to the input
device--if it is possible to write to an input device. Since I
directly write directly into the buffer, this should be possible.
Looking back on what I just wrote, my preferences would be 4,
followed by 3, then 1, and as the last alternative 2.
So my question is: Can 4 be done? If not, which other alternative,
including ones I did not think of, would be the best approach.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Coreaudio-api mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Coreaudio-api mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden