Advertisement
Hemirt

Untitled

Jun 2nd, 2016
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.51 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include "wave.h"
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8.  
  9. char riff[4] = { 'R', 'I', 'F', 'F' };
  10. char wave[4] = { 'W', 'A', 'V', 'E' };
  11. char fmt[4]  = { 'f', 'm', 't', ' ' };
  12. char dat[4]  = { 'd', 'a', 't', 'a' };
  13.  
  14. void writeWave(const char* filename, int freq, int channels, short* buffer, int samples) {
  15.     struct WaveHeader wh;
  16.     int i;
  17.     int elemWritten;
  18.     FILE *f;
  19.  
  20.     for(i=0; i<4; i++) {
  21.         wh.riffId[i] = riff[i];
  22.         wh.riffType[i] = wave[i];
  23.         wh.fmtId[i] = fmt[i];
  24.         wh.dataId[i] = dat[i];
  25.     }
  26.  
  27.     /* Format chunk */
  28.     wh.fmtLen = 16;
  29.     wh.formatTag = 1;  /* PCM */
  30.     wh.channels = channels;  
  31.     wh.samplesPerSec =  freq;
  32.     wh.avgBytesPerSec = freq * channels * sizeof(short);
  33.     wh.blockAlign = channels * sizeof(short);
  34.     wh.bitsPerSample = sizeof(short)*8;
  35.     wh.dataLen = samples * channels * sizeof(short);
  36.     wh.len = 36 + wh.dataLen;
  37.  
  38.     f = fopen(filename,"wb");
  39.     if (!f) {
  40.         printf("error: could not write wave\n");
  41.         return;
  42.     }
  43.  
  44.     elemWritten = fwrite(&wh, sizeof(wh), 1, f);
  45.     if (elemWritten) elemWritten = fwrite(buffer, wh.dataLen, 1, f);
  46.     fclose(f);
  47.    
  48.     if (!elemWritten){
  49.         printf("error: could not write wave\n");
  50.     }
  51. }
  52.  
  53. int readWave(const char* filename, int* freq, int* channels, short** buffer, int* samples) {
  54.     struct WaveHeader wh;
  55.     FILE *f;
  56.     int i;
  57.     int elemsRead;
  58.  
  59.     memset(&wh, 0, sizeof(wh));
  60.  
  61.     f = fopen(filename,"rb");
  62.     if (!f) {
  63.         printf("error: could not open wave %s\n",filename);
  64.         return 0;
  65.     }
  66.  
  67.     fread(&wh, sizeof(wh), 1, f);
  68.    
  69.     for(i=0; i<4; i++) {
  70.         if ((wh.riffId[i] != riff[i]) ||
  71.             (wh.riffType[i] != wave[i]) ||
  72.             (wh.fmtId[i] != fmt[i]) ||
  73.             (wh.dataId[i] != dat[i])){
  74.                 goto closeError;
  75.         }
  76.     }
  77.     // Format chunk
  78.     if (wh.fmtLen != 16) goto closeError;
  79.     if (wh.formatTag != 1) goto closeError;
  80.     *channels = wh.channels;
  81.     if (*channels <1 || *channels >2) goto closeError;
  82.     *freq = wh.samplesPerSec;
  83.     if (wh.blockAlign != *channels * sizeof(short)) goto closeError;
  84.     *samples = wh.dataLen / (*channels * sizeof(short));
  85.  
  86.     if (*samples < *freq) {
  87.         fclose(f);
  88.         printf("error: wave file is too short\n");
  89.         return 0;
  90.     }
  91.    
  92.     (*buffer) = (short*) malloc(wh.dataLen);
  93.     if (!*buffer){
  94.         printf("error: could not allocate memory for wave\n");
  95.         return 0;
  96.     }
  97.  
  98.     elemsRead = fread(*buffer, wh.dataLen, 1, f);
  99.     fclose(f);
  100.     if (elemsRead != 1){
  101.         printf("error: reading wave file\n");
  102.         return 0;
  103.     }
  104.  
  105.     return 1;
  106.  
  107. closeError:
  108.     fclose(f);
  109.     printf("error: invalid wave file %s\n",filename);
  110.     return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement