Search code examples
arraysbeep

Digital piano(using Beep function) in c programming


I want to make a program that make a sound when I entered a key.

The process is like this -> get data about key & frequency from .txt file and store that data in array. Then, using function 'findFrequency' and 'playpiano' that I made, I want to make a sound when I entered a key.

The code I made is like this :

#include <stdio.h>
#include <windows.h>
#include <stdbool.h>

#define NUM_NOTES 8

char key_table[NUM_NOTES];
float freq_table[NUM_NOTES];

float findFrequency(const char note);
void playpiano(void);

void main()
{
FILE *input_file = fopen("digitalpiano.txt", "r");

int num_keys;
fscanf(input_file, "%d\n", &num_keys);

printf("%d\n", num_keys);

for (int i = 0; i < NUM_NOTES; i++)
{
    char key; // I think I can delete this code
    float freq; // I think I can delete this code

    fscanf(input_file, "%c %f\n", &key_table[i], &freq_table[i]);
    printf("%c %.3f\n", key_table[i], freq_table[i]);

}

fclose(input_file);

while (true)
    playpiano;

}


float findFrequency(char note)
{
for (int i = 0; i < NUM_NOTES; i++)
{
    if (key_table[i] == note) return freq_table[i];
}
return 0.0f;
}

void playpiano(void)
{
char ch = getch();
Beep(findFrequency(ch), 500);

return;
}

But when I start this code, I can show a data from txt file. But when I entered a key, there are any sound, so I cannot hear anything.

And the digitalpiano.txt file is like this :

8
a 261.626
s 293.665
d 329.628
f 349.228
g 391.995
h 440.000
j 493.883
k 523.251

Solution

  • You aren't calling playpiano; you need to include the parentheses: playpiano();.