I'm having a bash at learning Objective C, and seem to be doing OK so
far in general. However, the project I have decided to learn on (have to
do something "real" or I lose interest) is to create a tone generator
for a friend who is a sound engineer. I need to play tones at specified
frequencies, preferably with choice of waveform (sine, square etc), and
I'd like to generate white and pink noise as well. But let's stick with
producing a simple tone for now.
I'm hitting an absolute brick wall finding out about this stuff. I
suspect I probably need to use Core Audio, but everything I can find
(which isn't much) seems far too in-depth and complicated just to
generate a specified tone. Coming from a Java background (though to be
fair I haven't used sound in Java, so it may not be easy there either),
I'd expect to do something along the lines of instantiating a Sound
object, setting frequency and other parameters and letting it play. Is
that wishful thinking?
I have come across James McCartney's SineWav program, but again it seems
to be operating at a lower level than I would like. Is it really
necessary to be worrying about 2π radians and device sampling rates in
order to make a simple beep?
Thanks for any help you can give,
Pete
Reinder Verlinde - 25 Oct 2005 23:32 GMT
In article <3s7nd5Fn0419U1@individual.net>,
Pete Verdon <usenet@verdonet.organisation.unitedkingdom.invalid>
wrote:
> I'm having a bash at learning Objective C, and seem to be doing OK so
> far in general. However, the project I have decided to learn on (have to
[quoted text clipped - 3 lines]
> I'd like to generate white and pink noise as well. But let's stick with
> producing a simple tone for now.
You should download 'Noise' from <http://www.blackholemedia.com/noise/>
It does the pink noise part, and ships with source code. It's license
contains the following:
English non-authoritative interpretation of the license:
This is pretty much the BSD license. This means you can take the
source and do whatever you please with it. While it isnot a
requirement of the license, we would appreciate a mention in the
credits of your application if you use our source.
> I'm hitting an absolute brick wall finding out about this stuff. I
> suspect I probably need to use Core Audio, but everything I can find
[quoted text clipped - 4 lines]
> object, setting frequency and other parameters and letting it play. Is
> that wishful thinking?
I think that is too much 80's thinking. Nowadays, simple sound playing
means making a call 'play this sound', where the system finds out
whether a sound is .wav, .MP3, or whatever.
You may be able to generate a sine or square wave using MIDI output, but
that will be just as simple (or hard) as generating a piano sound.
Reinder
Patrick Machielse - 26 Oct 2005 00:34 GMT
> I'm hitting an absolute brick wall finding out about this stuff. I
> suspect I probably need to use Core Audio, but everything I can find
> (which isn't much) seems far too in-depth and complicated just to
> generate a specified tone.
It's true, it is complicated. No simple Cocoa class for generating sine
waves (or recording audio for that matter). Alas.
> I have come across James McCartney's SineWav program, but again it seems
> to be operating at a lower level than I would like. Is it really
> necessary to be worrying about 2π radians and device sampling rates in
> order to make a simple beep?
yes. I used SineWave to 'learn' how I could use CoreAudio, it seems to
be one of the few resources. CoreAudio is probably a great technical
achievement, but programming it is a bear. It's a procedural
programmer's dream. Part of the problem seems to be the utter
'nerdiness' of the programmers responsable. If you've ever whitnessed an
audio related talk at WWDC you know what I mean. I didn't understand a
word of it. Also, good introductory texts are hard to find /
non-existant.
So the learning curve is steep, but it will work in the end. Mind you,
the 'Objective-C style' in that particular project is rather
non-standard, so beware.
patrick
Patrick Machielse - 26 Oct 2005 09:58 GMT
> Thanks for any help you can give,
just spotted this:
<http://www.blackholemedia.com/noise/>
have not tried it myself, but it includes the source.
patrick
Pete Verdon - 26 Oct 2005 11:15 GMT
> just spotted this:
> <http://www.blackholemedia.com/noise/>
>
> have not tried it myself, but it includes the source.
Yup, Reinder already pointed that out :-)
OK, it seems that I really do have to grovel about in the low level
stuff just to make a tone, which seems absurd. But on the other hand
doing it will give me some more practice in writing Objective C, so it's
not all bad. Thanks for your help.
Pete
Patrick Machielse - 26 Oct 2005 11:55 GMT
> > just spotted this:
> > <http://www.blackholemedia.com/noise/>
> >
> > have not tried it myself, but it includes the source.
>
> Yup, Reinder already pointed that out :-)
Oh rats! Just noteced it on some blog today...
> OK, it seems that I really do have to grovel about in the low level
> stuff just to make a tone, which seems absurd. But on the other hand
> doing it will give me some more practice in writing Objective C, so it's
> not all bad. Thanks for your help.
Consider making this your 2nd or 3rd project. This subject of this
project is probably one of the worst areas for learning Objective-C, but
if you like a challenge... :-)
patrick
Pete Verdon - 26 Oct 2005 22:41 GMT
> Consider making this your 2nd or 3rd project. This subject of this
> project is probably one of the worst areas for learning Objective-C, but
> if you like a challenge... :-)
Not really, but as I said in my first post, I am doing this for a
reason. My friend works as a sound engineer and needs a tone generator
to test equipment. His department has one, but only one, and at around
£150 he's not about to buy his own rather than just go and find the
company one. However, he apparently uses his powerbook all the time at
work (I have no idea what for) and it would be handy to just plug it in
and use a program made to look like the hardware device.
Pete
Peter Ammon - 29 Oct 2005 06:07 GMT
> I'm having a bash at learning Objective C, and seem to be doing OK so
> far in general. However, the project I have decided to learn on (have to
[quoted text clipped - 6 lines]
> I'm hitting an absolute brick wall finding out about this stuff. I
> suspect I probably need to use Core Audio
Nah.
> I have come across James McCartney's SineWav program, but again it seems
> to be operating at a lower level than I would like. Is it really
> necessary to be worrying about 2π radians and device sampling rates in
> order to make a simple beep?
Yeah, but it's easy. I assume you already know how a sound waveform
works, so you already know where pi figures in.
NSSound can play AIFF data, and the AIFF format is very straightforward.
Here's a simple sine wave tone generator I threw together. It expects
one parameter (a frequency) on the command line. Try, say, 500.
-Peter
#include <Cocoa/Cocoa.h>
int main(int argc, char* argv[]) {
double frequency = strtod(argv[1], NULL);
struct Simple_AIFF_File {
/* FORM chunk */
unsigned int formID;
int formChunkSize;
unsigned int formType;
/* COMM chunk */
unsigned int commID;
int commChunkSize;
short numChannels;
unsigned int numSampleFrames;
short sampleSize;
extended80 sampleRate;
/* SSND chunk */
unsigned int ssndID;
int ssndChunkSize;
unsigned int offset;
unsigned int blockSize;
int soundData[];
} __attribute__ ((__packed__));
const unsigned int sampleRate = 48000;
const unsigned int channels = 1;
const unsigned int bytesPerFrame = channels * sizeof(unsigned int);
const unsigned int seconds = 4;
const unsigned int dataSize = seconds * sampleRate * channels *
bytesPerFrame;
const unsigned int totalSize = dataSize + sizeof(struct Simple_AIFF_File);
unsigned int i;
struct Simple_AIFF_File* aiff = malloc(totalSize);
if (! aiff) {
printf("Out of memory allocating %lu bytes!\n", totalSize);
return EXIT_FAILURE;
}
aiff->formID = 'FORM';
aiff->formChunkSize = totalSize - offsetof(struct Simple_AIFF_File,
formType);
aiff->formType = 'AIFF';
aiff->commID = 'COMM';
aiff->commChunkSize = 18;
aiff->numChannels = 1;
aiff->numSampleFrames = seconds * sampleRate;
aiff->sampleSize = 32;
double sampleRateDouble = (double)sampleRate;
dtox80(&sampleRateDouble, &aiff->sampleRate);
aiff->ssndID = 'SSND';
aiff->ssndChunkSize = dataSize + 8;
aiff->offset = 0;
aiff->blockSize = 0;
for (i=0; i < seconds * sampleRate * channels; i++) {
aiff->soundData[i] = INT_MAX * sin(i * M_PI * 2. * (frequency /
(double)sampleRate) );
}
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSData* data = [[NSData alloc] initWithBytesNoCopy:aiff length:totalSize];
NSSound* sound = [[NSSound alloc] initWithData:data];
[data release];
[sound play];
sleep(seconds);
[sound release];
[pool release];
return 0;
}
Mike - 29 Oct 2005 18:12 GMT
There are some higher-level music/sound libraries available, which may
have classes of the sort you're looking for (i haven't spent much time
with them myself):
http://musickit.sourceforge.net/ (probably your best bet)
http://supercollider.sourceforge.net/ (requires you to run a
supercollider daemon in addition to your program, but i'm pretty sure
it gives you functions like brownian/pink noise, simple tone
generators, etc.)