BigETI

main.cpp - BinToSound

Nov 4th, 2014
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.72 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <iostream>
  3. #include <mmsystem.h>
  4. #pragma comment(lib, "winmm.lib")
  5.  
  6. int main(int argc, const char* argv[])
  7. {
  8.     FILE *file;
  9.     long file_size;
  10.     char *wave_data = NULL;
  11.     HWAVEOUT wave_out;
  12.     WAVEFORMATEX wave_format;
  13.     WAVEHDR wave_hdr;
  14.     if (argc > 1)
  15.     {
  16.         fopen_s(&file, argv[1], "rb");
  17.         if (file)
  18.         {
  19.             fseek(file, 0, SEEK_END);
  20.             file_size = ftell(file);
  21.             fseek(file, 0, SEEK_SET);
  22.             if (file_size)
  23.             {
  24.                 try
  25.                 {
  26.                     wave_data = new char[file_size];
  27.                 }
  28.                 catch (...)
  29.                 {
  30.                     wave_data = NULL;
  31.                     fclose(file);
  32.                     exit(-1);
  33.                 }
  34.                 if (fread(wave_data, sizeof(char), file_size, file) == file_size)
  35.                 {
  36.                     fclose(file);
  37.                     wave_format.wFormatTag = WAVE_FORMAT_PCM;
  38.                     wave_format.nChannels = 1;
  39.                     wave_format.nSamplesPerSec = /*8192*/48000;
  40.                     wave_format.wBitsPerSample = 16;
  41.                     wave_format.nBlockAlign = (wave_format.nChannels * wave_format.wBitsPerSample) / 8;
  42.                     wave_format.nAvgBytesPerSec = wave_format.nSamplesPerSec * wave_format.nBlockAlign;
  43.                     wave_format.cbSize = 0; // ?
  44.                     if (!waveOutOpen(&wave_out, 0, &wave_format, NULL, NULL, CALLBACK_NULL | WAVE_FORMAT_DIRECT))
  45.                     {
  46.                         wave_hdr.lpData = wave_data;
  47.                         wave_hdr.dwBufferLength = file_size;
  48.                         wave_hdr.dwBytesRecorded = 0; // ?
  49.                         wave_hdr.dwUser = NULL;
  50.                         wave_hdr.dwFlags = 0;
  51.                         wave_hdr.dwLoops = 1;
  52.                         waveOutPrepareHeader(wave_out, &wave_hdr, sizeof(WAVEHDR));
  53.                         std::wcout << L"Playing \"" << argv[1] << L"\"...\n";
  54.                         waveOutWrite(wave_out, &wave_hdr, sizeof(WAVEHDR));
  55.                         waveOutClose(wave_out);
  56.                     }
  57.                 }
  58.                 else fclose(file);
  59.             }
  60.         }
  61.     }
  62.     if (wave_data)
  63.     {
  64.         getchar();
  65.         delete wave_data;
  66.     }
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment