0

I need help, my code doesn't output the piano sound.

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

//Dó: 261 Hz
//Ré: 293 Hz
//Mi: 329 Hz
//Fá: 349 Hz
//Sol: 392 Hz 
//Lá: 440 Hz
//Si: 493 Hz
//Dó(2): 522 Hz

int main(){
setlocale (LC_ALL,"Portuguese");
char nota = ' ';
int tempo = 150;
int som [8] = {261, 293, 329, 349, 392, 440,   493, 522}; 
puts("### PIANO VIRTUAL ###");
puts("\n m - Dó \n a - Ré \n r - Mi \n c - Fá \n    e - Sol \n l - Lá \n q - Si \n u - Dó*2");

nota = getchar();
switch (nota){
        case 'm': Beep(som[0]); tempo;
        case 'a': Beep(som[1]); tempo;
        case 'r': Beep(som[2]); tempo;
        case 'c': Beep(som[3]); tempo;  
        case 'e': Beep(som[4]); tempo;
        case 'l': Beep(som[5]); tempo;
        case 'q': Beep(som[6]); tempo;
    case 'u': Beep(som[7]); tempo;
    }       
}

I need that return a piano beep with the function "Beep"

I don't know what more to say so pls help me,

tadman
  • 208,517
  • 23
  • 234
  • 262
  • 1
    Where does `Beep` come from? – tadman May 30 '20 at 04:24
  • Can you please add the code where you defined ```Beep```? – Todd May 30 '20 at 04:26
  • @Todd how can I do it? Declare the function beep – user13558928 May 30 '20 at 04:28
  • 2
    When programming you can't just make stuff up and have the computer figure it out. You need to call existing functions or define them yourself and express *exactly* what the computer is supposed to do. – tadman May 30 '20 at 04:32
  • 1
    It's also worth noting that `tempo;` by itself does absolutely nothing in C code. – tadman May 30 '20 at 04:33
  • 3
    On top of that you need to learn how `switch` works and in particular the *fall-through* behaviour. If you don't `break` it will just trickle through to the next condition. There's absolutely nothing wrong with learning, but these problems indicate you don't have a good reference to work from. Consider getting a [good book](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list). – tadman May 30 '20 at 04:34

1 Answers1

1

Standard C has no function for sounds.

Check by reading n1570 (the C11 standard) or this reference. There is no Beep there. Read also the Modern C book.

Some operating systems provide APIs and libraries to make sounds. But some computers (e.g. the web server providing you this content) don't even have any loudspeakers or devices capable of producing a nice sound (apart their fans). Any sound-related function is either operating system or computer specific. On an Arduino, your C code producing a sound would be very different than one for my Linux desktop.

On Linux, read about ALSA and consider using libsdl (which is an abstraction layer working on several OSes). On Windows, study the WinAPI.

You could code something interacting with your web browser and have your web browser produce a sound (if your computer can do that). Then learn more about the HTTP protocol and HTML5 and JavaScript.

You could find several libraries (for Linux at least) providing an API for HTTP services. For example libonion.

Read of course How to debug small programs

If you compile your C code with GCC, be sure to enable all warnings and debug info, so compile with gcc -Wall -Wextra -g. Learn also to use some build automation tool such as GNU make or ninja, etc....

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547