wav.mm 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #import <UIKit/UIkit.h>
  19. #include "wav.h"
  20. void WavWriter::writeString(const char *str) {
  21. fputc(str[0], wav);
  22. fputc(str[1], wav);
  23. fputc(str[2], wav);
  24. fputc(str[3], wav);
  25. }
  26. void WavWriter::writeInt32(int value) {
  27. fputc((value >> 0) & 0xff, wav);
  28. fputc((value >> 8) & 0xff, wav);
  29. fputc((value >> 16) & 0xff, wav);
  30. fputc((value >> 24) & 0xff, wav);
  31. }
  32. void WavWriter::writeInt16(int value) {
  33. fputc((value >> 0) & 0xff, wav);
  34. fputc((value >> 8) & 0xff, wav);
  35. }
  36. void WavWriter::writeHeader(int length) {
  37. writeString("RIFF");
  38. writeInt32(4 + 8 + 20 + 8 + length); //将16改为20
  39. writeString("WAVE");
  40. writeString("fmt ");
  41. writeInt32(20);
  42. int bytesPerFrame = bitsPerSample/8*channels;
  43. int bytesPerSec = bytesPerFrame*sampleRate;
  44. writeInt16(1); // Format
  45. writeInt16(channels); // Channels
  46. writeInt32(sampleRate); // Samplerate
  47. writeInt32(bytesPerSec); // Bytes per sec
  48. writeInt16(bytesPerFrame); // Bytes per frame
  49. writeInt16(bitsPerSample); // Bits per sample
  50. writeInt32(0); //这儿需要字节对齐 nExSize
  51. writeString("data");
  52. writeInt32(length);
  53. }
  54. WavWriter::WavWriter(const char *filename, int sampleRate, int bitsPerSample, int channels)
  55. {
  56. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  57. NSString *documentPath = [paths objectAtIndex:0];
  58. NSString *filePath = [documentPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%s", filename]];
  59. NSLog(@"documentPath=%@", documentPath);
  60. wav = fopen([filePath cStringUsingEncoding:NSASCIIStringEncoding], "wb");
  61. if (wav == NULL)
  62. return;
  63. dataLength = 0;
  64. this->sampleRate = sampleRate;
  65. this->bitsPerSample = bitsPerSample;
  66. this->channels = channels;
  67. writeHeader(dataLength);
  68. }
  69. WavWriter::~WavWriter() {
  70. if (wav == NULL)
  71. return;
  72. fseek(wav, 0, SEEK_SET);
  73. writeHeader(dataLength);
  74. fclose(wav);
  75. }
  76. void WavWriter::writeData(const unsigned char* data, int length) {
  77. if (wav == NULL)
  78. return;
  79. fwrite(data, length, 1, wav);
  80. dataLength += length;
  81. }