Musical scale: Difference between revisions

Content deleted Content added
Loren (talk | contribs)
Added XPL0
Aamrun (talk | contribs)
Line 4: Line 4:


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.
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.

=={{header|C}}==
Although C provides low level access to devices, there is no standardized way. Here are illustrated two approaches.

==={{header|Borland's Turbo C}}===
Borland's Turbo C system has the dos.h header file which contains the functions sound() and nosound(). sound takes an argument which is the frequency of the note to be played through the device speaker. delay(), also part of dos.h tells the code how long to suspend execution. The sound will however still be playing. It is therefore essential to call nosound() at the end which ends the speaker output, otherwise the Turbo C (DOS) session will have to be ended.

<lang C>
#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()
{
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;
}
</lang>

==={{header|Windows C}}===
I named it Windows C for want of a better name. This is actually more constrained than the above example since although it will run on any Windows machine, Beep can only play integer frequencies and thus the tones are noticeably lower than the ones played by sound() above.

<lang C>
#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()
{
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;
}
</lang>



=={{header|Lilypond}}==
=={{header|Lilypond}}==