13

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.

kechap
  • 2,077
  • 6
  • 28
  • 50
  • This doesn't directly answer your question, except that it is an interesting factoid about a musician who made sound with a computer that wasn't built to make sound: http://en.wikipedia.org/wiki/Aphex_Twin#ZX81_competition – Alex Reynolds Feb 04 '12 at 00:34
  • @AlexReynolds That's inspiring. – kechap Feb 04 '12 at 00:46
  • If you just want "a" sound, just do `cout << (char)7;` :) – Mysticial Feb 04 '12 at 00:47
  • @Mysticial: I think that's more of an A#, non? – Kerrek SB Feb 04 '12 at 00:50
  • @KerrekSB The BIOS beep is an A#? I thought that was implementation dependent? – Mysticial Feb 04 '12 at 00:51
  • Voting to close as tool rec. Even OSes are libraries ;-) And if no OS is used, we would need to know your exact hardware, and it would come down to: write a device driver for... which is likely too broad. SDL version: http://stackoverflow.com/questions/10110905/simple-wave-generator-with-sdl-in-c/36550306#36550306 – Ciro Santilli OurBigBook.com Apr 11 '16 at 13:37
  • "Even OSes are libraries"? I guess the question wasn't very specific about "the interesting stuff", but I suspect you missed the point they were going for. It's quite common in exploratory programming to try doing things "without a library" -- using only the base components provided by your development platform. Anyway, they specified the general level of abstraction of interest. – Brent Bradburn Mar 19 '20 at 05:41

4 Answers4

10

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.

Brent Bradburn
  • 51,587
  • 17
  • 154
  • 173
5

On some Unix(ish) systems, you can just write audio data to /dev/audio (or /dev/dsp) and it will play. On modern Linux systems using ALSA, you may need to pipe it to aplay instead. In either case, though, you don't need to use any specific sound library — just open an output stream and write to it. This is how all those bytebeat one-liners do it.

Ilmari Karonen
  • 49,047
  • 9
  • 93
  • 153
2

You need to comunicate with the audio hardware, but the times in which you can do it directly are long over...

May i suggest OpenAL?

Gigi
  • 4,953
  • 24
  • 25
2

All the other answers just suggest some abstraction or library. There's always the possiblity to fall back directly on the hardware. (Whether this is a good idea is a completely different question and completely up to you to decide)

May I suggest looking at audio driver code? The Emu10k1 driver is just one example available in linux sources.

Also worth a look is this: "Creating a Kernel Driver for the PC-Speaker"

I also remember the days on Amiga where some funny dude created a program that (ab-)used the Floppy drive stepper motor as an audio output.