-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.m
214 lines (177 loc) · 5.99 KB
/
main.m
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//
// main.m
// BlankCMSampleBuffer
//
// Created by armen karamian on 12/24/15.
// Copyright © 2015 armen karamian. All rights reserved.
//
#import <Foundation/Foundation.h>
@import AVFoundation;
static void releaseNSData(void *o, void *block, size_t size);
int main(int argc, const char * argv[]) {
@autoreleasepool
{
NSError *err;
NSURL *outputURL = [NSURL fileURLWithPath:@"/Users/armen/Desktop/testAudioOutput.aiff"];
AudioChannelLayout channelLayout;
memset(&channelLayout, 0, sizeof(AudioChannelLayout));
channelLayout.mChannelLayoutTag = kAudioChannelLayoutTag_Mono;
NSDictionary *outputSettings =
[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:kAudioFormatLinearPCM], AVFormatIDKey,
[NSNumber numberWithFloat:48000.0], AVSampleRateKey,
[NSNumber numberWithInt:1], AVNumberOfChannelsKey,
[NSData dataWithBytes:&channelLayout length:sizeof(AudioChannelLayout)],
AVChannelLayoutKey,
[NSNumber numberWithInt:16], AVLinearPCMBitDepthKey,
[NSNumber numberWithBool:NO], AVLinearPCMIsNonInterleaved,
[NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey,
[NSNumber numberWithBool:YES], AVLinearPCMIsBigEndianKey,
nil];
//create asbd and channel layout for output
AudioStreamBasicDescription asbd = {};
asbd.mBitsPerChannel = 16;
asbd.mBytesPerFrame = 2;
asbd.mSampleRate = 48000.0;
asbd.mFormatID = kAudioFormatLinearPCM;
asbd.mFramesPerPacket = 1;
asbd.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
asbd.mChannelsPerFrame = 1;
asbd.mBytesPerFrame = asbd.mChannelsPerFrame * 2;
asbd.mBytesPerPacket = asbd.mFramesPerPacket * asbd.mBytesPerFrame;
asbd.mReserved = 0;
AudioChannelLayout audioChannelLayout = {
.mChannelLayoutTag = kAudioChannelLayoutTag_Mono,
.mChannelBitmap = 0,
.mNumberChannelDescriptions = 0
};
CMAudioFormatDescriptionRef audioFormat;
CMAudioFormatDescriptionCreate(kCFAllocatorDefault, &asbd, sizeof(audioChannelLayout), &audioChannelLayout,
0, NULL, NULL, &audioFormat);
dispatch_queue_t q = dispatch_queue_create("assetwriterQ", NULL);
AVAssetWriter *writer = [AVAssetWriter assetWriterWithURL:outputURL
fileType:AVFileTypeAIFF
error:&err];
AVAssetWriterInput *input = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeAudio
outputSettings:outputSettings];
if([writer canAddInput:input])
{
NSLog(@"Adding input");
[writer addInput:input];
input.expectsMediaDataInRealTime = NO;
if([writer startWriting])
{
NSLog(@"Writing");
[writer startSessionAtSourceTime:kCMTimeZero];
[input requestMediaDataWhenReadyOnQueue:q usingBlock:^{
while (input.isReadyForMoreMediaData && writer.status == AVAssetWriterStatusWriting)
{
//create array of empty samples assuming 16 bit
int sampleCount = 48000;
UInt16 audiosamples[sampleCount];
for (int i = 0; i < sampleCount; i++)
{
audiosamples[i] = 1;
}
unsigned long audioSampleSize = sizeof(audiosamples);
//create NSData with audio samples audio data
NSData *audioData = [[NSData alloc] initWithBytes:audiosamples length:audioSampleSize];
CMBlockBufferCustomBlockSource blockSource =
{
.version = kCMBlockBufferCustomBlockSourceVersion,
.AllocateBlock = NULL,
.FreeBlock = &releaseNSData,
.refCon = (__bridge_retained void*) audioData,
};
CMBlockBufferRef audiosampleBlock;
OSStatus stat = CMBlockBufferCreateWithMemoryBlock(NULL,
(uint8_t*) audioData.bytes,
audioData.length,
NULL,
&blockSource,
0,
audioData.length,
0,
&audiosampleBlock);
if (stat != noErr)
{
NSLog(@"Block buffer error");
exit(1);
}
if (CMBlockBufferIsEmpty(audiosampleBlock))
{
NSLog(@"Block Buffer is empty");
exit(1);
}
CMSampleBufferRef audioSampleBuffer;
CMItemCount audioSampleCount = 48000;
CMTime startTime = CMTimeMake(1, 48000);
stat = CMAudioSampleBufferCreateWithPacketDescriptions(kCFAllocatorDefault,
audiosampleBlock,
true,
NULL,
NULL,
audioFormat,
audioSampleCount,
startTime,
NULL,
&audioSampleBuffer);
if (stat != noErr)
{
NSLog(@"Sample buffer error");
exit(1);
}
CMItemCount nunmItems = CMSampleBufferGetNumSamples(audioSampleBuffer);
if (audioSampleBuffer)
{
if (!CMSampleBufferDataIsReady(audioSampleBuffer))
{
NSLog(@"sample buffer is not ready");
exit(1);
}
if (!CMSampleBufferIsValid(audioSampleBuffer))
{
NSLog(@"Audio sapmle buffer is not valid");
exit(1);
}
stat = CMSampleBufferMakeDataReady(audioSampleBuffer);
if (stat == noErr)
{
bool result = [input appendSampleBuffer:audioSampleBuffer];
if (!result)
{
NSLog(@"%@",writer.error);
}
else
NSLog(@"Writing complete");
}
CFRelease(audiosampleBlock);
CMSampleBufferInvalidate(audioSampleBuffer);
CFRelease(audioSampleBuffer);
}
[input markAsFinished];
CMTime endTime = CMTimeMake(48000, 48000);
}
[writer finishWritingWithCompletionHandler:^{
NSLog(@"Writing to file complete");
}];
}];
dispatch_sync(q, ^{NSLog(@"Done");});
}
else
{
NSLog(@"Cannot write, destination file already exists?");
}
}
else
{
NSLog(@"Cannot add input");
}
}
return 0;
}
static void releaseNSData(void *o, void *block, size_t size)
{
NSData *data = (__bridge_transfer NSData*) o;
data = nil;
}