This repository has been archived by the owner on Aug 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 118
/
ORGMInputUnit.m
151 lines (122 loc) · 4.17 KB
/
ORGMInputUnit.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
//
// ORGMInputUnit.m
//
// Copyright (c) 2012 ap4y (lod@pisem.net)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#import "ORGMInputUnit.h"
#import "ORGMPluginManager.h"
@interface ORGMInputUnit () {
int bytesPerFrame;
void *inputBuffer;
BOOL _shouldSeek;
long seekFrame;
}
@property (retain, nonatomic) NSMutableData *data;
@property (retain, nonatomic) id<ORGMSource> source;
@property (retain, nonatomic) id<ORGMDecoder> decoder;
@property (assign, nonatomic) BOOL endOfInput;
@end
@implementation ORGMInputUnit
- (id)init {
self = [super init];
if (self) {
self.data = [NSMutableData data];
inputBuffer = malloc(CHUNK_SIZE);
_endOfInput = NO;
}
return self;
}
- (void)dealloc {
[self close];
free(inputBuffer);
[_decoder release];
[_source release];
[_data release];
[super dealloc];
}
#pragma mark - public
- (BOOL)openWithUrl:(NSURL *)url {
self.source = [[ORGMPluginManager sharedManager] sourceForURL:url error:nil];
if (!_source || ![_source open:url]) return NO;
self.decoder = [[ORGMPluginManager sharedManager] decoderForSource:_source error:nil];
if (!_decoder || ![_decoder open:_source]) return NO;
int bitsPerSample = [[_decoder.properties objectForKey:@"bitsPerSample"] intValue];
int channels = [[_decoder.properties objectForKey:@"channels"] intValue];
bytesPerFrame = (bitsPerSample/8) * channels;
return YES;
}
- (void)close {
[_decoder close];
}
- (void)process {
_isProcessing = YES;
int amountInBuffer = 0;
int framesRead = 0;
do {
if (_data.length >= BUFFER_SIZE) {
framesRead = 1;
break;
}
if (_shouldSeek) {
[_decoder seek:seekFrame];
_shouldSeek = NO;
}
int framesToRead = CHUNK_SIZE/bytesPerFrame;
framesRead = [_decoder readAudio:inputBuffer frames:framesToRead];
amountInBuffer = (framesRead * bytesPerFrame);
dispatch_sync([ORGMQueues lock_queue], ^{
[_data appendBytes:inputBuffer length:amountInBuffer];
});
} while (framesRead > 0);
if (framesRead <= 0) {
[self setEndOfInput:YES];
}
_isProcessing = NO;
}
- (double)framesCount {
NSNumber *frames = [_decoder.properties objectForKey:@"totalFrames"];
return [frames doubleValue];
}
- (void)seek:(double)time withDataFlush:(BOOL)flush {
if (flush) {
dispatch_sync([ORGMQueues lock_queue], ^{ self.data = [NSMutableData data]; });
}
seekFrame = time * [[_decoder.properties objectForKey:@"sampleRate"] floatValue];
_shouldSeek = YES;
}
- (void)seek:(double)time {
[self seek:time withDataFlush:NO];
}
- (AudioStreamBasicDescription)format {
return propertiesToASBD(_decoder.properties);
}
- (NSDictionary *)metadata {
return [_decoder metadata];
}
- (int)shiftBytes:(NSUInteger)amount buffer:(void *)buffer {
int bytesToRead = MIN(amount, _data.length);
dispatch_sync([ORGMQueues lock_queue], ^{
memcpy(buffer, _data.bytes, bytesToRead);
[_data replaceBytesInRange:NSMakeRange(0, bytesToRead) withBytes:NULL length:0];
});
return bytesToRead;
}
#pragma mark - private
@end