Advertisement
TELunus

SoundBox

Jan 13th, 2013
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.88 KB | None | 0 0
  1. #include <iostream>
  2. #include "windows.h"
  3. #include <cmath>
  4.  
  5. static const int sucsessfulExitStatus = 0;
  6.  
  7. LPSTR sineBlock(WAVEFORMATEX format, double frequency, double duration, int amplitude);
  8.  
  9. int main()
  10. {
  11.     int exitStatus = sucsessfulExitStatus;
  12.  
  13.     WAVEFORMATEX format = {0};
  14.     format.wFormatTag = WAVE_FORMAT_PCM;
  15.     format.nChannels = 1;
  16.     format.nSamplesPerSec = 44100;
  17.     format.wBitsPerSample = 8;
  18.     format.nBlockAlign = format.nChannels * format.wBitsPerSample / 8;//data must be dealt with in multiples of this many bytes
  19.     format.nAvgBytesPerSec = format.nSamplesPerSec * format.nBlockAlign;
  20.     format.cbSize = 0;
  21.  
  22.     LPHWAVEOUT deviceHandleAddress = new(HWAVEOUT__*);
  23.  
  24.     MMRESULT DeviceOpeningResult = waveOutOpen(deviceHandleAddress, WAVE_MAPPER, &format, 0,0,CALLBACK_NULL );//open a device, store the result
  25.     switch (DeviceOpeningResult)
  26.     {
  27.         case MMSYSERR_NOERROR:
  28.         std::cout<<"Yay, no errors!"<<std::endl;
  29.         break;
  30.  
  31.         case MMSYSERR_ALLOCATED:
  32.         //That device is aleady allocated.
  33.         break;
  34.  
  35.         case MMSYSERR_BADDEVICEID:
  36.         //That's not a device ID!
  37.         break;
  38.  
  39.         case MMSYSERR_NODRIVER:
  40.         //There's no driver for your device?
  41.         break;
  42.  
  43.         case MMSYSERR_NOMEM:
  44.         //Oops, out of memory!
  45.         break;
  46.  
  47.         case WAVERR_BADFORMAT:
  48.         //That's not a valid format!
  49.         break;
  50.  
  51.         case WAVERR_SYNC:
  52.         //Try WAVE_ALLOWSYNC.
  53.         break;
  54.  
  55.         default:
  56.         //Good work, you're not even supposed to be able to get here.
  57.         std::cout<<"is this what my syntax needs?"<<std::endl;
  58.         }
  59.  
  60.     LPSTR firstSoundBlock = sineBlock(format, 44100/40.0,60.0,2);//pointer to the first sound block
  61.  
  62.     LPWAVEHDR firstWaveHeader = new WAVEHDR;
  63.     (*firstWaveHeader).lpData = firstSoundBlock;
  64.     (*firstWaveHeader).dwBufferLength = 44100*60;
  65.     (*firstWaveHeader).dwFlags = 0;
  66.  
  67.     MMRESULT preparationResult = waveOutPrepareHeader(*deviceHandleAddress,firstWaveHeader,sizeof(WAVEHDR));
  68.  
  69.             switch (preparationResult)
  70.     {
  71.         case MMSYSERR_NOERROR:
  72.         //Yay, no errors!
  73.         std::cout<<"things are going well."<<std::endl;
  74.         break;
  75.  
  76.         case MMSYSERR_INVALHANDLE:
  77.         //Idiot, you used an invalid handle!
  78.         break;
  79.  
  80.         case MMSYSERR_NODRIVER:
  81.         //There's no driver for your device?
  82.         break;
  83.  
  84.         case MMSYSERR_NOMEM:
  85.         //Oops, out of memory!
  86.         break;
  87.  
  88.         default:
  89.         //Good work, you're not even supposed to be able to get here.
  90.         std::cout<<"is this what my syntax needs?"<<std::endl;
  91.         }
  92.  
  93.     MMRESULT writeResult = waveOutWrite(*deviceHandleAddress,firstWaveHeader,sizeof(WAVEHDR));
  94.  
  95.         switch (writeResult)
  96.     {
  97.         case MMSYSERR_NOERROR:
  98.         //Yay, no errors!
  99.         std::cout<<"this is good"<<std::endl;
  100.         break;
  101.  
  102.         case MMSYSERR_INVALHANDLE:
  103.         //Idiot, you used an invalid handle!
  104.         break;
  105.  
  106.         case MMSYSERR_NODRIVER:
  107.         //There's no driver for your device?
  108.         break;
  109.  
  110.         case MMSYSERR_NOMEM:
  111.         //Oops, out of memory!
  112.         break;
  113.  
  114.         case WAVERR_UNPREPARED:
  115.         //You should prepare your data block first.
  116.         break;
  117.  
  118.         default:
  119.         //Good work, you're not even supposed to be able to get here.
  120.         std::cout<<"is this what my syntax needs?"<<std::endl;
  121.         }
  122.  
  123.  
  124. //  if(PlaySound(TEXT("C:\\Users\\TELunus\\Music\\manedown3.mp3"), NULL, SND_FILENAME|SND_ASYNC))
  125. //  {
  126. //      std::cout<<"it worked!"<<std::endl;
  127. //  }
  128. //  else
  129. //  {
  130. //      std::cout<<"Nope!"<<std::endl;
  131. //  }
  132.  
  133.     system("PAUSE");
  134.  
  135.     return exitStatus;
  136. }
  137.  
  138. LPSTR sineBlock(WAVEFORMATEX format, double frequency, double duration, int amplitude)//only for 1 channel 8 bit for now.
  139. {
  140.     unsigned int numSamples = format.nSamplesPerSec*duration;
  141.     double omega = frequency/3.14159;
  142.     double timeInterval = 1.0;
  143.     timeInterval /= format.nSamplesPerSec;
  144.     std::cout<<"omega is "<<omega<<std::endl;
  145.     LPSTR data = new char[format.nBlockAlign*numSamples];
  146.     for(unsigned int i = 0; i < numSamples; i++)
  147.     {
  148.         //for(unsigned int j = 0; j < format.nBlockAlign; j++)
  149.         //{
  150.  
  151.         double time = timeInterval*i;
  152.  
  153.         data[format.nBlockAlign*i] = amplitude*(sin(omega*time));
  154.         //}
  155.     }
  156.     return data;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement