From 2b320d28dd8bd81e74bd720c94db6872e95d829a Mon Sep 17 00:00:00 2001 From: Filipe Pina Date: Wed, 11 Dec 2013 01:31:48 +0100 Subject: [PATCH] removed mute.caf dependency (creating silence.wav in runtime) --- MuteDetector.xcodeproj/project.pbxproj | 6 +-- MuteDetector/SKMuteSwitchDetector.m | 51 ++++++++++++++++++++++++- MuteDetector/mute.caf | Bin 7298 -> 0 bytes 3 files changed, 51 insertions(+), 6 deletions(-) delete mode 100644 MuteDetector/mute.caf 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 d0df847a06e6bcf7478549c9873a7219bf74448c..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 7298 zcmeHLL2er{43zC8y%cDl(5K`pMq-~@plJJozNkOwyV}IraAr87cH`cA&;skFv=lir zq_jvLj^}gRwf1=W{?K%Ny8nGo>+{!#FFCi5XL#}cc>Z=eY5v2nd_NykfvLb$U@9;b zm1?T-wK6lGY+w)#pTb8gd?GWSmYd=Oi&UggB zMsK~vc(B43Z=c(3+nt=S!kM9@s*&OO%yU)_QGlBLd-0>@lM_R3S&k0A=MZoxvooL<)Ap#d+a| z4#1z4N#4>SR0+>OqAQ~TnYeY{crQ=<#*J!~zM6Z(Frz5TR-XZT=FOhySP;*3=onW$ z09J~Vb?wNu)ps=%ygC>2<84%0?^RoWGp$N&H0~U#@6f3psTZvzqTeh}$ptzMyC~~5 zVl=9eiz8M)86%&Jq8)a({EckU_f*$?0Q^tfeU*D#4=Yu*0D|0|5$zBW-YtJ3$|ZWr znt{)4yg^Q(>lnGFDu)%$F1{SGrnORU1`OmuO=w98^)J@1PaW4+J=qy$3~18P*~$u7 zsfr*ZJ%*LovgEvwR83TI>4M0(t&`LF{dO`ey1Hw#B1CLd{x*zRqk*HaxWb#QX5 zQMnuN4J~|w6s~bhnXe)UEl>J%2YQUx?9oNYN+r8h)f&4DK6sNFleN~B9vp|?+o$)g z_C%c<@(k2@ngNY8m)eodgyW60B`Rj|j;lId>CW)f`CGdjR>c*s7{@FaUbm=IFjKG8 zS?KBkrqKh1ZGCmtn%Hw)5AWUh{*f~xM5GughH9&sD>cHJWZ;uSUheL`p3(pD!)(B% zFV>jHw)DYo_Am4HnsbEcQb2LS8&^b6D@TqA#L8J!GG0_fTG$c-udR_S^iJKXhITqC z96O!Zkx!OAQt^&lI~TKLX9i#)CTolQ#6;g+=fOFq^Tj-nQF^#P*y%B=+PTZ*)0y%; zbjfI4YXrkZtck;&oQiX;Rj4yln$agba?~wEtll*?eEEQB`%bJd>*|}eVj!!(i2WoP H;>XHYmp>+x