Writing tune to /dev/dsp
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Writing tune to /dev/dsp




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Writing tune to /dev/dsp  
Miko


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-23-06 12:15 AM

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





[ Post a follow-up to this message ]



    Re: Writing tune to /dev/dsp  
Logan Shaw


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-23-06 06: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





[ Post a follow-up to this message ]



    Re: Writing tune to /dev/dsp  
Miko


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-24-06 12:17 AM

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!





[ Post a follow-up to this message ]



    Re: Writing tune to /dev/dsp  
Fletcher Glenn


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-24-06 12:17 AM


"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







[ Post a follow-up to this message ]



    Re: Writing tune to /dev/dsp  
Hubble


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-24-06 12:16 PM

>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.






[ Post a follow-up to this message ]



    Re: Writing tune to /dev/dsp  
Ralf Fassel


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-24-06 12:16 PM

* 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'





[ Post a follow-up to this message ]



    Re: Writing tune to /dev/dsp  
Jim Cochrane


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-25-06 12:16 AM

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 ***





[ Post a follow-up to this message ]



    Re: Writing tune to /dev/dsp  
Martin Blume


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-25-06 12:18 PM

"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







[ Post a follow-up to this message ]



    Re: Writing tune to /dev/dsp  
Brian Raiter


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-29-06 10: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





[ Post a follow-up to this message ]



    Re: Writing tune to /dev/dsp  
Jordan Abel


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-29-06 10: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





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 10:38 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register