Posts tagged ·

sounds

·...

How to batch change volume of aif files

1 comment

After creating aif files that tells the distance of upto 100 miles in both miles and kilometers (around 50 individual files) I tested the distance announcement while playing music and found that the volume of all my files was too low.  Now I could go in and manually increase the volume on each file – but that would be a bit of a pain and possibly not very consistent.  Besides one of the things computers are good at is doing the same task over and over again.  So I looked for a software solution.  Luckily there is a great open source command line sound manipulation software called <a href=”http://sox.sourceforge.net/”>SOX</a>.  This tool has a function for gain which lets you set the gain of any files in decibels (dB).

To run this program on all files in a folder run the following command from the terminal after copying the sox program to the folder:

for i in *.aif; do ./sox $i ${i%.*}.aiff gain -n 8; done;

In the instance above the original files are .aif and they were converted to have the file extension .aiff.  I tried to overwrite the original file – but that did not work well.

Share

Using audio services to play alert sound

no comments

Some times it your application may inadvertently quit with out the user meaning to quit your app.  For example people run with the running/GPS app and the iPhone may be in a pocket / arm band / hand where the square button could be pressed unintentionally.  If the app quits while running it will not collect running data for the rest of the run and the user will be very disappointed.  To avoid this I have setup an alert which will make a sound if the user quits the app while the app is collecting running data.  I was able to find the original apple alert aiff files via Google and I chose to implement the Indigo sound as the alert – the cricket is not really loud enough to be an alert sound.

By implementing the playback of the sound as an alert sound the sound will be played even on the 1st gen iPod Touch which does not have a speaker.  Alert sounds can also be set to finish playback after the app finishes – which is an important feature for this use.  I was able to find how to set this property on Nagano’s blog.  Not sure what most of the page says but the language of Objective C is universal. Remember to add the audiotoolbox to your project.


#include &lt;AudioToolbox/AudioToolbox.h&gt;

I then load the sound in the viewDidLoad init function for the view controller where I want to play the sound – this way you are not trying to load the sound as the app is quitting.  The soundFileObject isa global for this VC.

// SOUNDS
 NSString *path = [[NSBundle mainBundle] pathForResource:@&quot;Indigo&quot; ofType:@&quot;aiff&quot;];
 NSURL *fileURL = [NSURL fileURLWithPath:path];

 OSStatus err;
 err = AudioServicesCreateSystemSoundID((CFURLRef)fileURL, &amp;soundFileObject);
 if(err) {
   DLog(@&quot;AudioServicesCreateSystemSoundID err = %d&quot;,err);
   exit(1);
 }

 UInt32 flag = 1;
 err = AudioServicesSetProperty(kAudioServicesPropertyCompletePlaybackIfAppDies,
     sizeof(UInt32),
     &amp;soundFileObject,
     sizeof(UInt32),
     &amp;flag);
 if(err){
   DLog(@&quot;AudioServicesSetProperty err = %d&quot;,err);
 }

Then under viewWillDisappear I have the following:

 if (runActivity.isStarted) {
   // play alert sound if exiting while running
   AudioServicesPlayAlertSound (self.soundFileObject);
 }

This way the sound will play if the view is unloading while the user is expecting the app to be collecting data.

Share