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.

243 lines
6.9 KiB

  1. // Copyright (c) 2016 Personal (Binbin Zhang)
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef FRONTEND_WAV_H_
  15. #define FRONTEND_WAV_H_
  16. #include <assert.h>
  17. #include <stdint.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <string>
  22. namespace wenet {
  23. struct WavHeader {
  24. char riff[4] = {'R', 'I', 'F', 'F'};
  25. unsigned int size = 0;
  26. char wav[4] = {'W', 'A', 'V', 'E'};
  27. char fmt[4] = {'f', 'm', 't', ' '};
  28. unsigned int fmt_size = 16;
  29. uint16_t format = 1;
  30. uint16_t channels = 0;
  31. unsigned int sample_rate = 0;
  32. unsigned int bytes_per_second = 0;
  33. uint16_t block_size = 0;
  34. uint16_t bit = 0;
  35. char data[4] = {'d', 'a', 't', 'a'};
  36. unsigned int data_size = 0;
  37. WavHeader() {}
  38. WavHeader(int num_samples, int num_channel, int sample_rate,
  39. int bits_per_sample) {
  40. data_size = num_samples * num_channel * (bits_per_sample / 8);
  41. size = sizeof(WavHeader) - 8 + data_size;
  42. channels = num_channel;
  43. this->sample_rate = sample_rate;
  44. bytes_per_second = sample_rate * num_channel * (bits_per_sample / 8);
  45. block_size = num_channel * (bits_per_sample / 8);
  46. bit = bits_per_sample;
  47. }
  48. };
  49. class WavReader {
  50. public:
  51. WavReader() : data_(nullptr) {}
  52. explicit WavReader(const std::string& filename) { Open(filename); }
  53. bool Open(const std::string& filename) {
  54. FILE* fp = fopen(filename.c_str(), "rb");
  55. if (NULL == fp) {
  56. LOG(WARNING) << "Error in read " << filename;
  57. return false;
  58. }
  59. WavHeader header;
  60. fread(&header, 1, sizeof(header), fp);
  61. if ((0 != strncmp(header.riff, "RIFF", 4)) ||
  62. (0 != strncmp(header.wav, "WAVE", 4)) ||
  63. (0 != strncmp(header.fmt, "fmt", 3))) {
  64. fprintf(stderr, "WaveData: expect audio format data.\n");
  65. return false;
  66. }
  67. if (header.fmt_size < 16) {
  68. fprintf(stderr,
  69. "WaveData: expect PCM format data "
  70. "to have fmt chunk of at least size 16.\n");
  71. fclose(fp);
  72. return false;
  73. } else if (header.fmt_size > 16) {
  74. int offset = 44 - 8 + header.fmt_size - 16;
  75. fseek(fp, offset, SEEK_SET);
  76. fread(header.data, 8, sizeof(char), fp);
  77. }
  78. // check "RIFF" "WAVE" "fmt " "data"
  79. // Skip any sub-chunks between "fmt" and "data". Usually there will
  80. // be a single "fact" sub chunk, but on Windows there can also be a
  81. // "list" sub chunk.
  82. while (0 != strncmp(header.data, "data", 4)) {
  83. // We will just ignore the data in these chunks.
  84. fseek(fp, header.data_size, SEEK_CUR);
  85. // read next sub chunk
  86. fread(header.data, 8, sizeof(char), fp);
  87. }
  88. num_channel_ = header.channels;
  89. sample_rate_ = header.sample_rate;
  90. bits_per_sample_ = header.bit;
  91. int num_data = header.data_size / (bits_per_sample_ / 8);
  92. data_ = new float[num_data];
  93. num_samples_ = num_data / num_channel_;
  94. for (int i = 0; i < num_data; ++i) {
  95. switch (bits_per_sample_) {
  96. case 8: {
  97. char sample;
  98. fread(&sample, 1, sizeof(char), fp);
  99. data_[i] = static_cast<float>(sample);
  100. break;
  101. }
  102. case 16: {
  103. int16_t sample;
  104. fread(&sample, 1, sizeof(int16_t), fp);
  105. data_[i] = static_cast<float>(sample);
  106. break;
  107. }
  108. case 32: {
  109. int sample;
  110. fread(&sample, 1, sizeof(int), fp);
  111. data_[i] = static_cast<float>(sample);
  112. break;
  113. }
  114. default:
  115. fprintf(stderr, "unsupported quantization bits");
  116. exit(1);
  117. }
  118. }
  119. fclose(fp);
  120. return true;
  121. }
  122. int num_channel() const { return num_channel_; }
  123. int sample_rate() const { return sample_rate_; }
  124. int bits_per_sample() const { return bits_per_sample_; }
  125. int num_samples() const { return num_samples_; }
  126. ~WavReader() { delete[] data_; }
  127. const float* data() const { return data_; }
  128. private:
  129. int num_channel_;
  130. int sample_rate_;
  131. int bits_per_sample_;
  132. int num_samples_; // sample points per channel
  133. float* data_;
  134. };
  135. class WavWriter {
  136. public:
  137. WavWriter(const float* data, int num_samples, int num_channel,
  138. int sample_rate, int bits_per_sample)
  139. : data_(data),
  140. num_samples_(num_samples),
  141. num_channel_(num_channel),
  142. sample_rate_(sample_rate),
  143. bits_per_sample_(bits_per_sample) {}
  144. void Write(const std::string& filename) {
  145. FILE* fp = fopen(filename.c_str(), "wb");
  146. WavHeader header(num_samples_, num_channel_, sample_rate_,
  147. bits_per_sample_);
  148. fwrite(&header, 1, sizeof(header), fp);
  149. for (int i = 0; i < num_samples_; ++i) {
  150. for (int j = 0; j < num_channel_; ++j) {
  151. switch (bits_per_sample_) {
  152. case 8: {
  153. char sample = static_cast<char>(data_[i * num_channel_ + j]);
  154. fwrite(&sample, 1, sizeof(sample), fp);
  155. break;
  156. }
  157. case 16: {
  158. int16_t sample = static_cast<int16_t>(data_[i * num_channel_ + j]);
  159. fwrite(&sample, 1, sizeof(sample), fp);
  160. break;
  161. }
  162. case 32: {
  163. int sample = static_cast<int>(data_[i * num_channel_ + j]);
  164. fwrite(&sample, 1, sizeof(sample), fp);
  165. break;
  166. }
  167. }
  168. }
  169. }
  170. fclose(fp);
  171. }
  172. private:
  173. const float* data_;
  174. int num_samples_; // total float points in data_
  175. int num_channel_;
  176. int sample_rate_;
  177. int bits_per_sample_;
  178. };
  179. class StreamWavWriter {
  180. public:
  181. StreamWavWriter(int num_channel, int sample_rate, int bits_per_sample)
  182. : num_channel_(num_channel),
  183. sample_rate_(sample_rate),
  184. bits_per_sample_(bits_per_sample),
  185. total_num_samples_(0) {}
  186. StreamWavWriter(const std::string& filename, int num_channel, int sample_rate,
  187. int bits_per_sample)
  188. : StreamWavWriter(num_channel, sample_rate, bits_per_sample) {
  189. Open(filename);
  190. }
  191. void Open(const std::string& filename) {
  192. fp_ = fopen(filename.c_str(), "wb");
  193. fseek(fp_, sizeof(WavHeader), SEEK_SET);
  194. }
  195. void Write(const int16_t* sample_data, size_t num_samples) {
  196. fwrite(sample_data, sizeof(int16_t), num_samples, fp_);
  197. total_num_samples_ += num_samples;
  198. }
  199. void Close() {
  200. WavHeader header(total_num_samples_, num_channel_, sample_rate_,
  201. bits_per_sample_);
  202. fseek(fp_, 0L, SEEK_SET);
  203. fwrite(&header, 1, sizeof(header), fp_);
  204. fclose(fp_);
  205. }
  206. private:
  207. FILE* fp_;
  208. int num_channel_;
  209. int sample_rate_;
  210. int bits_per_sample_;
  211. size_t total_num_samples_;
  212. };
  213. } // namespace wenet
  214. #endif // FRONTEND_WAV_H_