WAV

  1. Play a WAV file
    1. Save a WAV file in a local directory, for example, "D:\Waste\ncnu.wav".
    2. Create a Win32 Console project.
    3. Call the PlaySound() function.
      
      #include <Windows.h>
      
      int main()
      {
          PlaySound(TEXT("D:\\Waste\\ncnu.wav"), NULL, SND_FILENAME);
          return 0;
      }
      
      
    4. From the menu, choose Project - Properties (Alt-F7) - Configuration Properties - Linker - Input - Additional Dependencies, add "winmm.lib".
    5. Press F7 to compile. Press Ctrl-F5 to run.
  2. Play WAV files
    1. Suppose you have a list of WAV files of DTMF tones.
    2. Ask the user to input a phone number (e.g., "0492910960").
    3. Loop through each digit and play the DTMF tones.
    4. Sample code:
      
      #define _CRT_SECURE_NO_WARNINGS
      #include <iostream>     // You need this for swprintf()
      #include <Windows.h>
      
      int main()
      {
              char phoneNumber[] = "0492910960";
              char fn[80];
              for (size_t i=0; i<strlen(phoneNumber); i++)
              {
                      sprintf(fn, "D:\\Waste\\DTMF%c.wav", phoneNumber[i]);
                      PlaySound(fn, NULL, SND_FILENAME);
              }
              return 0;
      }
      
      
    5. Error: cannot convert parameter 1 from 'char' to 'LPCWSTR'
      • By default, VC assumes you are using Unicode.
      • Project - Properties - General - Character Set - Not Set
    6. If VC complains that sprintf() is not safe, add "#define _CRT_SECURE_NO_WARNINGS" in the source program before you include "iostream".
    7. You may have observed that there might be a brief pause between each digit tone. This delay was caused by file reading. If you want to eliminate that, you should load all the WAV files into the memory before you start playing them.
  3. Determine the size of a binary file
    1. Accept a filename from the command-line argument: argv[1].
    2. fopen(), fclose()
    3. fseek(), ftell()
    4. Sample code:
      
      #define _CRT_SECURE_NO_WARNINGS
      #include <iostream>
      using std::cout;
      using std::endl;
      
      int main(int argc, char* argv[])
      {
          char fn[20] = "a.out";
          if (argc == 2)
              strcpy(fn, argv[1]);
          FILE *fp = fopen(fn, "rb");
          fseek(fp, 0L, SEEK_END);
          int length = ftell(fp);
          cout << length << endl;
          fclose(fp);
          return 0;
      }
      
      
  4. Reading a binary file into the memory
    1. After determining the size of the file, call rewind() to move the file position to the beginning.
    2. Now call fread() to read 16 bytes into a buffer (a char array). Dump each byte in its ASCII code (hexadecimal) and character format.
    3. Repeat reading data from the file until EOF is encountered.
    4. The result may look like
      
      000000   47 6f 6f 64 20 6d 6f 72   6e 69 6e 67 2e 20 20 49 Good morning.  I
      000010   20 77 61 6e 74 20 74 6f   20 72 75 6e 20 61 20 74  want to run a t
      000020   65 73 74 2e 0a 49 20 62   6f 75 67 68 74 20 61 20 est..I bought a
      000030   63 61 72 20 69 6e 20 32   30 30 38 2c 0a          car in 2008,.
      
      
    5. Sample code
  5. Load a WAV file into the memory and play it.
    1. After you determine the size of the file, dynamically allocate a buffer by char* buffer = new char[length];
    2. Then call fread(buffer, 1, length, fp); to load the file into the memory buffer.
    3. Now you may call PlaySound(buffer, NULL, SND_MEMORY); to play it.
    4. Sample code