2
0

wavwriter.mm 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 "wavwriter.h"
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include <stdint.h>
  23. struct wav_writer {
  24. NSMutableData *outData;
  25. NSMutableData *header;
  26. int data_length;
  27. int sample_rate;
  28. int bits_per_sample;
  29. int channels;
  30. };
  31. static void write_string(struct wav_writer* ww, const char *str) {
  32. // fputc(str[0], ww->wav);
  33. // fputc(str[1], ww->wav);
  34. // fputc(str[2], ww->wav);
  35. // fputc(str[3], ww->wav);
  36. [ww->header appendBytes:str length:4/*strlen(str)*/];
  37. }
  38. static void write_int32(struct wav_writer* ww, int value) {
  39. [ww->header appendBytes:&value length:4/*strlen(str)*/];
  40. // fputc((value >> 0) & 0xff, ww->wav);
  41. // fputc((value >> 8) & 0xff, ww->wav);
  42. // fputc((value >> 16) & 0xff, ww->wav);
  43. // fputc((value >> 24) & 0xff, ww->wav);
  44. }
  45. static void write_int16(struct wav_writer* ww, int value) {
  46. [ww->header appendBytes:&value length:2/*strlen(str)*/];
  47. // fputc((value >> 0) & 0xff, ww->wav);
  48. // fputc((value >> 8) & 0xff, ww->wav);
  49. }
  50. static void write_header(struct wav_writer* ww, int length) {
  51. int bytes_per_frame, bytes_per_sec;
  52. write_string(ww, "RIFF");
  53. write_int32(ww, 4 + 8 + 16 + 8 + length);
  54. write_string(ww, "WAVE");
  55. write_string(ww, "fmt ");
  56. write_int32(ww, 16);
  57. bytes_per_frame = ww->bits_per_sample/8*ww->channels;
  58. bytes_per_sec = bytes_per_frame*ww->sample_rate;
  59. write_int16(ww, 1); // Format
  60. write_int16(ww, ww->channels); // Channels
  61. write_int32(ww, ww->sample_rate); // Samplerate
  62. write_int32(ww, bytes_per_sec); // Bytes per sec
  63. write_int16(ww, bytes_per_frame); // Bytes per frame
  64. write_int16(ww, ww->bits_per_sample); // Bits per sample
  65. write_string(ww, "data");
  66. write_int32(ww, length);
  67. }
  68. void* wav_write_open(NSMutableData *header, NSMutableData *outData, int sample_rate, int bits_per_sample, int channels) {
  69. struct wav_writer* ww = (struct wav_writer*) malloc(sizeof(*ww));
  70. memset(ww, 0, sizeof(*ww));
  71. ww->outData = outData;
  72. ww->header = header;
  73. ww->data_length = 0;
  74. ww->sample_rate = sample_rate;
  75. ww->bits_per_sample = bits_per_sample;
  76. ww->channels = channels;
  77. //write_header(ww, ww->data_length);
  78. return ww;
  79. }
  80. void wav_write_close(void* obj) {
  81. struct wav_writer* ww = (struct wav_writer*) obj;
  82. write_header(ww, ww->data_length);
  83. free(ww);
  84. }
  85. void wav_write_data(void* obj, const unsigned char* data, int length) {
  86. struct wav_writer* ww = (struct wav_writer*) obj;
  87. ww->data_length += length;
  88. [ww->outData appendBytes:data length:length];
  89. }