单军华
2018-07-11 7b02207537d35bfa1714bf8beafc921f717d100a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* ------------------------------------------------------------------
 * Copyright (C) 2009 Martin Storsjo
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 * -------------------------------------------------------------------
 */
 
#import <UIKit/UIKit.h>
#include "wav.h"
 
void WavWriter::writeString(const char *str) {
    fputc(str[0], wav);
    fputc(str[1], wav);
    fputc(str[2], wav);
    fputc(str[3], wav);
}
 
void WavWriter::writeInt32(int value) {
    fputc((value >>  0) & 0xff, wav);
    fputc((value >>  8) & 0xff, wav);
    fputc((value >> 16) & 0xff, wav);
    fputc((value >> 24) & 0xff, wav);
}
 
void WavWriter::writeInt16(int value) {
    fputc((value >> 0) & 0xff, wav);
    fputc((value >> 8) & 0xff, wav);
}
 
void WavWriter::writeHeader(int length) {
    writeString("RIFF");
    writeInt32(4 + 8 + 20 + 8 + length);  //将16改为20
    writeString("WAVE");
    
    writeString("fmt ");
    writeInt32(20);
    
    int bytesPerFrame = bitsPerSample/8*channels;
    int bytesPerSec = bytesPerFrame*sampleRate;
    writeInt16(1);             // Format
    writeInt16(channels);      // Channels
    writeInt32(sampleRate);    // Samplerate
    writeInt32(bytesPerSec);   // Bytes per sec
    writeInt16(bytesPerFrame); // Bytes per frame
    writeInt16(bitsPerSample); // Bits per sample
    
    writeInt32(0);             //这儿需要字节对齐  nExSize
    
    writeString("data");
    writeInt32(length);
}
 
WavWriter::WavWriter(const char *filename, int sampleRate, int bitsPerSample, int channels) 
{
    
    NSArray *paths               = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath       = [paths objectAtIndex:0];
    NSString *docFilePath        = [documentPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%s", filename]];
    //NSLog(@"documentPath=%@", documentPath);
    
    wav = fopen([docFilePath cStringUsingEncoding:NSASCIIStringEncoding], "wb");
    if (wav == NULL)
        return;
    dataLength = 0;
    this->sampleRate = sampleRate;
    this->bitsPerSample = bitsPerSample;
    this->channels = channels;
 
    writeHeader(dataLength);
}
 
WavWriter::~WavWriter() {
    if (wav == NULL)
        return;
    fseek(wav, 0, SEEK_SET);
    writeHeader(dataLength);
    fclose(wav);
}
 
void WavWriter::writeData(const unsigned char* data, int length) {
    if (wav == NULL)
        return;
    fwrite(data, length, 1, wav);
    dataLength += length;
}