Skip to content

Commit

Permalink
removed mute.caf dependency (creating silence.wav in runtime)
Browse files Browse the repository at this point in the history
  • Loading branch information
Filipe Pina committed Dec 11, 2013
1 parent aded38d commit 2b320d2
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
6 changes: 2 additions & 4 deletions MuteDetector.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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 = "<group>"; };
2CAFACE0185536DE005407DE /* SKMuteSwitchDetector.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SKMuteSwitchDetector.m; sourceTree = "<group>"; };
2CAFACE218553A2D005407DE /* mute.caf */ = {isa = PBXFileReference; lastKnownFileType = file; path = mute.caf; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -121,7 +119,6 @@
2CAFACAA1855341B005407DE /* MuteDetector */ = {
isa = PBXGroup;
children = (
2CAFACE218553A2D005407DE /* mute.caf */,
2CAFACDF185536DE005407DE /* SKMuteSwitchDetector.h */,
2CAFACE0185536DE005407DE /* SKMuteSwitchDetector.m */,
2CAFACB31855341B005407DE /* SKAppDelegate.h */,
Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -477,6 +473,7 @@
2CAFACD51855341B005407DE /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
2CAFACD61855341B005407DE /* Build configuration list for PBXNativeTarget "MuteDetectorTests" */ = {
isa = XCConfigurationList;
Expand All @@ -485,6 +482,7 @@
2CAFACD81855341B005407DE /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
51 changes: 49 additions & 2 deletions MuteDetector/SKMuteSwitchDetector.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#import "SKMuteSwitchDetector.h"
#import <AudioToolbox/AudioToolbox.h>

#define SAMPLE_RATE 44000
#define SAMPLES 0.2

void MuteSoundPlaybackComplete(SystemSoundID ssID,void* clientData){

Expand All @@ -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);

Expand Down
Binary file removed MuteDetector/mute.caf
Binary file not shown.

0 comments on commit 2b320d2

Please sign in to comment.