-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAudioFilePlayerPatch.m
203 lines (160 loc) · 4.99 KB
/
AudioFilePlayerPatch.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
#import "AudioFilePlayerPatch.h"
@implementation AudioFilePlayerPatch : QCPatch
+ (QCPatchExecutionMode)executionModeWithIdentifier:(id)fp8
{
return kQCPatchExecutionModeConsumer;
}
+ (BOOL)allowsSubpatchesWithIdentifier:(id)fp8
{
return NO;
}
+ (QCPatchTimeMode)timeModeWithIdentifier:(id)fp8
{
return kQCPatchTimeModeNone;
}
+ (BOOL)canInstantiateWithFile:(NSString*)file
{
BOOL ret = NO;
CFStringRef itemUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension,(CFStringRef)[file pathExtension], NULL);
if(UTTypeConformsTo(itemUTI, kUTTypeAudio))
ret = YES;
CFRelease(itemUTI);
return ret;
}
+ (id)instantiateWithFile:(NSString*)file
{
if([self canInstantiateWithFile: file])
{
id patch = [[AudioFilePlayerPatch alloc] initWithIdentifier: nil];
[patch setFile: file];
[(NSMutableDictionary*)[patch userInfo] setObject: [file lastPathComponent] forKey: @"name"];
return [patch autorelease];
}
return nil;
}
- (id)initWithIdentifier:(id)fp8
{
self=[super initWithIdentifier:fp8];
if(self)
{
[inputCurrentVolume setDoubleValue:1.0];
[inputCurrentVolume setMinDoubleValue:0.0];
[inputCurrentVolume setMaxDoubleValue:1.0];
[inputCurrentPosition setMinDoubleValue:0.0];
[[self userInfo] setObject: @"Kineme Audio File Player" forKey: @"name"];
}
return self;
}
- (void)setFile:(NSString*)file
{
[inputPath setStringValue: file];
}
- (BOOL)setup:(QCOpenGLContext *)context
{
_allocatedSounds=[[NSMutableArray alloc] initWithCapacity:16];
_executedSinceSetup=NO;
return YES;
}
- (void)cleanup:(QCOpenGLContext *)context
{
[_allocatedSounds release];
}
- (void)enable:(QCOpenGLContext *)context
{
// if we've already executed, and the trigger was already on upon enable, launch playback (useful when switching between windowed-mode and fullscreen-mode)
if( _executedSinceSetup && ![inputTrig wasUpdated] && [inputTrig booleanValue] )
[self _startPlaying];
}
- (void)disable:(QCOpenGLContext *)context
{
// stop any remaining sounds (since releasing the NSSound doesn't stop it)
for(NSSound *s in _allocatedSounds)
{
// stop receiving events (we don't want to double-deallocate)
[s setDelegate:nil];
[s stop];
// NSLog(@"AudioFilePlayerPatch: %08x: release (disable)",s);
}
// this automatically releases all of the child NSSound objects
[_allocatedSounds removeAllObjects];
}
- (BOOL)execute:(QCOpenGLContext *)context time:(double)time arguments:(NSDictionary *)arguments
{
// catch rising edge of trigger
if( [inputTrig wasUpdated] && [inputTrig booleanValue] )
[self _startPlaying];
if( [inputLoop wasUpdated] && ![inputLoop booleanValue] )
for(NSSound *s in _allocatedSounds)
[s setLoops:NO];
if( [inputCurrentVolume wasUpdated] )
{
const float volume = [inputCurrentVolume doubleValue];
for(NSSound *s in _allocatedSounds)
[s setVolume:volume];
}
if( [inputCurrentPosition wasUpdated] )
{
const double position = [inputCurrentPosition doubleValue];
for(NSSound *s in _allocatedSounds)
[s setCurrentTime:position];
}
_executedSinceSetup=YES;
return YES;
}
- (void)_startPlayingThread:(NSSound*)s
{
[s play];
[s release];
}
- (void)_startPlaying
{
NSString *path = KIExpandPath(self,[inputPath stringValue]);
// release happens in _startPlayingThread:
NSSound *s=[[NSSound alloc] initWithContentsOfFile:path byReference:YES];
if( !s )
{
[self logMessage:@"Failed to load audio file '%@'.",path];
return;
}
// NSLog(@"AudioFilePlayerPatch: %08x: alloc",s);
[s setLoops:[inputLoop booleanValue]];
[s setVolume:[inputCurrentVolume doubleValue]];
[s setCurrentTime:[inputCurrentPosition doubleValue]];
// pick up the sound:didFinishPlaying message so we can dealloc the NSSound
[s setDelegate:self];
// if a playback device isn't specified, use the default (by not forcing the playback device)
if( [[inputDeviceUID stringValue] length] )
{
@try
{
[s setPlaybackDeviceIdentifier:[inputDeviceUID stringValue]];
}
@catch(...)
{
[self logMessage:@"Failed to set output device UID '%@'.", [inputDeviceUID stringValue]];
[s release];
return;
}
}
// if a channel mapping isn't specified, use the default (by not forcing the channel mapping)
if( [[inputChannelMapping stringValue] length] )
{
NSMutableArray *channelMapping = [[NSMutableArray alloc] initWithCapacity:16];
NSArray *channelMappingStrings = [[inputChannelMapping stringValue] componentsSeparatedByString:@","];
for(NSString *s in channelMappingStrings)
[channelMapping addObject:[NSNumber numberWithInteger:[s integerValue]]];
[s setChannelMapping:channelMapping];
}
if([inputSynchronous booleanValue])
[self _startPlayingThread: s];
else
[NSThread detachNewThreadSelector:@selector(_startPlayingThread:) toTarget:self withObject:s];
[_allocatedSounds addObject:s];
}
- (void)sound:(NSSound *)sound didFinishPlaying:(BOOL)finishedPlaying
{
// NSLog(@"AudioFilePlayerPatch: %08x: release (didFinishPlaying:%d)",sound,finishedPlaying);
// this automatically releases the NSSound object
[_allocatedSounds removeObject:sound];
}
@end