Re: Beep ( frequency, duration)

2014-12-30 Thread Jens Alfke

 On Dec 29, 2014, at 2:12 PM, Michael Crawford mdcrawf...@gmail.com wrote:
 
 My input does come from another thread, reading ogg vorbis, flac, wave
 or mp3 files that write into a circular buffer.

If you're doing this for your own entertainment or education, going the 
low-level route like this is fine.
But for any serious use, I would go with AVFoundation if you want to play audio 
files. There is just so much mess and pain involved in rolling your own audio 
player. (I speak as someone who's done that. And never got it 100% crash-free 
before AVFoundation came along and allowed me to joyously delete all that code.)

—Jens
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Beep ( frequency, duration)

2014-12-30 Thread Michael Crawford
 If you're doing this for your own entertainment or education

I would tell you why I'm doing this, but to do so would just lead to
Chris Hanson bringing down the banhammer on me again.

   ;-D

Mike
Michael David Crawford, Consulting Software Engineer
mdcrawf...@gmail.com
http://www.warplife.com/mdc/

   Available for Software Development in the Portland, Oregon Metropolitan
Area.


On Tue, Dec 30, 2014 at 6:06 PM, Jens Alfke j...@mooseyard.com wrote:

 On Dec 29, 2014, at 2:12 PM, Michael Crawford mdcrawf...@gmail.com wrote:

 My input does come from another thread, reading ogg vorbis, flac, wave
 or mp3 files that write into a circular buffer.


 If you're doing this for your own entertainment or education, going the
 low-level route like this is fine.
 But for any serious use, I would go with AVFoundation if you want to play
 audio files. There is just so much mess and pain involved in rolling your
 own audio player. (I speak as someone who's done that. And never got it 100%
 crash-free before AVFoundation came along and allowed me to joyously delete
 all that code.)

 --Jens
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Beep ( frequency, duration)

2014-12-30 Thread Michael Crawford
I will mention that I stopped working on Ogg Frog because there was an
architectural problem I could not solve.  I may have the solution at
hand now but it's going to be a while before I actually try it.

The input codecs weren't the problem, I was using Xiph's code for Ogg
Vorbis and FLAC, some other widely used Open Source decoder for the
MP3.  I think I wrote my own WAVE encoder.

I like to do things the hard way.  Sometimes I discover interesting,
new things that way but it can be a huge PITA.

Mike
Michael David Crawford, Consulting Software Engineer
mdcrawf...@gmail.com
http://www.warplife.com/mdc/

   Available for Software Development in the Portland, Oregon Metropolitan
Area.


On Tue, Dec 30, 2014 at 6:30 PM, Michael Crawford mdcrawf...@gmail.com wrote:
 If you're doing this for your own entertainment or education

 I would tell you why I'm doing this, but to do so would just lead to
 Chris Hanson bringing down the banhammer on me again.

;-D

 Mike
 Michael David Crawford, Consulting Software Engineer
 mdcrawf...@gmail.com
 http://www.warplife.com/mdc/

Available for Software Development in the Portland, Oregon Metropolitan
 Area.


 On Tue, Dec 30, 2014 at 6:06 PM, Jens Alfke j...@mooseyard.com wrote:

 On Dec 29, 2014, at 2:12 PM, Michael Crawford mdcrawf...@gmail.com wrote:

 My input does come from another thread, reading ogg vorbis, flac, wave
 or mp3 files that write into a circular buffer.


 If you're doing this for your own entertainment or education, going the
 low-level route like this is fine.
 But for any serious use, I would go with AVFoundation if you want to play
 audio files. There is just so much mess and pain involved in rolling your
 own audio player. (I speak as someone who's done that. And never got it 100%
 crash-free before AVFoundation came along and allowed me to joyously delete
 all that code.)

 --Jens
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Beep ( frequency, duration)

2014-12-29 Thread Michael Crawford
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;  UInt32n bsp;
nbs p;  inBusNumber,
 n bsp;  UInt32n 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;  UInt32n bsp;
nbs p;  inBusNumber,
 n bsp;  UInt32n 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
mdcrawf...@gmail.com
http://www.warplife.com/mdc/

   Available for Software Development in the Portland, Oregon Metropolitan
Area.


On 

Re: Beep ( frequency, duration)

2014-12-29 Thread Jens Alfke

 On Dec 29, 2014, at 2:12 AM, Michael Crawford mdcrawf...@gmail.com wrote:
 
 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.

It's not quite that low-level! It's a thread, but a super-high-priority 
(real-time) one managed by CoreAudio's hardware abstraction layer (HAL). Which 
you should _not_ ever block, or system audio will get screwed up. (That's one 
of the difficulties doing audio at that level — you can't call any I/O from 
that callback, so you have to run another thread to read the audio data from a 
file/socket/whatever and queue it. Thank god we have AVFoundation now to do 
that kind of stuff.)

—Jens
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Beep ( frequency, duration)

2014-12-29 Thread Paul Scott


 On Dec 29, 2014, at 2:12 AM, Michael Crawford mdcrawf...@gmail.com wrote:
 
 OpenAComponent( comp, output )
 CloseComponent( output )

These are deprecated since — I believe — OS X 10.8.

You should probably use the iOS compatible functions:

AudioComponentInstanceNew(comp, output)
AudioComponentInstanceDispose(output)

Paul

smime.p7s
Description: S/MIME cryptographic signature
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Beep ( frequency, duration)

2014-12-29 Thread Wim Lewis

On Dec 29, 2014, at 2:12 AM, Michael Crawford mdcrawf...@gmail.com wrote:
for ( int i = 0; i  inNumberFrames; ++i ){
   *sample++ = sin( twoPi * ( now / 100.0 ) );
   now += 1.0;
}


Depending on what your overall goal is, it might be easier to use the software 
MIDI synthesizer instead of computing the samples yourself— if you want a 
bandlimited start and end, or even a music-like attack/decay/release envelope 
or a spectrum more complicated than a pure sine wave, using something like the 
AVMIDIPlayer class might free you from a bunch of realtime requirements and 
bookkeeping.

(On the other hand, I’ve never tried using it, so I don’t know if its simple 
API hides a well of frustration.)


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Beep ( frequency, duration)

2014-12-29 Thread Michael Crawford
 Depending on what your overall goal is

In this particular case, all I wanted was to learn how to spit audio
samples directly into the sound driver.

My input does come from another thread, reading ogg vorbis, flac, wave
or mp3 files that write into a circular buffer.  When the buffer is
full a mutex blocks the input thread; when the buffer is empty it
feeds silence - zeros - into the sound driver.
Michael David Crawford, Consulting Software Engineer
mdcrawf...@gmail.com
http://www.warplife.com/mdc/

   Available for Software Development in the Portland, Oregon Metropolitan
Area.


On Mon, Dec 29, 2014 at 1:14 PM, Wim Lewis w...@omnigroup.com wrote:

 On Dec 29, 2014, at 2:12 AM, Michael Crawford mdcrawf...@gmail.com wrote:
for ( int i = 0; i  inNumberFrames; ++i ){
   *sample++ = sin( twoPi * ( now / 100.0 ) );
   now += 1.0;
}


 Depending on what your overall goal is, it might be easier to use the 
 software MIDI synthesizer instead of computing the samples yourself-- if you 
 want a bandlimited start and end, or even a music-like attack/decay/release 
 envelope or a spectrum more complicated than a pure sine wave, using 
 something like the AVMIDIPlayer class might free you from a bunch of realtime 
 requirements and bookkeeping.

 (On the other hand, I've never tried using it, so I don't know if its simple 
 API hides a well of frustration.)


 ___

 Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

 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:
 https://lists.apple.com/mailman/options/cocoa-dev/mdcrawford%40gmail.com

 This email sent to mdcrawf...@gmail.com

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Beep ( frequency, duration)

2014-12-28 Thread Raglan T. Tiger
Is it possible with Cocoa to generate a tone of a specified frequency and 
duration to play synchronously?


-rags




___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Beep ( frequency, duration)

2014-12-28 Thread Jens Alfke

 On Dec 28, 2014, at 12:05 PM, Raglan T. Tiger r...@crusaderrabbit.net wrote:
 
 Is it possible with Cocoa to generate a tone of a specified frequency and 
 duration to play synchronously?

You might be able to do this with AVFoundation, or you may need to descend to 
the C-based CoreAudio APIs.

I don't think there's any built-in frequency generator component  … and in any 
case you haven't specified what waveform you want — sine? square? sawtooth? 
triangle? You'll probably want to create a wavetable (an array of samples 
that contains one complete cycle of the waveform), import that as an audio 
source, and then play it as a loop.

In any case, fasten your seat belt and prepare for some turbulence. I've found 
Apple's audio APIs to be the second most difficult and frustrating ones to work 
with on the platform (after crypto/security.)

—Jens
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Beep ( frequency, duration)

2014-12-28 Thread Paul Scott
On Dec 28, 2014, at 12:05 PM, Raglan T. Tiger r...@crusaderrabbit.net 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

smime.p7s
Description: S/MIME cryptographic signature
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com