Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 04-14-2010, 10:48 AM   #1 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 3
Vic320 is on a distinguished road
Default Creating audio programmatically

I want to create short audio clips (less than 5 seconds) programmatically (synthesized sound) and save it into an audio file on the iPhone. It is simple to do something like this with images (just a few lines of code) but I can't seem to find any simple way of doing this with audio. Does anyone have any ideas here? I looked at the "Speak Here" example code, which I have been told will allow me to do it, but that seems exceptionally complex for this. Sample code would be greatly appreciated!
Vic320 is offline   Reply With Quote
Old 04-15-2010, 03:00 AM   #2 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 10
deepak_chrome is on a distinguished road
Lightbulb Solution_For Sound problem

Hi there,
as i understand u want to play a sound clip only for 5 sec, to do the same you need to do something like this:-
1. Add framework:- audioToolbox.framework
2 . #import <AudioToolbox/AudioServices.h>
3. Use method to play your sound
4. Call method [self play_Sound];

-(void) play_Sound
{
//Get the filename of the sound file:
NSString *path = [NSString stringWithFormat:@"%@%@",
[[NSBundle mainBundle] resourcePath],
@"/your_sound_file.wav"];

//declare a system sound id
SystemSoundID soundID;

//Get a URL for the sound file
NSURL *filePath = [NSURL fileURLWithPath: path is Directory:NO];

//Use audio sevices to create the sound
AudioServicesCreateSystemSoundID((CFURLRef)filePat h, &soundID);

//Use audio services to play the sound

AudioServicesPlaySystemSound(soundID);
sleep(5); //define length for your sound clip
AudioServicesDisposeSystemSoundID(soundID);
}

Last edited by deepak_chrome; 04-15-2010 at 05:05 AM.
deepak_chrome is offline   Reply With Quote
Old 04-15-2010, 08:17 AM   #3 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 3
Vic320 is on a distinguished road
Default Almost

Thanks for the reply. This is actually half the solution (the playback part). I have figured out the create an audio file part on my own. Since I searched the Internet high and low for a *simple* solution and didn't find anything, I will post my solution here as a community service. The simplest solution that I came up with to programmatically create a sound is as follows:

Create a wave file from scratch. Wave files have a pretty simple structure so it's fairly easy to generate the needed header info (see the wave spec here: https://ccrma.stanford.edu/courses/4...ts/WaveFormat/). Since all of the sound files I am going to be creating are all the same length and sample rate, I created a file with all the header data all ready in place but it would be a simple matter to do it programmatically. I load that data into a NSMutableData object then just append my sound data after that. Example:

Code:
NSString *path = [[NSBundle mainBundle] pathForResource:@"WavHeader" ofType:@"wav"];
	
NSMutableData *audioData = [[NSMutableData alloc] initWithContentsOfFile:path];

int samples = 22050;
	
uint8_t dataToRecord[samples];
	
int x;
	
for(x = 0; x < samples; x++) {
		
	// populate the "dataToRecord" array with audio data. //
		
}

[audioData appendBytes:(const void *)dataToRecord length:samples];

	// Get path to documents folder and set a file name  //

[audioData writeToFile:pathToFile atomically:YES];
Vic320 is offline   Reply With Quote
Old 10-28-2010, 09:02 AM   #4 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 1
rahdu is on a distinguished road
Default It plays the original Wav file

i created a wav file in the Documents directory called myFile.wav but when i tried to play it i get the original wav file witch i used to get the data. what am i doing wrong ? 1 month nub here so any help will be appreciated.
rahdu is offline   Reply With Quote
Old 11-05-2010, 06:57 AM   #5 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 1
sobie is on a distinguished road
Default

hi
first thank you very much for your solution. but i'm a little confused about the way to fill dataToRecord. I tried two way, and they aren't working:

Code:
dataToRecord[x] = (sin(x*(PI2*(8000/freq1))) + sin(x*(PI2*(8000/freq2)))) * 16383;
PI2 = 6,28..
8000- playback frequency

and
Code:
dataToRecord[x]  = amplitude * (sin(x * freq1) + sin(x * freq2))
amplitude = 64

please help, can you show me how did you do it?


edit
maybe the header isn't valid.. a took the one from the site you gave, and there was as an example..
sobie is offline   Reply With Quote
Reply

Bookmarks

Tags
audio

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 319
16 members and 303 guests
chemistry, Domele, Duncan C, Fstuff, givensur, heshiming, HowEver, iAppDeveloper, iphonedevshani, jbro, JoeRCruso, kapps11, newDev, SLIC, stanny, WheyLabs
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,648
Threads: 94,112
Posts: 402,874
Top Poster: BrianSlick (7,990)
Welcome to our newest member, brandon6031
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 07:27 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0