diff --git a/MuteDetector.xcodeproj/project.pbxproj b/MuteDetector.xcodeproj/project.pbxproj index e654f54..fcdefdc 100644 --- a/MuteDetector.xcodeproj/project.pbxproj +++ b/MuteDetector.xcodeproj/project.pbxproj @@ -23,7 +23,6 @@ 2CAFACD01855341B005407DE /* MuteDetectorTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 2CAFACCF1855341B005407DE /* MuteDetectorTests.m */; }; 2CAFACDA18553580005407DE /* AVFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2CAFACD91855357F005407DE /* AVFoundation.framework */; }; 2CAFACE1185536DE005407DE /* SKMuteSwitchDetector.m in Sources */ = {isa = PBXBuildFile; fileRef = 2CAFACE0185536DE005407DE /* SKMuteSwitchDetector.m */; }; - 2CAFACE318553A2D005407DE /* mute.caf in Resources */ = {isa = PBXBuildFile; fileRef = 2CAFACE218553A2D005407DE /* mute.caf */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -59,7 +58,6 @@ 2CAFACD91855357F005407DE /* AVFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AVFoundation.framework; path = System/Library/Frameworks/AVFoundation.framework; sourceTree = SDKROOT; }; 2CAFACDF185536DE005407DE /* SKMuteSwitchDetector.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SKMuteSwitchDetector.h; sourceTree = ""; }; 2CAFACE0185536DE005407DE /* SKMuteSwitchDetector.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SKMuteSwitchDetector.m; sourceTree = ""; }; - 2CAFACE218553A2D005407DE /* mute.caf */ = {isa = PBXFileReference; lastKnownFileType = file; path = mute.caf; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -121,7 +119,6 @@ 2CAFACAA1855341B005407DE /* MuteDetector */ = { isa = PBXGroup; children = ( - 2CAFACE218553A2D005407DE /* mute.caf */, 2CAFACDF185536DE005407DE /* SKMuteSwitchDetector.h */, 2CAFACE0185536DE005407DE /* SKMuteSwitchDetector.m */, 2CAFACB31855341B005407DE /* SKAppDelegate.h */, @@ -244,7 +241,6 @@ 2CAFACBD1855341B005407DE /* Images.xcassets in Resources */, 2CAFACAF1855341B005407DE /* InfoPlist.strings in Resources */, 2CAFACB81855341B005407DE /* Main.storyboard in Resources */, - 2CAFACE318553A2D005407DE /* mute.caf in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -477,6 +473,7 @@ 2CAFACD51855341B005407DE /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; 2CAFACD61855341B005407DE /* Build configuration list for PBXNativeTarget "MuteDetectorTests" */ = { isa = XCConfigurationList; @@ -485,6 +482,7 @@ 2CAFACD81855341B005407DE /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; /* End XCConfigurationList section */ }; diff --git a/MuteDetector/SKMuteSwitchDetector.m b/MuteDetector/SKMuteSwitchDetector.m index fa97e7a..7bf7136 100644 --- a/MuteDetector/SKMuteSwitchDetector.m +++ b/MuteDetector/SKMuteSwitchDetector.m @@ -10,6 +10,8 @@ #import "SKMuteSwitchDetector.h" #import +#define SAMPLE_RATE 44000 +#define SAMPLES 0.2 void MuteSoundPlaybackComplete(SystemSoundID ssID,void* clientData){ @@ -24,15 +26,60 @@ void MuteSoundPlaybackComplete(SystemSoundID ssID,void* clientData){ AudioServicesDisposeSystemSoundID(soundId); } +BOOL createSoundFileIfRequired(NSString* soundFile) { + if ([[NSFileManager defaultManager] fileExistsAtPath:soundFile isDirectory:NO]) + return YES; + + NSUInteger length = SAMPLE_RATE * SAMPLES * 2; // 2 bytes per sample + NSUInteger temp; + // initialize with room for RIFF chunk (36) + "data" header from data chunk + actual sound data + NSMutableData *data = [NSMutableData dataWithCapacity:(length + 36 + 4)]; + + [data appendData:[@"RIFF" dataUsingEncoding:NSASCIIStringEncoding]]; + temp = length + 36; + [data appendData:[NSData dataWithBytes:&temp length:4]]; + [data appendData:[@"WAVE" dataUsingEncoding:NSASCIIStringEncoding]]; + [data appendData:[@"fmt " dataUsingEncoding:NSASCIIStringEncoding]]; + temp = 16; + [data appendData:[NSData dataWithBytes:&temp length:4]]; + temp = 1; + [data appendData:[NSData dataWithBytes:&temp length:2]]; + [data appendData:[NSData dataWithBytes:&temp length:2]]; + temp = SAMPLE_RATE; + [data appendData:[NSData dataWithBytes:&temp length:4]]; + temp *= 2; + [data appendData:[NSData dataWithBytes:&temp length:4]]; + temp = 2; + [data appendData:[NSData dataWithBytes:&temp length:2]]; + temp = 16; + [data appendData:[NSData dataWithBytes:&temp length:2]]; + [data appendData:[@"data" dataUsingEncoding:NSASCIIStringEncoding]]; + [data appendData:[NSData dataWithBytes:&length length:4]]; + temp = 0; + + NSData *nullByte = [NSData dataWithBytes:&temp length:1]; + + for (NSUInteger i = 0; i < length; i++) + [data appendData:nullByte]; + + return [data writeToFile:soundFile atomically:YES]; +} + @implementation SKMuteSwitchDetector + (void)checkSwitch:(SKMuteSwitchDetectorBlock)andPerform { if (!andPerform) return; - NSURL* url = [[NSBundle mainBundle] URLForResource:@"mute" withExtension:@"caf"]; + NSString *soundFile = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches/silence.wav"]; + + if (!createSoundFileIfRequired(soundFile)) { + andPerform(NO, NO); + return; + } + SystemSoundID soundId; - if (AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &soundId) == kAudioServicesNoError){ + if (AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundFile], &soundId) == kAudioServicesNoError){ UInt32 yes = 1; AudioServicesSetProperty(kAudioServicesPropertyIsUISound, sizeof(soundId),&soundId,sizeof(yes), &yes); diff --git a/MuteDetector/mute.caf b/MuteDetector/mute.caf deleted file mode 100644 index d0df847..0000000 Binary files a/MuteDetector/mute.caf and /dev/null differ