Tutorial :A simple way to generate tones / sine waves on mac? (ruby would be nice)



Question:

I'm writing a program that involves playing back sine waves and combinations of sine waves... it must run on Mac, and I'm looking for a simple API that I can use. I'm open to ObjC, C, C++, Ruby, Python, etc.... I don't care what language as long as it's just a few lines of code. But Ruby would be nice :-)

On Linux you can write to /dev/dsp, /dev/sound etc. but not on mac. I know how to generate a sine wave, but the problem is getting the PCM samples that I create to the audio hardware. I'm aware of the example code in /Developer/Examples/CoreAudio/SimpleSDK/DefaultOutputUnit but it's several hundred lines of ridiculous Core Audio / AudioUnit code and I want something simple to work with.


Solution:1

one way is to use PyAudio

import pyaudio, array, math    p = pyaudio.PyAudio()  stream = p.open(rate=44100, channels=1, format=pyaudio.paFloat32, output=True)  stream.write(array.array('f',      (.25 * math.sin(i / 10.) for i in range(44100))).tostring())  stream.close()  p.terminate()  

not very clean code, but it works

PyAudio is not standard, but is available through easy_install (I used python2.5)


Solution:2

You could dump your PCM to a wave file on disk and use /usr/bin/afplay which is a standard binary on OSX.


Solution:3

Edit: Bloopsaphone!

Depending on your needs, CSound might be something to look into, though it probably wasn't exactly what you had in mind.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »