Skip to content

Commit

Permalink
Added SoundManager project
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklockwood committed Mar 30, 2011
0 parents commit 56a821f
Show file tree
Hide file tree
Showing 5 changed files with 464 additions and 0 deletions.
20 changes: 20 additions & 0 deletions LICENCE.txt
@@ -0,0 +1,20 @@
SoundManager
version 1.0, March 30th, 2011

Copyright (C) 2011 Charcoal Design

This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.

Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
80 changes: 80 additions & 0 deletions README.txt
@@ -0,0 +1,80 @@
Purpose
--------------

SoundManager is a simple class for playing sound and music in iPhone or Mac app store apps.


Installation
--------------

To use the SoundManager class in an app, just drag the class files into your project. For iOS apps you will also need to add the AVFoundation framework.


Configuration
--------------

The SoundManager class has the following configuration constants:

FILE_EXTENSION - the default file extension for sounds when not specified.
CROSSFADE_DURATION - the crossfade duration between music tracks.
MUSIC_VOLUME - the volume at which to play music tracks.
SOUND_VOLUME - the volume at which to play sounds.


Properties
--------------

@property (nonatomic, readonly) BOOL playingMusic;

This readonly property reports if the SoundManager is currently playing music.

@property (nonatomic, assign) BOOL allowsBackgroundMusic;

This property is used to control the audio session on the iPhone to allow iPod music to be played in the background. It defaults to NO, so it should be set to YES before you attempt to play any sound or music if you do not want the iPod music to be interrupted. It does nothing on Mac OS currently.


Methods
--------------

+ (SoundManager *)sharedManager;

This class method returns a singleton instance of the SoundManager.

- (void)prepareToPlay;

The prepareToPlay method preloads a random sound from your application bundle, which initialises the AVAudioPlayer. It should be called before you attempt to play any audio, ideally during the startup sequence, to eliminate the delay when you first play a sound or music track. This method currently does nothing on Mac OS.

- (void)playSound:(NSString *)name;

The play method will load and play a sound from the application bundle whose filename matches the name passed. You can include the file extension in the name, or omit it, in which case the SoundManager will look for a matching file with the extension specified in the FILE_EXTENSION constant (defaults to .caf).

- (void)playMusic:(NSString *)name;

This method plays a music track. The music will fade in from silent to the volume specified in MUSIC_VOLUME over a period of time specified by CROSSFADE_DURATION. The sound manager only allows one music track to be played at a time, so if an existing track is playing it will be faded out.

- (void)stopMusic;

This will fade out the currently playing music track over the period specified by CROSSFADE_DURATION.


Supported Formats
-------------------

The iPhone can be quite picky about which sounds it will play. For best results,
use .caf files, which you can generate using the afconvert command line tool. Here are some common configurations:

For background music (mono):

/usr/bin/afconvert -f caff -d aac -c 1 {input_file_name} {output_file_name}.caf

For background music (stereo):

/usr/bin/afconvert -f caff -d aac {input_file_name} {output_file_name}.caf

For sound effects (mono):

/usr/bin/afconvert -f caff -d ima4 -c 1 {input_file_name} {output_file_name}.caf

For sound effects (stereo):

/usr/bin/afconvert -f caff -d ima4 {input_file_name} {output_file_name}.caf
3 changes: 3 additions & 0 deletions RELEASE NOTES.txt
@@ -0,0 +1,3 @@
Version 1.0

- Initial release
63 changes: 63 additions & 0 deletions SoundManager.h
@@ -0,0 +1,63 @@
//
// SoundManager.h
// SoundManager
//
// Created by Nick Lockwood on 29/01/2011.
// Copyright 2011 Charcoal Design. All rights reserved.
//

#import <Foundation/Foundation.h>


#define FILE_EXTENSION @"caf"
#define CROSSFADE_DURATION 3.0
#define MUSIC_VOLUME 0.5
#define SOUND_VOLUME 1.0


@interface Sound : NSObject
{
float targetVolume;
float volumeDelta;
NSTimeInterval lastTick;
NSTimer *timer;
Sound *selfReference;
NSURL *url;
id sound;
}

+ (Sound *)soundWithName:(NSString *)name;
+ (Sound *)soundWithURL:(NSURL *)url;

- (Sound *)initWithName:(NSString *)name;
- (Sound *)initWithURL:(NSURL *)url;

@property (nonatomic, retain, readonly) NSURL *url;
@property (nonatomic, assign, readonly) BOOL playing;
@property (nonatomic, assign) float volume;

- (void)fadeTo:(float)volume duration:(NSTimeInterval)duration;
- (void)fadeIn:(NSTimeInterval)duration;
- (void)fadeOut:(NSTimeInterval)duration;
- (void)play:(BOOL)loop;

@end


@interface SoundManager : NSObject
{
Sound *currentMusic;
BOOL allowsBackgroundMusic;
}

@property (nonatomic, readonly) BOOL playingMusic;
@property (nonatomic, assign) BOOL allowsBackgroundMusic;

+ (SoundManager *)sharedManager;

- (void)prepareToPlay;
- (void)playMusic:(NSString *)name;
- (void)stopMusic;
- (void)playSound:(NSString *)name;

@end

0 comments on commit 56a821f

Please sign in to comment.