1

I'm sorry if this is long. I feel I need to be very thorough as to where I am actually at. Right so first off I do have experience with JavaScript. Which is not the same thing, But it sure looks familiar to me.

I want to make music with C. I got his big book called "The audio programming book". It was like $60. It dose have an introduction to C in it. It all makes good sense to me so far. nothing really new.

So here's my problem... And its very simple. So simple in-fact that it doesn't seem to be covered in the book. I don't understand the relationship between my program and the speakers. my computer's audio device. I thought to myself "Alright lets start with the very basics. Let's see if we can just make a noise." And so... I have something that will make a beep.

#include <stdio.h>
#include <stdlib.h>

    int main()
    {
        printf("Hello world!\n");

        printf("\a\n");
        return 0;
    }

So that's cute. But that's seemingly all that function is capable of. And honestly That would be rather confusing if... the thing that is meant for printing text to your screen... was also the thing you use to talk to your speakers....

I've been looking through the book. I've been looking online. I've been looking for hours. And clearly searching in all the wrong places... because literally all I can find is more of how to make your computer beep...

where is the "send to speaker"?? where do I put in the frequency? Heaven forbid the part were I tell the thing what device I would like to talk to.

Dose C simply not have any built-in functionality for sending signals to the speakers?

Do I need a Library? Do need C++? What am I missing. I don't know anything about desktop applications. All my experiences is with Internet technologies.

HAL9000
  • 21
  • 2
  • 4
  • That's the only sound you will be able to play using `printf()`. To build melodies or play music files from your computer you will need to use a 3rd party library like FFmpeg or GStreamer. – karlphillip Apr 13 '16 at 15:00
  • 2
    Which standard language includes such functions?? Learn how to crouch before starting to run. There is much more to learn about C the "Hello World" before you should even start thinking about using advanced 3rd party libraries. – too honest for this site Apr 13 '16 at 15:02
  • 1
    [Here's a C program that uses OpenCV to display video and FFmpeg to play audio from a file on the disk](http://stackoverflow.com/a/8191506/176769). – karlphillip Apr 13 '16 at 15:04
  • Nope, no built-in functionality to connect to the speakers (or the hard-disk, for that matter).. You can generate the audio by studying the format of digital music formats like wav or voc or smp. Once done, you can output a single tone of a specified duration to a sound-file and then later, play it with an installed application. Talking to the hardware yourself will need to go through a library and depending on the OS you're under, some will not work. For what it's worth Windows has a built-in sound subsystem. I've made wav files with arduino - no ability to playback there!! – enhzflep Apr 13 '16 at 15:05
  • Audio is actually somewhat complicated. You need to continuously make sure that the audio buffer on the sound card has data available to play, and so on. This is why it's not part of the standard, because it's so very dependent on different implementations. Of course you can write it yourself, but you'd better use a 3rd party library, I'd say. Personally, I've used openAL. If you know openGL, this one will make instant sense to you. – antiHUMAN Apr 13 '16 at 15:05
  • But if you're on windows, for example, you could use win32 to play sounds and music. – antiHUMAN Apr 13 '16 at 15:06
  • You _may_ get some utility out of the following project: http://www.pouet.net/prod.php?which=20171 - I've implemented the synth in JS/C/ASM and also removed the DirectSound requirement. Not the best code out there, but I found it helpful to look through a couple of years back.It's also kinda neat having a 2.1kb exe that produces a 10MB wav file, though I've seen things like Clinkster produce **mind-numbingly** good sound from under 3kb of compressed program and data. – enhzflep Apr 13 '16 at 15:15
  • 2
    Thank you karlphillip, enhzflep, antiHUMAN, user2079303. I don't know any one that has this kind of experience... that I might jusk ask them a simple question like this. So I have to come here when Im just Lost and missing something. Its Wonderful to get the benefit of multiple individuals worth of experience. I know this sounds sappy.. I'm just.. thank you all for taking the time. – HAL9000 Apr 13 '16 at 15:41

2 Answers2

2

Dose C simply not have any built-in functionality for sending signals to the speakers?

Indeed, C does not have any built-in functionality for sending signals to the speakers.

Do I need a Library?

No, but I would highly recommend to use one.

Do need C++?

No. You don't need C++ for doing anything in C. Besides, C++ also has no built-in audio functionality.

where is the "send to speaker"?? where do I put in the frequency? Heaven forbid the part were I tell the thing what device I would like to talk to.

All these and other things related to audio are platform (operating system) specific. In order to interact with a sound card (which is the device that sends signals to the speakers), you must use platform specific API. Some operating systems may have multiple different APIs for audio. You can, as I already recommended, use a (cross-platform) library that abstracts the platform specific API.

eerorika
  • 232,697
  • 12
  • 197
  • 326
0

You say you want to "make music with C". This seems to imply that you want to be able to feed data into the sound buffer in real-time through something like an ASIO driver, with low-lantency, and that you want to write your own audio synthesis. This is all quite complicated stuff, but you should start by getting some library/API that gives you that kind of access. Sadly, most of them do not tend to be free, but there probably are some free options, if you look around.

Since all of this depends so much on what library/API you use, I sadly cannot say anything more precise.

Another option is to study up and learn how to program VST-instruments (plug-ins for DAWs), which will probabably be even more useful for what it sounds like you're trying to do.

And no, you don't need C++, unless the library/API you want to use only supports C++, for some reason. That doesn't tend to be the case.

antiHUMAN
  • 250
  • 2
  • 11