Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WavFile.cpp 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. Copyright (C) 2006 Nasca Octavian Paul
  3. Author: Nasca Octavian Paul
  4. Mark McCurry
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of version 2 of the GNU General Public License
  7. as published by the Free Software Foundation.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License (version 2) for more details.
  12. You should have received a copy of the GNU General Public License (version 2)
  13. along with this program; if not, write to the Free Software Foundation,
  14. Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. */
  16. #include <cstdio>
  17. #include <cstring>
  18. #include <cstdlib>
  19. #include <iostream>
  20. #include "WavFile.h"
  21. using namespace std;
  22. WavFile::WavFile(string filename, int samplerate, int channels)
  23. :sampleswritten(0), samplerate(samplerate), channels(channels),
  24. file(fopen(filename.c_str(), "w"))
  25. {
  26. if(file) {
  27. cout << "INFO: Making space for wave file header" << endl;
  28. //making space for the header written at destruction
  29. char tmp[44];
  30. memset(tmp, 0, 44 * sizeof(char));
  31. fwrite(tmp, 1, 44, file);
  32. }
  33. }
  34. WavFile::~WavFile()
  35. {
  36. if(file) {
  37. cout << "INFO: Writing wave file header" << endl;
  38. unsigned int chunksize;
  39. rewind(file);
  40. fwrite("RIFF", 4, 1, file);
  41. chunksize = sampleswritten * 4 + 36;
  42. fwrite(&chunksize, 4, 1, file);
  43. fwrite("WAVEfmt ", 8, 1, file);
  44. chunksize = 16;
  45. fwrite(&chunksize, 4, 1, file);
  46. unsigned short int formattag = 1; //uncompresed wave
  47. fwrite(&formattag, 2, 1, file);
  48. unsigned short int nchannels = channels; //stereo
  49. fwrite(&nchannels, 2, 1, file);
  50. unsigned int samplerate_ = samplerate; //samplerate
  51. fwrite(&samplerate_, 4, 1, file);
  52. unsigned int bytespersec = samplerate * 2 * channels; //bytes/sec
  53. fwrite(&bytespersec, 4, 1, file);
  54. unsigned short int blockalign = 2 * channels; //2 channels * 16 bits/8
  55. fwrite(&blockalign, 2, 1, file);
  56. unsigned short int bitspersample = 16;
  57. fwrite(&bitspersample, 2, 1, file);
  58. fwrite("data", 4, 1, file);
  59. chunksize = sampleswritten * blockalign;
  60. fwrite(&chunksize, 4, 1, file);
  61. fclose(file);
  62. file = NULL;
  63. }
  64. }
  65. bool WavFile::good() const
  66. {
  67. return file;
  68. }
  69. void WavFile::writeStereoSamples(int nsmps, short int *smps)
  70. {
  71. if(file) {
  72. fwrite(smps, nsmps, 4, file);
  73. sampleswritten += nsmps;
  74. }
  75. }
  76. void WavFile::writeMonoSamples(int nsmps, short int *smps)
  77. {
  78. if(file) {
  79. fwrite(smps, nsmps, 2, file);
  80. sampleswritten += nsmps;
  81. }
  82. }