• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
simply record and play audio from buffer problem
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

simply record and play audio from buffer problem


  • Subject: simply record and play audio from buffer problem
  • From: Xiaofeng Li <email@hidden>
  • Date: Wed, 25 Apr 2012 12:06:25 -0500

Hi, There,

I wrote the following code to play back the audio data I recorded in a
buffer. However, it works fine except it repeats twice. Any idea?
I simplified the code as the following, and it is working. I am using
a Mac pro and OSX 10.6.


#include <stdlib.h>
#include <math.h>
#include <AudioToolbox/AudioQueue.h>
#include <CoreAudio/CoreAudioTypes.h>
#include <CoreFoundation/CFRunLoop.h>
#include "link.h"
//my own header file


#define NUM_CHANNELS 2
#define NUM_BUFFERS 1

//#define BUFFER_SIZE 4096

#define SAMPLE_TYPE short int
#define MAX_NUMBER 32767
#define SAMPLE_RATE 8000



unsigned int count;
short int *outbuf=NULL;		//save audio data obtained from the link
int size=0;					//length of data
int error=0;				// 0 means no error
int channel_num=0;


void callback(void *custom_data, AudioQueueRef queue,
AudioQueueBufferRef buffer)
{
    SAMPLE_TYPE *casted_buffer = (SAMPLE_TYPE *)buffer->mAudioData;

	unsigned int i, j;


		for (i = 0; i < size/sizeof(SAMPLE_TYPE) ; i += channel_num)
		{

			for (j = 0; j < channel_num; j++)
			{
				casted_buffer[i + j] = outbuf[i + j];

			}

			count++;
		}



    AudioQueueEnqueueBuffer(queue, buffer, 0, NULL);



	if ( count > (size / channel_num /sizeof(short int)) )
    {
		AudioQueueStop(queue, false);
		AudioQueueDispose(queue, false);
		CFRunLoopStop(CFRunLoopGetCurrent());
    }


}



void playFromBuffer(int channels, int samplerate)
{
    count = 0;
    unsigned int i;			//for loop interator


    AudioStreamBasicDescription format;
    AudioQueueRef queue;
    AudioQueueBufferRef buffers[NUM_BUFFERS];
    channel_num=channels;
	outbuf=NULL;

    format.mSampleRate       = samplerate;
    format.mFormatID         = kAudioFormatLinearPCM;
    format.mFormatFlags      = kLinearPCMFormatFlagIsSignedInteger |
kAudioFormatFlagIsPacked;
    format.mBitsPerChannel   = 8* sizeof(SAMPLE_TYPE);
    format.mChannelsPerFrame = channels;
    format.mBytesPerFrame    = sizeof(SAMPLE_TYPE) * channel_num;
    format.mFramesPerPacket  = 1;
    format.mBytesPerPacket   = format.mBytesPerFrame * format.mFramesPerPacket;
    format.mReserved         = 0;



//get data from link

	if(outbuf==NULL)
	{
		if(!GetInteger16List(link,&outbuf,&size))
		{
			PutString(link,"PutInteger16List has some problem.");
		}
	}

	else
	{
		PutString(link,"outbuf error!");
	}


    AudioQueueNewOutput(&format, callback, NULL,
CFRunLoopGetCurrent(), kCFRunLoopCommonModes, 0, &queue);


	if(size==0)
	{
		PutString(link,"size is wrong");
	}


    for (i = 0; i < NUM_BUFFERS; i++)
    {
			AudioQueueAllocateBuffer(queue, size, &buffers[i]);

			buffers[i]->mAudioDataByteSize = size;

			callback(NULL, queue, buffers[i]);
    }



    AudioQueueStart(queue, NULL);


    CFRunLoopRun();


        //release memory, internally there is a free(outbuf)

	ReleaseInteger16List(link,outbuf,size);

	outbuf=NULL;

	PutString(link, "True");
}


Thanks!


--
Xiaofeng Li
 _______________________________________________
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

  • Prev by Date: Re: Dropped Buffers from mouse-menu tracking
  • Next by Date: Re: vDSP and iOS render callback
  • Previous by thread: Re: Dropped Buffers from mouse-menu tracking
  • Next by thread: AU Reverb2 parameters
  • Index(es):
    • Date
    • Thread