Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- #include <iostream>
- #include <mmsystem.h>
- #pragma comment(lib, "winmm.lib")
- int main(int argc, const char* argv[])
- {
- FILE *file;
- long file_size;
- char *wave_data = NULL;
- HWAVEOUT wave_out;
- WAVEFORMATEX wave_format;
- WAVEHDR wave_hdr;
- if (argc > 1)
- {
- fopen_s(&file, argv[1], "rb");
- if (file)
- {
- fseek(file, 0, SEEK_END);
- file_size = ftell(file);
- fseek(file, 0, SEEK_SET);
- if (file_size)
- {
- try
- {
- wave_data = new char[file_size];
- }
- catch (...)
- {
- wave_data = NULL;
- fclose(file);
- exit(-1);
- }
- if (fread(wave_data, sizeof(char), file_size, file) == file_size)
- {
- fclose(file);
- wave_format.wFormatTag = WAVE_FORMAT_PCM;
- wave_format.nChannels = 1;
- wave_format.nSamplesPerSec = /*8192*/48000;
- wave_format.wBitsPerSample = 16;
- wave_format.nBlockAlign = (wave_format.nChannels * wave_format.wBitsPerSample) / 8;
- wave_format.nAvgBytesPerSec = wave_format.nSamplesPerSec * wave_format.nBlockAlign;
- wave_format.cbSize = 0; // ?
- if (!waveOutOpen(&wave_out, 0, &wave_format, NULL, NULL, CALLBACK_NULL | WAVE_FORMAT_DIRECT))
- {
- wave_hdr.lpData = wave_data;
- wave_hdr.dwBufferLength = file_size;
- wave_hdr.dwBytesRecorded = 0; // ?
- wave_hdr.dwUser = NULL;
- wave_hdr.dwFlags = 0;
- wave_hdr.dwLoops = 1;
- waveOutPrepareHeader(wave_out, &wave_hdr, sizeof(WAVEHDR));
- std::wcout << L"Playing \"" << argv[1] << L"\"...\n";
- waveOutWrite(wave_out, &wave_hdr, sizeof(WAVEHDR));
- waveOutClose(wave_out);
- }
- }
- else fclose(file);
- }
- }
- }
- if (wave_data)
- {
- getchar();
- delete wave_data;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment