Unix Programming - Writing tune to /dev/dsp

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > May 2006 > Writing tune to /dev/dsp





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Writing tune to /dev/dsp
Miko

2006-05-22, 7:15 pm

Does anyone know a good document on how I could
write a c program that plays a tune by writing to
/dev/dsp? By that I mean that the tune should be
generated in the program
Logan Shaw

2006-05-23, 1:17 am

Miko wrote:
> Does anyone know a good document on how I could
> write a c program that plays a tune by writing to
> /dev/dsp? By that I mean that the tune should be
> generated in the program


As far as I know, /dev/dsp can be set (probably with some
ioctl() or something) to be simply a linear PCM device.
In that case, the writing to /dev/dsp part is pretty simple,
and the difficulty is getting the right samples to write.

The simplest thing to do is to start with a square wave.
If you have a 44.1 kHz sampling rate and you want to play
a 441 Hz tone, all you have to do is pick two values in
the range (the range being -32768 to +32767 if you use signed
16-bit integer samples), like maybe -5000 and +5000, and play
one for 50 samples, then the other for 50 samples, then back
to the first. That way, you repeat after 100 samples, which
means you repeat 441 times a second when using a 44100 Hz
sampling rate.

Having said that, you probably want to get a little more
sophisticated at some point and change to a note at a
different pitch. If you want to fit in with Western music,
you'll probably want to stick with the 12-tone scale. That's
pretty easy to handle since in a standard scale (equal
temperament), the notes are each related by the ratio 2^(1/12).
So, if you have a 440 Hz tone (an A), then an A# (or B-flat)
is 440 * 2^(1/12) Hz, or about 466.1637 Hz. As you can see,
you're likely going to get into floating point if you intend
to keep the pitches accurate. You'll notice that one nice
property of the relative pitches of notes is that a note one
octave above another in the 12-tone scale would be a ratio of
2^(12/12), which reduces to 2, i.e. double. So the note an
octave above 440 Hz is 880 Hz.

That should be enough to get you going. A nice simple first
project would be a program that reads a data file and plays
notes according to what's in the data file. The data file
could simply be a text file where each line contains a frequency
in Hz and a duration in milliseconds. You could play a square
wave at that frequency for the recommended duration. By
hand-crafting the file, it would be fairly straightforward
to have the system play a recognizable tune.

The next step is polyphony (i.e. playing more than one note
at a time). This is a bit tougher. Basically, you need to
generate multiple waves and then mix them together. Luckily,
mixing waves together is as simple as adding them. The only
caveat is that you need to make sure that when you add all
your waves together, you don't overflow the 16-bit integers.
If you do, it will sound awful. :-)

- Logan
Miko

2006-05-23, 7:17 pm

Logan Shaw wrote:
> Miko wrote:
>
> As far as I know, /dev/dsp can be set (probably with some
> ioctl() or something) to be simply a linear PCM device.
> In that case, the writing to /dev/dsp part is pretty simple,
> and the difficulty is getting the right samples to write.
>
> [...]
>


Thank you very much for your help!

I have two other quetions

1. What is the proper way to quote when you want to send a
simple thanks like this? Now I just quoted the first part and
snipped the rest, but the thanks is for the whole reply of
course.

2. What is the difference in sound of square wave, sine wave and
the third, "triangle" wave, don't know the proper name/names.

Thanks!
Fletcher Glenn

2006-05-23, 7:17 pm


"Miko" <naoak@kajoa.kco> wrote in message
news:d%Kcg.1573$E02.391@newsb.telia.net...
> Logan Shaw wrote:
>
> Thank you very much for your help!
>
> I have two other quetions
>
> 1. What is the proper way to quote when you want to send a
> simple thanks like this? Now I just quoted the first part and
> snipped the rest, but the thanks is for the whole reply of
> course.
>
> 2. What is the difference in sound of square wave, sine wave and
> the third, "triangle" wave, don't know the proper name/names.
>
> Thanks!


A sine wave is like a whistling sound. A square wave sounds like an
electronic police siren. A triangle wave sounds ragged/odd.

--

Fletcher Glenn


Hubble

2006-05-24, 7:16 am

>A sine wave is like a whistling sound. A square wave sounds like an
>electronic police siren. A triangle wave sounds ragged/odd.


Due to overtone spectrum, you could also say:

- a sin wave sounds like a (softly played) guitar
- a symmetric square wave sounds like a flute
- a sawtooth wave sounds like a violin
- a triangle wave has no equivalent

Hubble.

Ralf Fassel

2006-05-24, 7:16 am

* Logan Shaw <lshaw-usenet@austin.rr.com>
| If you want to fit in with Western music, you'll probably want to
| stick with the 12-tone scale.

What if I want to fit in with the other kind of music?

SCNR...
R'
Jim Cochrane

2006-05-24, 7:16 pm

On 2006-05-24, Ralf Fassel <ralfixx@gmx.de> wrote:
> * Logan Shaw <lshaw-usenet@austin.rr.com>
>| If you want to fit in with Western music, you'll probably want to
>| stick with the 12-tone scale.
>
> What if I want to fit in with the other kind of music?
>
> SCNR...
> R'


It depends on which kind - you've got many choices - e.g.:

http://www.google.com/search?q=musi...:en-US:official

--


*** Posted via a free Usenet account from http://www.teranews.com ***
Martin Blume

2006-05-25, 7:18 am

"Miko" schrieb
> Does anyone know a good document on how I could
> write a c program that plays a tune by writing to
> /dev/dsp? By that I mean that the tune should be
> generated in the program


You might want to look at:
http://linux-sound.org/dsp.html

HTH
Martin


Brian Raiter

2006-05-29, 5:32 pm

>- a sin wave sounds like a (softly played) guitar

Actually, a soprano recorder played softly produces a waveform
remarkably close to a sine wave. But of course, the best physical
instrument for producing a sine wave is a tuning fork.

b
Jordan Abel

2006-05-29, 5:32 pm

On 2006-05-25, Brian Raiter <breadbox@muppetlabs.com> wrote:
>
> Actually, a soprano recorder played softly produces a waveform
> remarkably close to a sine wave. But of course, the best physical
> instrument for producing a sine wave is a tuning fork.


I believe the tones used for the vacant number intercept (the number you
have dialed is not in service) on US phone lines are simple (as opposed
to DTMF) sine waves. And the components of DTMF tones are sine waves,
some phones will let you generate a column's tone by pressing two
buttons in that column
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com