-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPrismAudioManager.mm
148 lines (122 loc) · 4.64 KB
/
PrismAudioManager.mm
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
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioServices.h>
#import <Foundation/Foundation.h>
#import <Accelerate/Accelerate.h>
#import <AppSupport/AppSupport.h>
#import "MediaRemote.h"
#import "prismheaders.h"
#import "PrismAudioManager.h"
#import "PrismFFTHelper.h"
@implementation PrismAudioManager
+ (PrismAudioManager* )defaultManager
{
static dispatch_once_t pred;
static PrismAudioManager *shared = nil;
dispatch_once(&pred, ^
{
shared = [self new];
});
return shared;
}
-(void)tapStreamFromItem:(MPAVItem*)item
{
NSLog(@"[PrismAudioManager]tapStreamFromItem: %@", item);
[self removeTap];
_itemWithTap = item;
if([[[[_itemWithTap playerItem] asset] tracks] count] == 0 )
{
NSLog(@"[PrismAudioManager]Not attempting to tap into the stream.");
return;
}
AVAssetTrack * audioTrack = [[[[_itemWithTap playerItem] asset] tracks] objectAtIndex:0];
[self beginRecordingAudioFromTrack:audioTrack];
}
-(void)beginRecordingAudioFromTrack:(AVAssetTrack*)audioTrack
{
MTAudioProcessingTapRef tap;
MTAudioProcessingTapCallbacks callbacks;
callbacks.version = kMTAudioProcessingTapCallbacksVersion_0;
callbacks.clientInfo = (__bridge void *)(self);
callbacks.init = init;
callbacks.prepare = prepare;
callbacks.process = process;
callbacks.unprepare = unprepare;
callbacks.finalize = finalize;
OSStatus err = MTAudioProcessingTapCreate(kCFAllocatorDefault, &callbacks,
kMTAudioProcessingTapCreationFlag_PostEffects, &tap);
if (err) {
NSLog(@"[PrismAudioManager]Unable to create the Audio Processing Tap : %ld", err);
return;
}
AVMutableAudioMix *audioMix = [AVMutableAudioMix audioMix];
AVMutableAudioMixInputParameters *inputParams = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:audioTrack];
inputParams.audioTapProcessor = tap;
audioMix.inputParameters = @[inputParams];
[_itemWithTap playerItem].audioMix = audioMix;
}
- (void)updateSpectrumDataWithData:(NSArray *)data withVol:(float)avgVol withLength:(NSInteger)length{
dispatch_async(dispatch_get_main_queue(), ^
{
NSDictionary * userInfo = nil;
if( data )
{
userInfo = @{@"spectrumData" : data , @"avgVol" : [NSNumber numberWithFloat:avgVol], @"spectrumDataLength" : [NSNumber numberWithInt:length] };
}
[[NSNotificationCenter defaultCenter] postNotificationName:@"didChangeSpectrumData" object:nil userInfo: userInfo];
});
}
- (void)removeTap
{
NSLog(@"[PrismAudioManager]removing tap from: %@", _itemWithTap);
if(_itemWithTap && _itemWithTap.playerItem.audioMix != nil)
{
AVMutableAudioMixInputParameters *params= ((AVMutableAudioMixInputParameters*)_itemWithTap.playerItem.audioMix.inputParameters[0]);
MTAudioProcessingTapRef tap = params.audioTapProcessor;
_itemWithTap.playerItem.audioMix = nil;
CFRelease(tap);
}
}
static PrismFFTHelper * fftHelper = nil;
static BOOL isNonInterleaved = NO;
void init(MTAudioProcessingTapRef tap, void *clientInfo, void **tapStorageOut)
{
NSLog(@"[PrismAudioManager]Initialising the Audio Tap Processor");
*tapStorageOut = clientInfo;
}
void finalize(MTAudioProcessingTapRef tap)
{
NSLog(@"[PrismAudioManager]Finalizing the Audio Tap Processor");
}
void prepare(MTAudioProcessingTapRef tap, CMItemCount maxFrames, const AudioStreamBasicDescription *processingFormat)
{
NSLog(@"[PrismAudioManager]Preparing the Audio Tap Processor");
if(processingFormat->mFormatFlags && kAudioFormatFlagIsNonInterleaved)
isNonInterleaved = YES;
else
isNonInterleaved = NO;
}
void unprepare(MTAudioProcessingTapRef tap)
{
NSLog(@"[PrismAudioManager]Unpreparing the Audio Tap Processor");
}
void process(MTAudioProcessingTapRef tap, CMItemCount numberFrames,
MTAudioProcessingTapFlags flags, AudioBufferList *bufferListInOut,
CMItemCount *numberFramesOut, MTAudioProcessingTapFlags *flagsOut)
{
OSStatus err = MTAudioProcessingTapGetSourceAudio(tap, numberFrames, bufferListInOut,
flagsOut, NULL, numberFramesOut);
if (err)
{
NSLog(@"[PrismAudioManager]Error from GetSourceAudio: %ld", err);
return;
}
if( !fftHelper )
{
fftHelper = [PrismFFTHelper new];
}
[fftHelper performComputation:bufferListInOut numberFrames:numberFrames isNonInterleaved:isNonInterleaved completionHandler:^(NSArray *fftData, float avgVol, NSInteger length)
{
[[PrismAudioManager defaultManager] updateSpectrumDataWithData:fftData withVol:avgVol withLength:length];
}];
}
@end