Jump to content

Music in C++


digip

Recommended Posts

I was working on trying to figure out how to generate sounds out of C++ and came up with the following little intro to Metallica's Harvester of Sorrow. I am a guitar player, so I was able to pick out the notes and then matched them to a scale of frequencies I found online. I am new to C programming, so I do not know if the "windows.h" file has an equivalent to anything on another OS but there is probably a way to do the same thing for other OS's.

#include <iostream.h>
#include <windows.h> // WinApi header

using namespace std;

int main()
{
// Example of how to make different beep tones
// Beep(523,500); // 523 hertz (C5) for 500 milliseconds


// "Metallica : Harvester of Sorrow" Opening by DigiP
// Notes and their Frequencies
// http://www.phy.mtu.edu/~suits/notefreqs.html
// 329: E
// 493: B
// 698: F^
// 659: E^
// 783: G^

// 392: G
// 440: A
// 587: D
// 349: F

Beep(329,300); //E
Beep(493,300); //B
Beep(698,300); //F^
Beep(659,600); //E^

Beep(783,300); //G^
Beep(698,300); //F^
Beep(659,600); //E^

Beep(329,100); 
Beep(493,300); 
Beep(698,300); 
Beep(659,600);

Beep(392,250); 
Beep(440,200); 
Beep(587,300); 

Beep(349,250);
Beep(587,500); 

Beep(329,300); 
Beep(493,300); 
Beep(698,300); 
Beep(659,600); 

Beep(783,300); 
Beep(698,300); 
Beep(659,600); 

Beep(329,100); 
Beep(493,300); 
Beep(698,300); 
Beep(659,600);

Beep(392,250); 
Beep(440,200); 
Beep(587,300); 

Beep(349,250);
Beep(587,400); 


return 0;
}

The neat thing to do would be to map keys to different beeps so you can then play songs using the letters of each note from a standard computer keyboard. Anyone interested in helping me to do that, please post the code here.

(There is probably an easier way to make this, but I am still teaching myself.)

For anyone without a compiler and wants to hear it, you can download it:

Rar: http://www.twistedpairrecords.com/digip/Ha...rrow_in_C++.rar

Stand Alone Exe: http://www.twistedpairrecords.com/digip/Ha...rrow_in_C++.exe

Link to comment
Share on other sites

Interesting concept but why don't you just try something like fmod.. or IrrKlang or open al... Still windows system beeps are all right.

Eh, I wasn't looking for high quality audio or anything. Just the most basic computer sound to make a melody with. Something along the lines of the BSODTV them song, that uses an old Dot Matrix printer to make music.

I don't need mp3's or other formats, or 3d sound for games or anything. I'm Just learning C with a little music thrown in for fun.

Link to comment
Share on other sites

You use the windows-header in your program, shouldn't it be possible to write it without windows.h?

Tried. I get this

music2.c: In function `int main()':
music2.c:26: error: `Beep' was not declared in this scope
music2.c:70:2: warning: no newline at end of file

Aparently Beep (for the pc speaker, not your sound card) in this sense is in the windows.h resource file.

This is why I was saying it only worked on windows, but if there is a way to do it universally, or you write your own header that works for any system, then that would be cool to. I would like to see it work across all OS's and not rely on the wondows.h file, so I an then sit down and make some funky little songs that work on any platform that can compile C.

Link to comment
Share on other sites

Ok. I think I have it for ALL other systems. Can someone try this on an OS other than Windows and test it? It works on windows, but I want to have someone compile and run it on something else to see what happens. Also, you need to have PC speaker on the mother board (Does not run through your sound card)

#include <iostream.h>
#include <stdlib.h> // For all others that have stdlib

using namespace std;

int main()
{
// Example of how to make different _beep tones
// _beep(523,500); // 523 hertz (C5) for 500 milliseconds


// "Metallica : Harvester of Sorrow" Opening by DigiP
// Notes and their Frequencies
// http://www.phy.mtu.edu/~suits/notefreqs.html
// 329: E
// 493: B
// 698: F^
// 659: E^
// 783: G^

// 392: G
// 440: A
// 587: D
// 349: F

_beep(329,300); //E
_beep(493,300); //B
_beep(698,300); //F^
_beep(659,600); //E^

_beep(783,300); //G^
_beep(698,300); //F^
_beep(659,600); //E^

_beep(329,100); 
_beep(493,300); 
_beep(698,300); 
_beep(659,600);

_beep(392,250); 
_beep(440,200); 
_beep(587,300); 

_beep(349,250);
_beep(587,500); 

_beep(329,300); 
_beep(493,300); 
_beep(698,300); 
_beep(659,600); 

_beep(783,300); 
_beep(698,300); 
_beep(659,600); 

_beep(329,100); 
_beep(493,300); 
_beep(698,300); 
_beep(659,600);

_beep(392,250); 
_beep(440,200); 
_beep(587,300); 

_beep(349,250);
_beep(587,400); 


return 0;
}

Link to comment
Share on other sites

I get an error concerning the include files, but appending .h solved it. [system: ubuntu]

Nevertheless, I get an error that "_beep" is not defined in a certain matter. For those who are interested.

user@machine:~$ g++ beep.cpp -o beep
programmierung/beep.cpp: In function »int main()«:
programmierung/beep.cpp:10: Fehler: »_beep« wurde in diesem Gültigkeitsbereich nicht definiert

Ah, you also forgot a newline in the end

Link to comment
Share on other sites

I get an error concerning the include files, but appending .h solved it. [system: ubuntu]

Nevertheless, I get an error that "_beep" is not defined in a certain matter. For those who are interested.

user@machine:~$ g++ beep.cpp -o beep
programmierung/beep.cpp: In function »int main()«:
programmierung/beep.cpp:10: Fehler: »_beep« wurde in diesem Gültigkeitsbereich nicht definiert

Ah, you also forgot a newline in the end

Yeah, I get a "depricated" error for _beep when compiling, but the program still runs fine though. Did it run on your system, or after the error from compile it wouldn't run?

If it works, thats great. I'll go back and edit them for the .h

Link to comment
Share on other sites

Charlie Brown Theme song. :lol:

Finished exe: http://www.twistedpairrecords.com/digip/Peanuts.exe

#include <iostream.h>
#include <stdlib.h>

using namespace std;

int main()
{
// Example of how to make different _beep tones
// _beep(523,500); // 523 hertz (C5) for 500 milliseconds


// Charlie Brown Theme: "Linus and Lucy" by Vince Guaraldi
// Coded by DigiP
// g    d    g^    g    d    g^    g    d    g^ 
// 196  293  392   196  293  392  196  293  392
//
// g    d    e^
// 196  293  329


//First melody Section
_beep(196,200);
_beep(293,200);
_beep(392,300);

_beep(196,200);
_beep(293,200);
_beep(392,600);

_beep(100,200);

_beep(196,200);
_beep(293,200);
_beep(392,500);

_beep(100,100);

_beep(196,200);
_beep(293,200);
_beep(329,500);

//
_beep(196,200);
_beep(293,200);
_beep(392,300);

_beep(196,200);
_beep(293,200);
_beep(392,600);

_beep(100,200);

_beep(196,200);
_beep(293,200);
_beep(329,300);

_beep(100,100);

_beep(196,200);
_beep(293,300);
_beep(329,800);

//
_beep(196,200);
_beep(293,200);
_beep(392,300);

_beep(196,200);
_beep(293,200);
_beep(392,600);

_beep(100,200);

_beep(196,200);
_beep(293,200);
_beep(329,300);

_beep(100,100);

_beep(196,200);
_beep(293,300);
_beep(329,800);

//
_beep(196,200);
_beep(293,200);
_beep(392,300);

_beep(196,200);
_beep(293,200);
_beep(392,600);

_beep(100,200);

_beep(196,200);
_beep(293,200);
_beep(329,300);

_beep(100,100);

_beep(196,200);
_beep(293,300);
_beep(329,800);


// g a b b a g a g a b b b
// b 246

_beep(50,30); //
_beep(196,400); //G
_beep(220,200); //a
_beep(246,400); //B
_beep(50,30); //
_beep(246,300); //B
_beep(50,30); //
_beep(220,200); //A
_beep(196,200); //G
_beep(220,700); //a
_beep(196,800); //G
_beep(50,30); //
_beep(196,200); //G
_beep(220,400); //a
_beep(246,400); //b
_beep(50,30); //
_beep(246,1000);


_beep(50,30); //
_beep(196,400); //G
_beep(220,200); //a
_beep(246,400); //B
_beep(50,30); //
_beep(246,300); //B
_beep(50,30); //
_beep(220,200); //A
_beep(196,200); //G
_beep(220,700); //a
_beep(196,800); //G
_beep(50,30); //
_beep(196,200); //G

_beep(50,30); //
_beep(196,500); //G
_beep(220,400); //a
_beep(50,30); //
_beep(220,1000); //a
_beep(50,30); //



_beep(50,30); //
_beep(196,400); //G
_beep(220,200); //a
_beep(246,400); //B
_beep(50,30); //
_beep(246,300); //B
_beep(50,30); //
_beep(220,200); //A
_beep(196,200); //G
_beep(220,700); //a
_beep(196,800); //G
_beep(50,30); //
_beep(196,200); //G
_beep(220,400); //a
_beep(246,400); //b
_beep(50,30); //
_beep(246,1000);


_beep(50,30); //
_beep(196,400); //G
_beep(220,200); //a
_beep(246,400); //B
_beep(50,30); //
_beep(246,300); //B
_beep(50,30); //
_beep(220,200); //A
_beep(196,200); //G
_beep(220,700); //a
_beep(196,800); //G
_beep(50,30); //
_beep(196,200); //G

_beep(50,30); //
_beep(196,500); //G
_beep(220,400); //a
_beep(50,30); //
_beep(220,1000); //a
_beep(50,30); //


_beep(50,30); //
_beep(196,600); //G
_beep(220,300); //a
_beep(50,30); //
_beep(220,300); //a
_beep(220,800); //a

_beep(50,30); //
_beep(196,2000); //G

_beep(50,30); //
_beep(190,10); //
_beep(196,1500); //G

//--------------------------------------
// Break down
_beep(258,200); //c
_beep(50,30); //
_beep(261,400);
_beep(50,30); //
_beep(261,400);
_beep(50,30); //
_beep(261,400);

_beep(50,30); //
_beep(293,700); //d

_beep(329,500); //e^
_beep(293,200);
_beep(329,500);

// Break
_beep(258,200); //c
_beep(50,30); //
_beep(261,400);
_beep(50,30); //
_beep(261,400);
_beep(50,30); //
_beep(261,400);

_beep(50,30); //
_beep(293,700); //d

_beep(329,500); //e^
_beep(293,200);
_beep(329,500);

//-------------
_beep(50,30); //
_beep(196,600); //G
_beep(220,300); //a
_beep(50,30); //
_beep(220,300); //a
_beep(220,800); //a

_beep(50,30); //
_beep(196,2000); //G

_beep(50,30); //
_beep(190,10); //
_beep(196,1500); //G

return 0;
}

Link to comment
Share on other sites

Nope, it cancelled the process after displaying the errors. Maybe there's a force option oO "Use the force, coder."

I see a "To disable this warning use -Wno-deprecated" on the console, but wouldn't know where to put that. Maybe on the CLI when compiling? Mine still run fine though even with the error while compiling.

Link to comment
Share on other sites

Nope, it cancelled the process after displaying the errors. Maybe there's a force option oO "Use the force, coder."

Im using MinGW to compile mine on windows. What I did was copy the stdlib file to beep.h and on the line where it said

_CRTIMP void __cdecl __MINGW_NOTHROW    _beep (unsigned int, unsigned int) __MINGW_ATTRIB_DEPRECATED;

I changed it to

 
_CRTIMP void __cdecl __MINGW_NOTHROW    _beep (unsigned int, unsigned int);

and then in the src file I removed

//#include <iostream>
//#include <stdlib.h>

and just changed them to

#include <beep.h>

Recompiled, got no errors and program runs fine. :)

Might have to make your own header file for Ubuntu or do what I did here with your systems stdlib.h file

Hope that helps.

Link to comment
Share on other sites

  • 4 weeks later...

You can get a better effect using sine waves. Heres an example stolen from the Hacker Voice forums

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

#ifndef DBL_EPSILON
/* Difference between 1.0 and the minimum double greater than 1.0 */
#define DBL_EPSILON 2.2204460492503131e-16
#endif


int writesamples(char *filename, short *samptable, int samples)
{
FILE *f1;
f1 = fopen(filename, "wb");
fwrite(samptable, sizeof(short), samples, f1);
return fclose(f1);
}

double makesinetable(float *sinetable)
{
const double pi = (4.0 * atan(1.0));
double angle_rad, desiredpeak, inputpeak;
int i, angle = 0;
float value;

// Create the sine table
while(angle <= 360){
 angle_rad = (pi * (angle / 180.0));
 value = sin(angle_rad);
 sinetable[(angle - 1)] = value;
 angle++;
}

inputpeak = 0.0;
for(i = 0; i < 360; i++)
 if(sinetable[i] > inputpeak) inputpeak = sinetable[i];

desiredpeak = 32767.0;
return ((desiredpeak / inputpeak) + DBL_EPSILON);
}

void samplefloats(float *sinetable, short *samptable, int samples, double factor)
{
double dSample;
int nSample, i;
for(i = 0; i < samples; i++){
 dSample = ((double)sinetable[i] * factor);
 nSample = (int)dSample;
 if(nSample < -32767) nSample = -32767;
 else if(nSample > 32767) nSample = 32767;
 samptable[i] = (short)nSample;
}
}

int main(void)
{
float *sinetable;
short *samptable;
double factor;

printf("rawsine16.c");
printf("=================\n");
printf("  MAKE A SINE WAVE \n");
printf("=================\n");

sinetable = malloc(360 * sizeof(float));
samptable = malloc(360 * sizeof(short));

factor = makesinetable(sinetable);
samplefloats(sinetable, samptable, 360, factor);
writesamples("thing16bit.raw", samptable, 360);

free(samptable);
free(sinetable);
return 0;
}

personally, I've never played with it in C/C++, but i've made a whole lot of interesting sounds using the same technique in python, its all very fun.

I did have a link to a page describing the technique used to create sounds using the .wav file format and sine waves, but unfortunately it seems to be 404'ing now. If anyone has any further information of this subject it would be great to see it shared.

James

Link to comment
Share on other sites

Is this useful to anyone?

CONST int C0 = 16;

CONST int Cs0 = 17;

CONST int Df0 = 17;

CONST int D0 = 18;

CONST int Ds0 = 19;

CONST int Ef0 = 19;

CONST int E0 = 20;

CONST int F0 = 21;

CONST int Fs0 = 23;

CONST int Gf0 = 23;

CONST int G0 = 24;

CONST int Af0 = 25;

CONST int Gs0 = 25;

CONST int A0 = 27;

CONST int As0 = 29;

CONST int Ff0 = 29;

CONST int F0 = 30;

CONST int C1 = 32;

CONST int Cs1 = 34;

CONST int Df1 = 34;

CONST int D1 = 36;

CONST int Ds1 = 38;

CONST int Ef1 = 38;

CONST int E1 = 41;

CONST int F1 = 43;

CONST int Fs1 = 46;

CONST int Gf1 = 46;

CONST int G1 = 49;

CONST int Af1 = 51;

CONST int Gs1 = 51;

CONST int A1 = 55;

CONST int As1 = 58;

CONST int Ff1 = 58;

CONST int F1 = 61;

CONST int C2 = 65;

CONST int Cs2 = 69;

CONST int Df2 = 69;

CONST int D2 = 73;

CONST int Ds2 = 77;

CONST int Ef2 = 77;

CONST int E2 = 82;

CONST int F2 = 87;

CONST int Fs2 = 92;

CONST int Gf2 = 92;

CONST int G2 = 98;

CONST int Af2 = 103;

CONST int Gs2 = 103;

CONST int A2 = 110;

CONST int As2 = 116;

CONST int Ff2 = 116;

CONST int F2 = 123;

CONST int C3 = 130;

CONST int Cs3 = 138;

CONST int Df3 = 138;

CONST int D3 = 146;

CONST int Ds3 = 155;

CONST int Ef3 = 155;

CONST int E3 = 164;

CONST int F3 = 174;

CONST int Fs3 = 185;

CONST int Gf3 = 185;

CONST int G3 = 196;

CONST int Af3 = 207;

CONST int Gs3 = 207;

CONST int A3 = 220;

CONST int As3 = 233;

CONST int Ff3 = 233;

CONST int F3 = 246;

CONST int C4 = 261;

CONST int Cs4 = 277;

CONST int Df4 = 277;

CONST int D4 = 293;

CONST int Ds4 = 311;

CONST int Ef4 = 311;

CONST int E4 = 329;

CONST int F4 = 349;

CONST int Fs4 = 369;

CONST int Gf4 = 369;

CONST int G4 = 392;

CONST int Af4 = 415;

CONST int Gs4 = 415;

CONST int A4 = 440;

CONST int As4 = 466;

CONST int Ff4 = 466;

CONST int F4 = 493;

CONST int C5 = 523;

CONST int Cs5 = 554;

CONST int Df5 = 554;

CONST int D5 = 587;

CONST int Ds5 = 622;

CONST int Ef5 = 622;

CONST int E5 = 659;

CONST int F5 = 698;

CONST int Fs5 = 739;

CONST int Gf5 = 739;

CONST int G5 = 783;

CONST int Af5 = 830;

CONST int Gs5 = 830;

CONST int A5 = 880;

CONST int As5 = 932;

CONST int Ff5 = 932;

CONST int F5 = 987;

CONST int C6 = 1046;

CONST int Cs6 = 1108;

CONST int Df6 = 1108;

CONST int D6 = 1174;

CONST int Ds6 = 1244;

CONST int Ef6 = 1244;

CONST int E6 = 1318;

CONST int F6 = 1396;

CONST int Fs6 = 1479;

CONST int Gf6 = 1479;

CONST int G6 = 1567;

CONST int Af6 = 1661;

CONST int Gs6 = 1661;

CONST int A6 = 1760;

CONST int As6 = 1864;

CONST int Ff6 = 1864;

CONST int F6 = 1975;

CONST int C7 = 2093;

CONST int Cs7 = 2217;

CONST int Df7 = 2217;

CONST int D7 = 2349;

CONST int Ds7 = 2489;

CONST int Ef7 = 2489;

CONST int E7 = 2637;

CONST int F7 = 2793;

CONST int Fs7 = 2959;

CONST int Gf7 = 2959;

CONST int G7 = 3135;

CONST int Af7 = 3322;

CONST int Gs7 = 3322;

CONST int A7 = 3520;

CONST int As7 = 3729;

CONST int Ff7 = 3729;

CONST int F7 = 3951;

CONST int C8 = 4186;

CONST int Cs8 = 4434;

CONST int Df8 = 4434;

CONST int D8 = 4698;

CONST int Ds8 = 4978;

CONST int Ef8 = 4978;

Link to comment
Share on other sites

seems interesting to me. if i knew anything about music i would try the keymapping thing as my first complete prog after learning enough c++ to function.

Link to comment
Share on other sites

Is this useful to anyone?

CONST int Ef8 = 4978;

That would be cool to have a command line take input from the user and you can then just type the notes and length to sustain them and the program spit out the song. THis way you can just make some 3 chord little punk songs using just the phrases of notes, like "a a a d d e e e d d a a a d d e e e" and it find the frequencies from your list above and then play them. You could then add somehting like a duration following the note indicated by a + sign and a number representing the duration, like "a+2a+2a+2 d+2d+2 e+2e+2e+2 d+2d+2" etc.. Maybe then map notes ot the letter on the keyboard and add sharps and flats bases on something like shift key for sharp and alt key for flats, etc. Just an idea. Next step would be to build a sequencer to map out the song notation, and then record the song to some format like wav or mp3. Granted, there are already things out there that can do all of this, but just for the fun of making your own from scratch with nothing more than ideas on how to do it yourself is what usually draws me ot tinker with shit like this. I don't care how cool it is to others, just if I am learnign somehting and having fun while tinkering with it.

Link to comment
Share on other sites

  • 7 months later...

I'm sorry if I'm digging up an old topic, but I find this very interesting and useful.

I am currently making a '8bit' game for a '8bit' game competition so this kind of music would be great.

I'm not very good at c++ because I just started learning it few months ago, but I think I can find a way to make a dll of this and use it in my game to make 8bit music from some of my music.

I'm also a guitar player and Metallica fan and I found some mistakes in your beep version of "Harvester Of Sorrow". I'll try to correct it and then I'll edit my post. ^_^

//not 100% correct

Beep(329,300);
Beep(493,300);
Beep(698,300);
Beep(659,300);

Beep(329,150);
Beep(493,150);
Beep(783,300);
Beep(698,300);
Beep(659,300);

Beep(329,300);
Beep(493,300);
Beep(698,300);
Beep(590,300);

Beep(392,150);
Beep(440,150);
Beep(587,300);

Beep(349,300);
Beep(587,300);

// and same thing again:

Beep(329,300);
Beep(493,300);
Beep(698,300);
Beep(659,300);

Beep(329,150);
Beep(493,150);
Beep(783,300);
Beep(698,300);
Beep(659,300);

Beep(329,300);
Beep(493,300);
Beep(698,300);
Beep(590,300);

Beep(392,150);
Beep(440,150);
Beep(587,300);

Beep(349,300);
Beep(587,300);

Link to comment
Share on other sites

  • 3 weeks later...

Excellent stuff :-D

Iv'e not seen anything like this since those heady days compiling and developing under MS-Dos using Turbo C & BC++. oh and who can for get Zortech?? I still have a set of 20 5.25 Installation disks hiding in a corner somewhere :P

Seriously though, good on you for having fun while learning, thats what it's all about, and it's something a lot of students don't do these days, i remember the voyage of discovery i had back in the 80's, and i still enjoy and have passion for what i do today.

If you want to expand on the speaker beep thing, something you may want to try is to use the Classic Win32 Multimedia API, and open the default midi device directly, you can then send note on/note off commands one at a time with the appropriate values to play music.

I keep meaning to resurrect a project i chucked together eons ago, to play back music files created by the music tracker suite i wrote for the BBC Model B micro, and in order to do that i was going to adopt a similar approach, I just never got round to it :-D

Cheers

Shawty

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...