C ++ generate a simple WAV file (2)

Source: Internet
Author: User

Now that the sound generation is okay, a random music file similar to a buzzer is generated. (Note: There may be issues such as the format is not formal, but the idea should be okay, and the test can be run.) because it is random, first create a class R that generates random numbers. H and R. cpp r. h: copy the Code # ifndef R_H _ # define R_H_class R {public: R (); int suiji (int I); int suiji (); private: int shuchu (int number) ;};# endif/* R_H _ */copy the code R. cpp: copy the Code # include <windows. h> # include <iostream> # include <cstdlib> # include "R. h "using namespace std; R: R () {int I = GetTickCount (); cout <I <endl; srand (I);} int R :: suiji (int I) {return shuchu (I);} I Nt R: suiji () {return shuchu (8);} int R: shuchu (int number) {if (number> 1000) number = 1000; int jieguo; jieguo = rand () % number; return jieguo;} copy the Code through R r and r. suiji (); generate a random number. Next, generate the header file Head Of The WAV file. h and Head. cpp Head. h: basically, it is still in the format of the previous article. However, we encapsulate some things and copy the Code # ifndef HEAD_H _ # define HEAD_H _ class Head {public: Head (); void setsize (int); long int getsa (); long int getsize ();~ Head (); private: char RIFF [4]; long int size0; char WAVE [4]; char FMT [4]; long int size1; short int fmttag; short int channel; long int samplespersec; long int bytepersec; short int blockalign; short int bitpersamples; char DATA [4]; long int size2;}; # endif/* HEAD_H _ */copy the code Head. cpp: the basic configuration is still based on the original copy Code # include "Head. h "# include <iostream> using namespace std; Head: Head () {strcpy (RIFF," RIFF "); si Ze0 = 0; strcpy (WAVE, "WAVE"); strcpy (FMT, "fmt"); size1 = 16; fmttag = 1; channel = 1; samplespersec = 11025; bytepersec = 11025; blockalign = 1; bitpersamples = 8; strcpy (DATA, "data"); size2 = 0; cout <RIFF <size0 <WAVE <endl ;} head ::~ Head () {cout <generation completed <endl;} void Head: setsize (int size) {Head: size0 = size + 24; Head :: size2 = size;} long int Head: getsa () {return Head: samplespersec;} long int Head: getsize () {return Head: size2 ;} the last part of the code to be copied is the main file. cpp: the basic method is relatively simple. dou [8] Stores dou ruai mi fa in C major... to the frequency of dou. Because one-second sampling is 11025 (as set above), pai stores the number of samples in 1/4 time. Then, set the total number of samples to 44100, that is, 4 seconds, and set the data size to 44100. Next, it is similar to the previous one and is generated. Copy the Code # include <iostream> # include <fstream> # include "Head. h "# include <math. h> # include "R. h "using namespace std; int dou [8] = {262,294,330,349,392,440,494,524}; int pai = 11025/4; void runrun () {Head head; R r; head. setsize (44100); char body [head. getsize ()]; int I, i2; for (i2 = 0; i2 <16; i2 ++) {int B = r. suiji (7); for (I = 0; I <pai; I ++) {float a = (head. getsa ()/dou [B]); body [I + i2 * pai] = (int) (64 * sin (6.28/a * I) + 128 ); cout <a <B <endl ;}} ofstream ocout; ocout. open ("1213.wav", ios: out | ios: binary); ocout. write (char *) & head, sizeof head); ocout. write (char *) & body, sizeof body); ocout. close (); cout

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.