Hi,
Iam trying to design a small C programme which can generate a squarewave function and play a sequence of musical notes for 30 seconds. How would i go about doing this?
Many Thanks
teknologik
Printable View
Hi,
Iam trying to design a small C programme which can generate a squarewave function and play a sequence of musical notes for 30 seconds. How would i go about doing this?
Many Thanks
teknologik
Under some compilers, there is a function called sound(). Note that this function is not portable, so don't rely on it. For audio, I would suggest a library such as SDL and/or SDL_Mixer.
EDIT: The compiler I discovered the sound() function on is called DJGPP.
i know how to make the square wave using C its just im not sure how to generate a sequence of musical notes correctly.
static char seq[]="CDEFGABAGFEDC"; but thats as far as i can get
does anyone know how to generate a sequence of notes?
Well, presuming this is homework, I'd suggest looking into whatever your professor or tutor suggests . . .
IF (and only if) your tutor/professor can't tell you what to use, try sound() and a lookup table of freqs.
EDIT: If this isn't homework, I'm sorry . . .
my school days have been and gone; so lucky for me this isnt homework other wise i would never get it handed in on time lol just trying to learn some C thats all, how would i go about making a look up table then
Well, I know that middle A is 440 . . . I have a lookup table around here somewhere . . .
Here you go:
So, _LC is Low C, i.e. one octave below Middle C. _LCS is one semitone higher.Code:#define _LC 130
#define _LCS 137
#define _LD 145
#define _LDS 155
#define _LE 166
#define _LF 175
#define _LFS 185
#define _LG 195
#define _LGS 207
#define _LA 220
#define _LAS 235
#define _LB 245
#define _MC 260
#define _MCS 275
#define _MD 290
#define _MDS 311
#define _ME 333
#define _MF 350
#define _MFS 370
#define _MG 390
#define _MGS 415
#define _MA 440
#define _MAS 470
#define _MB 490
#define _HC 520
BTW, I found this by playing them on my piano (which was out of tune at the time . . .) so they may be a little off. You do know that one octave higher is double the freq, right?
Googling for "piano note hertz" turned up this page: http://www.vibrationdata.com/piano.htm
It has hertz values for different notes.
[edit] Also, see this Wikipedia entry: http://en.wikipedia.org/wiki/Note
Scroll down to Note frequency (hertz) for a formula for calculating frequency values. [/edit]
So, a simple program to play the above sequence you mentioned:
Two things: I apoligize about the underscore leading the #defines, and this code _may_ not work, as I haven't compiled it yet . . .Code:#include <stdio.h>
#include <unistd.h>
int main() {
int notes[14] = {_MC, _MD, _M, _ME, _MF, _MG, _LA, _LB, _LA, _LG, _LF, _LE, _LD, _LC};
int x;
for(x = 0; x < 14; x ++) {
sound(notes[x]);
delay(100); /* NOTE! THIS CRASHES WINDOWS 98 SOMETIMES */
}
sound(0);
return 0;
}
EDIT: Try sleep(), but I don't know if RHIDE/DJGPP supports it . . .
DJGPP's sound() function appears to be in pc.h: www.delorie.com/djgpp/doc/libc/libc_735.html
That function looks very unportable. Perhaps there is a Windows function that would be more likely to work with the OP's existing compiler?
I don't know, as I am suggesting a possible solution, not a rock-solid answer. Oops :P.
> the compiler which i am using Turbo C v 2.0.1
Lemme guess, your OS is XP ?
Would you buy a Ferrari, and replace the engine with a steam engine?
Because that is what you've done by trying to use TC2.01 on a modern machine.
yes the os is xp
ok then salem what do you recommend a suitable compiler?