Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating sounds without a library? [closed]

Tags:

c++

c

audio

I am trying to find documentation, tutorials, examples on how to generate sounds. I mean without using a library that will hide all the interesting stuff.

I am interesting in sound and I want to start doing something with it but I don't know from where to start.

Correct me if I am wrong but the lowest level to generate sound is one of these (DirectSound ,CoreAudio,ALSA,OSS) depending on the OS. So I have to pick an operating system and learn the appropriate sound system?

Is this really worth or I should just learn a library that wraps all the above and offers cross platform compatibility?

Maybe this question is not very clear and I am sorry for that but as it turned out I don't even know what I want. I am just trying to find something interesting for my thesis.

like image 309
kechap Avatar asked Feb 04 '12 00:02

kechap


1 Answers

Here's an example to get you started.

// filename "wf.cpp" (simple wave-form generator)

   #include <iostream>
   #include <cmath>
   #include <stdint.h>

int main()
   {

   const double R=8000; // sample rate (samples per second)
   const double C=261.625565; // frequency of middle-C (hertz)
   const double F=R/256; // bytebeat frequency of 1*t due to 8-bit truncation (hertz)
   const double V=127; // a volume constant

   for ( int t=0; ; t++ )
      {
      uint8_t temp = (sin(t*2*M_PI/R*C)+1)*V; // pure middle C sine wave
   // uint8_t temp = t/F*C; // middle C saw wave (bytebeat style)
   // uint8_t temp = (t*5&t>>7)|(t*3&t>>10); // viznut bytebeat composition
      std::cout<<temp;
      }

   }

compile and run on Linux via ALSA interface:

make wf && ./wf |aplay

compile and run on Linux via GStreamer interface:

make wf && ./wf |gst-launch-0.10 -v filesrc location=/dev/stdin ! 'audio/x-raw-int,rate=8000,channels=1,depth=8' ! autoaudiosink

GStreamer claims to be cross-platform. It's main feature of interest is that you can create (or use existing) plugins to construct a pipeline of audio filters.

like image 175
Brent Bradburn Avatar answered Sep 19 '22 10:09

Brent Bradburn