2
0

wavreader.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* ------------------------------------------------------------------
  2. * Copyright (C) 2009 Martin Storsjo
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  13. * express or implied.
  14. * See the License for the specific language governing permissions
  15. * and limitations under the License.
  16. * -------------------------------------------------------------------
  17. */
  18. #include "wavreader.h"
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <stdint.h>
  23. #include <assert.h>
  24. #define TAG(a, b, c, d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
  25. struct wav_reader {
  26. FILE *wav;
  27. uint32_t data_length;
  28. int format;
  29. int sample_rate;
  30. int bits_per_sample;
  31. int channels;
  32. int byte_rate;
  33. int block_align;
  34. };
  35. static uint32_t read_tag(struct wav_reader* wr) {
  36. uint32_t tag = 0;
  37. tag = (tag << 8) | fgetc(wr->wav);
  38. tag = (tag << 8) | fgetc(wr->wav);
  39. tag = (tag << 8) | fgetc(wr->wav);
  40. tag = (tag << 8) | fgetc(wr->wav);
  41. return tag;
  42. }
  43. static uint32_t read_int32(struct wav_reader* wr) {
  44. uint32_t value = 0;
  45. value |= fgetc(wr->wav) << 0;
  46. value |= fgetc(wr->wav) << 8;
  47. value |= fgetc(wr->wav) << 16;
  48. value |= fgetc(wr->wav) << 24;
  49. return value;
  50. }
  51. static uint16_t read_int16(struct wav_reader* wr) {
  52. uint16_t value = 0;
  53. value |= fgetc(wr->wav) << 0;
  54. value |= fgetc(wr->wav) << 8;
  55. return value;
  56. }
  57. void* wav_read_open(const char *filename) {
  58. struct wav_reader* wr = (struct wav_reader*) malloc(sizeof(*wr));
  59. long data_pos = 0;
  60. memset(wr, 0, sizeof(*wr));
  61. wr->wav = fopen(filename, "rb");
  62. if (wr->wav == NULL) {
  63. free(wr);
  64. return NULL;
  65. }
  66. while (1) {
  67. uint32_t tag, tag2, length;
  68. tag = read_tag(wr);
  69. if (feof(wr->wav))
  70. break;
  71. length = read_int32(wr);
  72. if (tag != TAG('R', 'I', 'F', 'F') || length < 4) {
  73. fseek(wr->wav, length, SEEK_CUR);
  74. continue;
  75. }
  76. tag2 = read_tag(wr);
  77. length -= 4;
  78. if (tag2 != TAG('W', 'A', 'V', 'E')) {
  79. fseek(wr->wav, length, SEEK_CUR);
  80. continue;
  81. }
  82. // RIFF chunk found, iterate through it
  83. while (length >= 8) {
  84. uint32_t subtag, sublength;
  85. subtag = read_tag(wr);
  86. if (feof(wr->wav))
  87. break;
  88. sublength = read_int32(wr);
  89. length -= 8;
  90. if (length < sublength)
  91. break;
  92. if (subtag == TAG('f', 'm', 't', ' ')) {
  93. if (sublength < 16) {
  94. // Insufficient data for 'fmt '
  95. break;
  96. }
  97. wr->format = read_int16(wr);
  98. wr->channels = read_int16(wr);
  99. wr->sample_rate = read_int32(wr);
  100. wr->byte_rate = read_int32(wr);
  101. wr->block_align = read_int16(wr);
  102. wr->bits_per_sample = read_int16(wr);
  103. } else if (subtag == TAG('d', 'a', 't', 'a')) {
  104. data_pos = ftell(wr->wav);
  105. wr->data_length = sublength;
  106. fseek(wr->wav, sublength, SEEK_CUR);
  107. } else {
  108. fseek(wr->wav, sublength, SEEK_CUR);
  109. }
  110. length -= sublength;
  111. }
  112. if (length > 0) {
  113. // Bad chunk?
  114. fseek(wr->wav, length, SEEK_CUR);
  115. }
  116. }
  117. fseek(wr->wav, data_pos, SEEK_SET);
  118. return wr;
  119. }
  120. void wav_read_close(void* obj) {
  121. struct wav_reader* wr = (struct wav_reader*) obj;
  122. fclose(wr->wav);
  123. free(wr);
  124. }
  125. int wav_get_header(void* obj, int* format, int* channels, int* sample_rate, int* bits_per_sample, unsigned int* data_length) {
  126. struct wav_reader* wr = (struct wav_reader*) obj;
  127. if (format)
  128. *format = wr->format;
  129. if (channels)
  130. *channels = wr->channels;
  131. if (sample_rate)
  132. *sample_rate = wr->sample_rate;
  133. if (bits_per_sample)
  134. *bits_per_sample = wr->bits_per_sample;
  135. if (data_length)
  136. *data_length = wr->data_length;
  137. return wr->format && wr->sample_rate;
  138. }
  139. int wav_read_data(void* obj, unsigned char* data, unsigned int length) {
  140. struct wav_reader* wr = (struct wav_reader*) obj;
  141. size_t n;
  142. if (wr->wav == NULL)
  143. return -1;
  144. if (length > wr->data_length)
  145. length = wr->data_length;
  146. n = fread(data, 1, length, wr->wav);
  147. wr->data_length -= length;
  148. return (int)n;
  149. }
  150. int wav_duration(const char* fileName) {
  151. void* wav= wav_read_open(fileName);
  152. if (0 == wav) {
  153. return 0;
  154. }
  155. int format, channels, sample_rate, bits_per_sample;
  156. unsigned int data_length;
  157. wav_get_header(wav, &format, &channels, &sample_rate, &bits_per_sample, &data_length);
  158. wav_read_close(wav);
  159. int bytes_per_frame = bits_per_sample/8*channels;
  160. int bytes_per_sec = bytes_per_frame*sample_rate;
  161. assert(bytes_per_sec > 0);
  162. if (0 == bytes_per_sec) {
  163. return 0;
  164. }
  165. int sec = 1000*data_length/bytes_per_sec;
  166. return sec;
  167. }