Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "windows.h"
- #include <cmath>
- static const int sucsessfulExitStatus = 0;
- LPSTR sineBlock(WAVEFORMATEX format, double frequency, double duration, int amplitude);
- int main()
- {
- int exitStatus = sucsessfulExitStatus;
- WAVEFORMATEX format = {0};
- format.wFormatTag = WAVE_FORMAT_PCM;
- format.nChannels = 1;
- format.nSamplesPerSec = 44100;
- format.wBitsPerSample = 8;
- format.nBlockAlign = format.nChannels * format.wBitsPerSample / 8;//data must be dealt with in multiples of this many bytes
- format.nAvgBytesPerSec = format.nSamplesPerSec * format.nBlockAlign;
- format.cbSize = 0;
- LPHWAVEOUT deviceHandleAddress = new(HWAVEOUT__*);
- MMRESULT DeviceOpeningResult = waveOutOpen(deviceHandleAddress, WAVE_MAPPER, &format, 0,0,CALLBACK_NULL );//open a device, store the result
- switch (DeviceOpeningResult)
- {
- case MMSYSERR_NOERROR:
- std::cout<<"Yay, no errors!"<<std::endl;
- break;
- case MMSYSERR_ALLOCATED:
- //That device is aleady allocated.
- break;
- case MMSYSERR_BADDEVICEID:
- //That's not a device ID!
- break;
- case MMSYSERR_NODRIVER:
- //There's no driver for your device?
- break;
- case MMSYSERR_NOMEM:
- //Oops, out of memory!
- break;
- case WAVERR_BADFORMAT:
- //That's not a valid format!
- break;
- case WAVERR_SYNC:
- //Try WAVE_ALLOWSYNC.
- break;
- default:
- //Good work, you're not even supposed to be able to get here.
- std::cout<<"is this what my syntax needs?"<<std::endl;
- }
- LPSTR firstSoundBlock = sineBlock(format, 44100/40.0,60.0,2);//pointer to the first sound block
- LPWAVEHDR firstWaveHeader = new WAVEHDR;
- (*firstWaveHeader).lpData = firstSoundBlock;
- (*firstWaveHeader).dwBufferLength = 44100*60;
- (*firstWaveHeader).dwFlags = 0;
- MMRESULT preparationResult = waveOutPrepareHeader(*deviceHandleAddress,firstWaveHeader,sizeof(WAVEHDR));
- switch (preparationResult)
- {
- case MMSYSERR_NOERROR:
- //Yay, no errors!
- std::cout<<"things are going well."<<std::endl;
- break;
- case MMSYSERR_INVALHANDLE:
- //Idiot, you used an invalid handle!
- break;
- case MMSYSERR_NODRIVER:
- //There's no driver for your device?
- break;
- case MMSYSERR_NOMEM:
- //Oops, out of memory!
- break;
- default:
- //Good work, you're not even supposed to be able to get here.
- std::cout<<"is this what my syntax needs?"<<std::endl;
- }
- MMRESULT writeResult = waveOutWrite(*deviceHandleAddress,firstWaveHeader,sizeof(WAVEHDR));
- switch (writeResult)
- {
- case MMSYSERR_NOERROR:
- //Yay, no errors!
- std::cout<<"this is good"<<std::endl;
- break;
- case MMSYSERR_INVALHANDLE:
- //Idiot, you used an invalid handle!
- break;
- case MMSYSERR_NODRIVER:
- //There's no driver for your device?
- break;
- case MMSYSERR_NOMEM:
- //Oops, out of memory!
- break;
- case WAVERR_UNPREPARED:
- //You should prepare your data block first.
- break;
- default:
- //Good work, you're not even supposed to be able to get here.
- std::cout<<"is this what my syntax needs?"<<std::endl;
- }
- // if(PlaySound(TEXT("C:\\Users\\TELunus\\Music\\manedown3.mp3"), NULL, SND_FILENAME|SND_ASYNC))
- // {
- // std::cout<<"it worked!"<<std::endl;
- // }
- // else
- // {
- // std::cout<<"Nope!"<<std::endl;
- // }
- system("PAUSE");
- return exitStatus;
- }
- LPSTR sineBlock(WAVEFORMATEX format, double frequency, double duration, int amplitude)//only for 1 channel 8 bit for now.
- {
- unsigned int numSamples = format.nSamplesPerSec*duration;
- double omega = frequency/3.14159;
- double timeInterval = 1.0;
- timeInterval /= format.nSamplesPerSec;
- std::cout<<"omega is "<<omega<<std::endl;
- LPSTR data = new char[format.nBlockAlign*numSamples];
- for(unsigned int i = 0; i < numSamples; i++)
- {
- //for(unsigned int j = 0; j < format.nBlockAlign; j++)
- //{
- double time = timeInterval*i;
- data[format.nBlockAlign*i] = amplitude*(sin(omega*time));
- //}
- }
- return data;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement