wavfile.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. A simple sound library for CSE 20211 by Douglas Thain
  3. For course assignments, you should not change this file.
  4. For complete documentation, see:
  5. http://www.nd.edu/~dthain/courses/cse20211/fall2013/wavfile
  6. */
  7. #include "wavfile.h"
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. FILE* wavfile_write_open(const char* filename)
  15. {
  16. struct wavfile_header header;
  17. int samples_per_second = WAVFILE_SAMPLES_PER_SECOND;
  18. int bits_per_sample = 16;
  19. strncpy(header.riff_tag, "RIFF", 4);
  20. strncpy(header.wave_tag, "WAVE", 4);
  21. strncpy(header.fmt_tag, "fmt ", 4);
  22. strncpy(header.data_tag, "data", 4);
  23. header.riff_length = 0;
  24. header.fmt_length = 16;
  25. header.audio_format = 1;
  26. header.num_channels = 1;
  27. header.sample_rate = samples_per_second;
  28. header.byte_rate = samples_per_second * (bits_per_sample / 8);
  29. header.block_align = bits_per_sample / 8;
  30. header.bits_per_sample = bits_per_sample;
  31. header.data_length = 0;
  32. FILE* file = fopen(filename, "w+");
  33. if (!file) return 0;
  34. fwrite(&header, sizeof(header), 1, file);
  35. fflush(file);
  36. return file;
  37. }
  38. FILE* wavfile_read_open(const char* filename, struct wavfile_header* header)
  39. {
  40. FILE* file = 0;
  41. if (header != NULL)
  42. {
  43. file = fopen(filename, "rb");
  44. if (!file)
  45. return 0;
  46. int r = fread(header, sizeof(struct wavfile_header), 1, file);
  47. r = sizeof(struct wavfile_header);
  48. printf("%d\n", r);
  49. }
  50. else
  51. {
  52. return 0;
  53. }
  54. return file;
  55. }
  56. long wavfile_read(FILE* file, short data[], int length)
  57. {
  58. return fread(data, sizeof(short), length, file);
  59. }
  60. void wavfile_write(FILE* file, short data[], int length)
  61. {
  62. fwrite(data, sizeof(short), length, file);
  63. }
  64. void wavfile_write_close(FILE* file)
  65. {
  66. int file_length = ftell(file);
  67. int data_length = file_length - sizeof(struct wavfile_header);
  68. fseek(file, sizeof(struct wavfile_header) - sizeof(int), SEEK_SET);
  69. fwrite(&data_length, sizeof(data_length), 1, file);
  70. int riff_length = file_length - 8;
  71. fseek(file, 4, SEEK_SET);
  72. fwrite(&riff_length, sizeof(riff_length), 1, file);
  73. fflush(file);
  74. fclose(file);
  75. }
  76. /*read the data from specified postion and channel*/
  77. static short wavfile_read_pos(FILE* file, int channel_num, int channel_index, int pos)
  78. {
  79. short value;
  80. long index = 44 + (pos * channel_num + channel_index) * sizeof(short);
  81. fseek(file, index, SEEK_SET);
  82. fread(&value, sizeof(short), 1, file);
  83. return value;
  84. }
  85. void wavfile_read_channel(FILE* file, short data[], int channel_num, int channel_index, int start_pos, int length)
  86. {
  87. int i;
  88. for (i = 0; i < length; ++i)
  89. {
  90. data[i] = wavfile_read_pos(file, channel_num, channel_index, start_pos + i);
  91. }
  92. }
  93. #ifdef __cplusplus
  94. }
  95. #endif