How to resolve the algorithm Musical scale step by step in the C programming language

Published on 7 June 2024 03:52 AM
#C

How to resolve the algorithm Musical scale step by step in the C programming language

Table of Contents

Problem Statement

Output the 8 notes of the C major diatonic scale to the default musical sound device on the system. Specifically, pitch must be tuned to 12-tone equal temperament (12TET) with the modern standard A=440Hz. These are the notes "C, D, E, F, G, A, B, C(1 octave higher)", or "Do, Re, Mi, Fa, Sol, La, Si/Ti, Do(1 octave higher)" on Fixed do Solfège. For the purpose of this task, Middle C (in the case of the above tuning, around 261.63 Hz) should be used as the starting note, and any note duration is allowed. For languages that cannot utilize a sound device, it is permissible to output to a musical score sheet (or midi file), or the task can be omitted.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Musical scale step by step in the C programming language

This code is a simple music player that plays a sequence of notes. It uses the sound function in the dos.h library to play the notes. The delay function is used to delay the playback of the notes.

The first version of the code uses the kbhit function to check if a key has been pressed. If a key has been pressed, the program exits. The second version of the code uses the Beep function to play the notes. The Beep function takes two arguments: the frequency of the note and the duration of the note. The frequency of the note is calculated using the formula 261.63 * pow(2, key / 12.0). The duration of the note is specified in milliseconds.

The main function initializes the sequence of notes and then enters a loop. In the loop, the program plays the current note and then increments the index of the current note. If the index of the current note is equal to the length of the sequence, the index is reset to 0. The program exits the loop when a key is pressed.

Here is a more detailed explanation of the code:

  • The first line of the code includes the stdio.h library. This library contains the printf function, which is used to print text to the console.
  • The second line of the code includes the conio.h library. This library contains the kbhit function, which is used to check if a key has been pressed.
  • The third line of the code includes the math.h library. This library contains the pow function, which is used to calculate the frequency of the notes.
  • The fourth line of the code includes the dos.h library. This library contains the sound function, which is used to play the notes.
  • The fifth line of the code defines a note structure. This structure contains two members: a string representing the name of the note and an integer representing the key of the note.
  • The sixth line of the code declares an array of note structures. This array contains the sequence of notes that will be played by the program.
  • The seventh line of the code defines the main function. This function is the entry point of the program.
  • The eighth line of the code declares an integer variable i. This variable will be used to keep track of the current note in the sequence.
  • The ninth line of the code enters a while loop. This loop will continue until a key is pressed.
  • The tenth line of the code uses the printf function to print the name of the current note to the console.
  • The eleventh line of the code uses the sound function to play the current note. The frequency of the note is calculated using the formula 261.63 * pow(2, sequence[i].key / 12.0). The duration of the note is specified as 1000 milliseconds.
  • The twelfth line of the code uses the delay function to delay the playback of the notes. The duration of the delay is specified as 500 milliseconds if the current note is the first note in the sequence, and 1000 milliseconds otherwise.
  • The thirteenth line of the code increments the index of the current note.
  • The fourteenth line of the code checks if the index of the current note is equal to the length of the sequence. If it is, the index is reset to 0.
  • The fifteenth line of the code checks if a key has been pressed. If a key has been pressed, the program exits the loop.
  • The sixteenth line of the code uses the nosound function to stop the playback of the notes.
  • The seventeenth line of the code returns 0 to indicate that the program has exited successfully.

Source code in the c programming language

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<dos.h>

typedef struct{
	char str[3];
	int key;
	}note;
	
note sequence[] = {{"Do",0},{"Re",2},{"Mi",4},{"Fa",5},{"So",7},{"La",9},{"Ti",11},{"Do",12}};

int main(void)
{
	int i=0;
	
	while(!kbhit())
	{
		printf("\t%s",sequence[i].str);
		sound(261.63*pow(2,sequence[i].key/12.0));
		delay(sequence[i].key%12==0?500:1000);
		i = (i+1)%8;
		i==0?printf("\n"):printf("");
	}
	nosound();
	return 0;
}


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

typedef struct{
	char str[3];
	int key;
	}note;
	
note sequence[] = {{"Do",0},{"Re",2},{"Mi",4},{"Fa",5},{"So",7},{"La",9},{"Ti",11},{"Do",12}};

int main(void)
{
	int i=0;
	
	while(1)
	{
		printf("\t%s",sequence[i].str);
		Beep(261.63*pow(2,sequence[i].key/12.0),sequence[i].key%12==0?500:1000);
		i = (i+1)%8;
		i==0?printf("\n"):printf("");
	}
	return 0;
}


  

You may also check:How to resolve the algorithm Abundant, deficient and perfect number classifications step by step in the Yabasic programming language
You may also check:How to resolve the algorithm Hello world/Newbie step by step in the Racket programming language
You may also check:How to resolve the algorithm Barnsley fern step by step in the REXX programming language
You may also check:How to resolve the algorithm Increment a numerical string step by step in the Rascal programming language
You may also check:How to resolve the algorithm Mad Libs step by step in the Picat programming language