sending PCM data
sending PCM data
- Subject: sending PCM data
- From: sheen mac <email@hidden>
- Date: Thu, 30 Nov 2006 08:49:33 -0800 (PST)
Hi All,
I am new to coreAudio.I am developing an AudioChat application.
I did the record part from a sample program posted in this mailing list.
I converted the float PCM data to NSData and send to network.
But in the network,it failed.But if both application in one mechine
it works good.I attached the code here.
Kindly help me.
Thanks in advance,
Sheen
#import "MyController.h"
#import <CoreAudio/CoreAudio.h>
static AudioDeviceID Input_device;
static AudioDeviceID Output_device;
static float* Pcms = NULL;
static float* Pcms2 = NULL;
static int Max_units = 5000000; // 5 million PCM samples
static int Units_recorded = 0;
static int Units_played = 0;
OSStatus recordCallback(
AudioDeviceID device,
const AudioTimeStamp* now,
const AudioBufferList* input_data,
const AudioTimeStamp* input_time,
AudioBufferList* output_data,
const AudioTimeStamp*
output_time,
void* client_data
)
{
int num_of_units = 0;
int i = 0;
float* new_data = NULL;
new_data = (float*)(input_data->mBuffers[0].mData);
num_of_units = input_data->mBuffers[0].mDataByteSize / sizeof(float);
for (i = 0; (i < num_of_units) && (Units_recorded < Max_units); i++) {
Pcms[Units_recorded] = new_data[i];
Units_recorded += 1;
}
return kAudioHardwareNoError;
}
OSStatus playCallback(
AudioDeviceID device,
const AudioTimeStamp* now,
const AudioBufferList* input_data,
const AudioTimeStamp* input_time,
AudioBufferList* output_data,
const AudioTimeStamp* output_time,
void* client_data
)
{
float* dst = NULL; // destination for the data
int num_of_units = 0; // number of sample points buffer can take
int num_to_play = 0; // number of sample units to play
int num_of_channels = 2; // number of channels output
supports
int i = 0; // index
int j = 0; // index into destination buffer.
dst = (float*)(output_data->mBuffers[0].mData);
num_of_units = output_data->mBuffers[0].mDataByteSize / sizeof(float);
num_to_play = num_of_units / num_of_channels;
for (i = 0; i < num_to_play; i++) {
if (Units_played < Units_recorded) {
dst[j++] = Pcms2[Units_played]; // recorded stuff
dst[j++] = Pcms2[Units_played];
Units_played += 1;
}
else {
dst[j++] = 0.0; // silence
dst[j++] = 0.0;
}
}
return kAudioHardwareNoError;
}
@implementation MyController
- init
{
OSStatus err;
UInt32 property_size;
if (self = [super init]) {
// do local initialization
property_size =
sizeof(AudioDeviceID);
err = AudioHardwareGetProperty(
kAudioHardwarePropertyDefaultInputDevice,
&property_size, &Input_device);
if (err || (Input_device == kAudioDeviceUnknown)) {
return self;
}
err = AudioDeviceAddIOProc(Input_device, recordCallback, NULL);
if (err) {
return
self;
}
property_size = sizeof(AudioDeviceID);
err = AudioHardwareGetProperty(
kAudioHardwarePropertyDefaultOutputDevice,
&property_size, &Output_device);
if (err || (Output_device == kAudioDeviceUnknown)) {
return self;
}
err =
AudioDeviceAddIOProc(Output_device, playCallback, NULL);
if (err) {
return self;
}
Pcms = (float*)malloc(Max_units * sizeof(float));
Pcms2 = (float*)malloc(Max_units * sizeof(float));
}
NSLog(@"Set up audio");
return self;
}
- (void)awakeFromNib
{
[play_button setButtonType: NSToggleButton];
[record_button setButtonType: NSToggleButton];
}
- (IBAction)pressPlay:(id)sender
{
OSStatus err;
if ([sender state] == 1) {
[record_button setEnabled: NO];
Units_played = 0;
NSData *recData=[NSData dataWithBytes:Pcms length:(Units_recorded *
sizeof(float))];
[recData getBytes:Pcms2];
err = AudioDeviceStart(Output_device, playCallback);
if (err) {
NSLog(@"Could not start output device");
return;
}
}
else {
[record_button setEnabled: YES];// re-enable the record button
// stop the play callback thread
err = AudioDeviceStop(Output_device, playCallback);
if (err) {
NSLog(@"Could not stop input device");
return;
}
}
}
- (IBAction)pressRecord:(id)sender
{
OSStatus err;
if ([sender state] == 1) {
[play_button setEnabled: NO]; // disable Play while recording
Units_recorded = 0; // reset index to beginning
// start recording callback thread
err = AudioDeviceStart(Input_device, recordCallback);
if (err) {
NSLog(@"Could not start recording device");
return;
}
}
else {
[play_button setEnabled: YES]; // re-enable Play button
// stop the recording
callback thread
err = AudioDeviceStop(Input_device, recordCallback);
if (err) {
NSLog(@"Could not stop recording device");
return;
}
}
}
@end
Want to start your own business? Learn how on
Yahoo! Small Business. _______________________________________________
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