Re: Beep ( frequency, duration)
Re: Beep ( frequency, duration)
- Subject: Re: Beep ( frequency, duration)
- From: Michael Crawford <email@hidden>
- Date: Mon, 29 Dec 2014 10:12:44 +0000
The following plays a 441 Hz tone directly into the audio driver.
If you alter this code, it is important not to block requests from the
audio driver for more samples. While this source runs in userspace,
and it supplies data from userspace, the requests for samples seem to
be coming from a hardware interrupt task.
I had the idea I could set a mutex when I ran out of samples with the
result that I scared the crap out of my dogs as well as very nearly
set my MacBook Pro on fire.
I think I wrote this for Tiger. I haven't tried it on more-recent systems.
Mike
=====
// BirthCry.cpp - The very first sound Ogg Frog made!
/* Ogg Frog: Free Music Ripping, Encoding and Backup Program
*
* Copyright (C) 2006 Michael David Crawford.
*
* Do What Thou Wilt Is The Whole Of The Law.
* (Relicensed by the copyright holder from the GPLv2.)
*/
#include <iostream>
#include <math.h>
#include <AudioToolbox/AudioToolbox.h>
#include <AudioUnit/AudioUnitProperties.h>
int OpenOutput( AudioUnit &output );
int CloseOutput( AudioUnit output );
OSStatus PCMData( void * & nbsp; &nb sp; inRefCon,
&n bsp; AudioUnitRenderActionFlags * ioActionFlags,
&n bsp; const AudioTimeStamp * inTimeStamp,
&n bsp; UInt32 &n bsp;
&nbs p; inBusNumber,
&n bsp; UInt32 &n bsp;
&nbs p; inNumberFrames,
&n bsp; AudioBufferList * & nbsp; ioData);
using std::cerr;
using std::cout;
int main( int argc, char **argv )
{
AudioUnit output;
if ( OpenOutput( output ) != 0 ){
cerr << "OpenOutput failed\n";
return -1;
}
ComponentResult cResult;
if ( noErr != ( cResult = AudioOutputUnitStart( output ) ) ){
cerr << "AudioOutputUnitStart failed\n";
return -1;
}
usleep( 10 * 1000 * 1000 );
if ( noErr != ( cResult = AudioOutputUnitStop( output ) ) ){
cerr << "AudioOutputUnitStop failed\n";
return -1;
}
if ( CloseOutput( output ) != 0 ){
cerr << "CloseOutput failed\n";
return -1;
}
return 0;
}
int OpenOutput( AudioUnit &output )
{
ComponentDescription cd;
cd.componentType = kAudioUnitType_Output;
cd.componentSubType = kAudioUnitSubType_DefaultOutput;
cd.componentManufacturer = kAudioUnitManufacturer_Apple;
cd.componentFlags = 0;
cd.componentFlagsMask = 0;
Component comp = FindNextComponent( NULL, &cd );
if ( comp == NULL )
return -1;
OSStatus result;
result = OpenAComponent( comp, &output );
if ( noErr != result )
return -1;
result = AudioUnitInitialize( output );
if ( noErr != result )
return -1;
AURenderCallbackStruct callbackData;
callbackData.inputProc = PCMData;
callbackData.inputProcRefCon = NULL;
if ( ( result = AudioUnitSetProperty( output,
&n bsp; &nbs p;
kAudioUnitProperty_SetRenderCallback,
&n bsp; &nbs p; kAudioUnitScope_Global,
&n bsp; &nbs p; 0,
&n bsp; &nbs p; &callbackData,
&n bsp; &nbs p; sizeof( callbackData ) ) )
!= noErr ){
return -1;
}
AudioStreamBasicDescription streamDesc;
UInt32 streamDescSize = sizeof( streamDesc );
if ( noErr != ( result = AudioUnitGetProperty( output,
&n bsp; &nbs p;
kAudioUnitProperty_StreamFormat,
&n bsp; &nbs p;
kAudioUnitScope_Global,
&n bsp; &nbs p; 0,
&n bsp; &nbs p; &streamDesc,
&n bsp; &nbs p;
&streamDescSize ) ) ){
return -1;
}
&n bsp; &nbs p;
return 0;
}
int CloseOutput( AudioUnit output )
{
ComponentResult cResult;
OSErr err;
if ( noErr != ( cResult = AudioUnitUninitialize( output ) ) ){
return -1;
}
if ( noErr != ( err = CloseComponent( output ) ) ){
return -1;
}
return 0;
}
OSStatus PCMData( void *inRefCon,
&n bsp; AudioUnitRenderActionFlags * ioActionFlags,
&n bsp; const AudioTimeStamp * inTimeStamp,
&n bsp; UInt32 &n bsp;
&nbs p; inBusNumber,
&n bsp; UInt32 &n bsp;
&nbs p; inNumberFrames,
&n bsp; AudioBufferList * & nbsp; ioData)
{
Float64 now = inTimeStamp->mSampleTime;
Float64 twoPi = 2 * 3.1415926535;
Float32 *sample = (Float32*)( ioData->mBuffers[ 0 ].mData );
for ( int i = 0; i < inNumberFrames; ++i ){
*sample++ = sin( twoPi * ( now / 100.0 ) );
now += 1.0;
}
return noErr;
}
Michael David Crawford, Consulting Software Engineer
email@hidden
http://www.warplife.com/mdc/
Available for Software Development in the Portland, Oregon Metropolitan
Area.
On Sun, Dec 28, 2014 at 10:23 PM, Paul Scott <email@hidden> wrote:
> On Dec 28, 2014, at 12:05 PM, Raglan T. Tiger <email@hidden> wrote:
>>
>> Is it possible with Cocoa to generate a tone of a specified frequency and duration to play synchronously?
>>
>>
>> -rags
>>
>
> You can use Audio Unit Framework from Objective-C or Swift to generate sound on the fly. For example, a sine wave of a specific frequency and duration. There used to be a really good example (sample code) of this in the developer library, but it seems to have been removed, and some supporting functions deprecated in favor of doing things the iOS way. There is a much more complicated example in the developer library now but it's not clear how this would be used in a stand-alone program (i.e., not an AU plug-in).
>
> I've created an application for both iOS and OS X that uses Audio Unit Framework to generate sounds. If I can find the time to rip out the essentials in a stand-alone demo (I cannot give away proprietary code), then I'll pass it along. In the meantime this link is what the working code is based on:
>
> https://developer.apple.com/library/mac/documentation/AudioUnit/Reference/AudioUnit_Framework/_index.html
>
> Paul
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden