Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create sound from an array of double and play it on speaker

Tags:

c++

c

linux

audio

I have an array of double (size more than 60k entries), I have the frequency value. Now I want to create a sound from it using C/C++ which I can play on speaker. My OS is linux.

Thanks. I hope I am clear enough.

like image 926
Dilawar Avatar asked Dec 06 '25 07:12

Dilawar


2 Answers

http://www.linuxjournal.com/article/6735

This is a link to an article in Linux Journal about programming with the ALSA (Advance Linux Sound Architecture). It contains some example code.

like image 149
nategoose Avatar answered Dec 07 '25 19:12

nategoose


The following information comes from a command-line program called beep, available in Debian. The source code is available through the repositories, and also available here.

There's an ioctl() call with a KIOCSOUND request to the console device that you can use to play sounds through the PC speaker. The snippet is:

ioctl(fd, KIOCSOUND, CLOCK_TICK_RATE/(int)frequency);

to play a sound with frequency frequency, and:

ioctl(fd, KIOCSOUND, 0);

to stop the beep. fd is a file descriptor with write permission to /dev/console, and frequency is the sound frequency, given in hertz. The constant CLOCK_TICK_RATE is related to a timer chip used to create the beep, and in the beep source code has the value 1193180 (hertz). Although this might be different for your system, if my mind is correct, I do remember have seen this same constant on old DOS programs that used the PC speaker.

like image 26
alecov Avatar answered Dec 07 '25 21:12

alecov