I have to play frequency which will create beep sound and after regular interval and after certain action interval reduces n after some time beep will become continous stedy sound. I have got some code on
"http://atastypixel.com/blog/2008/11/04/using-remoteio-audio-unit/"
but m not geting it m also geting error if I add AudioUnit and CoreServices framework. Please can any one help any small example code will be very helpfull.
how can I play particular frequency in iphone app.?
(13 posts) (3 voices)-
Posted 2 years ago #
-
The easiest approach is to just play back a sample that is already at the required frequency. If you use OpenAL then you can adjust the pitch from half to double. So, say you have a beep sample that you know is at 440Hz then OpenAL allows you to reproduce beeps from 220Hz - 880Hz. For the continuous tone I would use another sample, again at a known frequency but suitable for looping and play it back looped (again using OpenAL).
Any synthesiser should be capable of reproducing basic tones at known frequencies. If you have access to Sound Forge you can use Tools/Synthesis/Simple to generate simple tones.
Posted 2 years ago # -
thanx Steve Oldmeadow but i never used OPenAL. I am new to iphone programming so i dont know how to implement whatever u have told. So can you give any link where example code is given. And I dont have access to Sound Forge.
Posted 2 years ago # -
If you don't know how to use OpenAL you can check out CocosDenshion, it has a sound engine that uses OpenAL. A demo is included with cocos2d that plays back a looped tone (top left pad).
http://www.cocos2d-iphone.org/wiki/doku.php/cocosdenshion:faq
Do you know anything about sound synthesis? You could Google for a free software synthesiser that runs on whatever platform you prefer. Generating simple tones is fairly easy but I guess if you have no experience with synthesis it is a lot to learn.
Also, I just came across this. It is an open source simple synthesiser that uses audio units. It would be appropriate if all you need to do audio wise is generate the tones.
Posted 2 years ago # -
... and it appears Audacity can generate tones too. That is free and runs on Macs.
Posted 2 years ago # -
Thank you very much i'll check those links.
Posted 2 years ago # -
Search for tone generators. Audacity is pretty good but I've used the wave editor from here to make multiple sine wave clips for my first app:
http://www.audiofile-engineering.com/waveeditor/
It will cost you $80 though, but is very easy to use.
NCH has a tone generator that's something like $30, but I've never tried it:
http://www.nchsoftware.com/Posted 2 years ago # -
@ Tim : thanks for links but i dont want to generate tones i just have to create beep sound. which I have managed using audio unit.
But now problem is I am not able to repeat that sound, if I call function playing sound again sound is not cleare as it is at first time.
also I have to play it in a loop to make continuos stedy sound.
m using this code. Can u suggest anything ?#define kOutputBus 0
#define kInputBus 1
#define SAMPLE_RATE 44100vector<int> _pcm;
int _index;
AudioComponentInstance audioUnit;@implementation Sound
void generateTone(
vector<int>& pcm,
int freq,
double lengthMS,
int sampleRate,
double riseTimeMS,
double gain)
{
int numSamples = ((double) sampleRate) * lengthMS / 1000.;
int riseTimeSamples = ((double) sampleRate) * riseTimeMS / 1000.;if(gain > 1.)
gain = 1.;
if(gain < 0.)
gain = 0.;pcm.resize(numSamples);
for(int i = 0; i < numSamples; ++i)
{
double value = sin(2. * M_PI * freq * i / sampleRate);
if(i < riseTimeSamples)
value *= sin(i * M_PI / (2.0 * riseTimeSamples));
if(i > numSamples - riseTimeSamples - 1)
value *= sin(2. * M_PI * (i - (numSamples - riseTimeSamples) + riseTimeSamples)/ (4. * riseTimeSamples));pcm[i] = (int) (value * 32500.0 * gain);
pcm[i] += (pcm[i]<<16);
}}
static OSStatus playbackCallback(void *inRefCon,
AudioUnitRenderActionFlags *ioActionFlags,
const AudioTimeStamp *inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList *ioData)
{
cout<<"index = "<<_index<<endl;
cout<<"numBuffers = "<<ioData->mNumberBuffers<<endl;int totalNumberOfSamples = _pcm.size();
for(UInt32 i = 0; i < ioData->mNumberBuffers; ++i)
{
int samplesLeft = totalNumberOfSamples - _index;
int numSamples = ioData->mBuffers[i].mDataByteSize / 4;
if(samplesLeft > 0)
{
if(samplesLeft < numSamples)
{
memcpy(ioData->mBuffers[i].mData, &_pcm[_index], samplesLeft * 4);
_index += samplesLeft;
memset((char*) ioData->mBuffers[i].mData + samplesLeft * 4, 0, (numSamples - samplesLeft) * 4) ;
}
else
{
memcpy(ioData->mBuffers[i].mData, &_pcm[_index], numSamples * 4) ;
_index += numSamples;
}
}
else
memset(ioData->mBuffers[i].mData, 0, ioData->mBuffers[i].mDataByteSize);
}return noErr;
}-(id) soundinitialize
{
//generate pcm tone freq = 800, duration = 1s(1000), rise/fall time = 5msgenerateTone(_pcm, 800, 1000, SAMPLE_RATE, 5, 0.8);
_index = 0;// OSStatus status;
// AudioComponentInstance audioUnit;// Describe audio component
AudioComponentDescription desc;
desc.componentType = kAudioUnitType_Output;
desc.componentSubType = kAudioUnitSubType_RemoteIO;
desc.componentFlags = 0;
desc.componentFlagsMask = 0;
desc.componentManufacturer = kAudioUnitManufacturer_Apple;// Get component
AudioComponent inputComponent = AudioComponentFindNext(NULL, &desc);// Get audio units
status = AudioComponentInstanceNew(inputComponent, &audioUnit);
//checkStatus(status);UInt32 flag = 1;
// Enable IO for playback
status = AudioUnitSetProperty(audioUnit,
kAudioOutputUnitProperty_EnableIO,
kAudioUnitScope_Output,
kOutputBus,
&flag,
sizeof(flag));
//checkStatus(status);// Describe format
AudioStreamBasicDescription audioFormat;
audioFormat.mSampleRate = SAMPLE_RATE;
audioFormat.mFormatID = kAudioFormatLinearPCM;
audioFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
audioFormat.mFramesPerPacket = 1;
audioFormat.mChannelsPerFrame = 2;
audioFormat.mBitsPerChannel = 16;
audioFormat.mBytesPerPacket = 4;
audioFormat.mBytesPerFrame = 4;// Apply format
status = AudioUnitSetProperty(audioUnit,
kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Input,
kOutputBus,
&audioFormat,
sizeof(audioFormat));
// checkStatus(status);// Set output callback
AURenderCallbackStruct callbackStruct;
callbackStruct.inputProc = playbackCallback; // playbackcallback method called
callbackStruct.inputProcRefCon = self;
status = AudioUnitSetProperty(audioUnit,
kAudioUnitProperty_SetRenderCallback,
kAudioUnitScope_Global,
kOutputBus,
&callbackStruct,
sizeof(callbackStruct));// Initialize
status = AudioUnitInitialize(audioUnit);// Start playing
status = AudioOutputUnitStart(audioUnit);
return self;
}
Posted 2 years ago # -
Unfortunately I haven't done much with audio unit. I previously used the sound engine from crash landing to do seamless loops but that broke with the 3.0 code (it's fixable but I've since moved away from it since it wasn't recommended to continue using anyhow).
Steve's most likely your guy for further audio discussion, but if you're just needing to play a beep why not look at some of the other sound tools to simplify the approach? I think even avaudioplayer has looping capability as well.
Posted 2 years ago # -
what I have to implement is some kind of detector. So m taking reading from compass i.e current location and then move the device. after that check the location with previous recorded value. if its nearby it will play beep and as we are goin more near beep sound will be played with short interval n then continuos sound. so playing sound is related to difference in values recoded by compass. so I thought using audio unit will be easier which is not :-(
@ Steve: can you help me in this ?
Posted 2 years ago # -
@amruta - Given your requirements I would just use CocosDenshion to play two different sounds, the beep and the continuous sound. I'm sure you can get a suitable beep sound from somewhere like SoundSnap so then the only issue is creating the continuous tone, you could at least prototype with the one that ships with the CocosDenshionDemo.
If you persist with trying to use an AudioUnit approach you are going to have to deal with things like audio session interruption. For example if the user receives a phone call and then rejects it while playing your game if you do not handle the audio session interruption correctly then you will lose your audio. CocosDenshion handles this for you.
Posted 2 years ago # -
ok, But I havent used cocosDension so have to start from scratch. Any ways i'll start with cocosDension can you give me any links to get started with cocosdension
Posted 2 years ago # -
CocosDenshion comes with cocos2d. There is a demo called CocosDenshionDemo so you probably have everything you need sitting on your computer already.
Posted 2 years ago #
Reply
You must log in to post.