Implementing sound

Member
Posts: 45
Joined: 2002.04
Post: #1
I need something really simple..and quick to implement.

All I want to be able to do is play a few sounds.. nothing fancy... no stereo panning or anything.. no crazy effects.. just play 4-5 sounds at the most at the same time...

I need a solution that is simple and easy to implement something requires very little time to get results..

Is this wishful thinking or is there something out there i can use for my uDev entry>?

A little background music would add a lot to my game.. as would some simple sound effects..


any thoughts?

same code is welcome..

hank-

/* Drunk...... fix later.... */
Quote this message in a reply
Member
Posts: 469
Joined: 2002.10
Post: #2
how much control do you want? NSSound is fairly straight forward.
Code:
[[NSSound soundNamed:@"soundinbundle"] play];

---Kelvin--
15.4" MacBook Pro revA
1.83GHz/2GB/250GB
Quote this message in a reply
Member
Posts: 269
Joined: 2005.04
Post: #3
And for background music, NSMovie is the way to go.

Code:
NSMovie *musicQT;
musicQT = [[NSMovie alloc] initWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource: @"music" ofType: @"mp3"]] byReference:YES]; // load music.mp3 from your resources
GoToBeginningOfMovie([musicQT QTMovie]);
StartMovie([musicQT QTMovie]);

StopMovie([musicQT QTMovie]); // Once you want to stop it


Sound Manager is probably the easiest for Carbon, but it's still way more complicated than NSSound and NSMovie.
Quote this message in a reply
Member
Posts: 469
Joined: 2002.10
Post: #4
if you want looping music CocoaBlitz has an NSMovie subclass CBMovie that has all the controls of NSMovieView. Good for looping and other things like that.

---Kelvin--
15.4" MacBook Pro revA
1.83GHz/2GB/250GB
Quote this message in a reply
Member
Posts: 156
Joined: 2002.11
Post: #5
For a Carbon "almost-turnkey" solution, go to http://www.idevgames.com/forum/showthrea...#post51277
Quote this message in a reply
Moderator
Posts: 435
Joined: 2002.09
Post: #6
Quote:Originally posted by kelvin
if you want looping music CocoaBlitz has an NSMovie subclass CBMovie that has all the controls of NSMovieView. Good for looping and other things like that.


Could I use this in my uDevGame entry? Of course you would be credited and linked, but are there any other terms?

[EDIT: never mind, I thought I could use this subclass separately from the CocoaBlitz framework. I believe you already have information posted about the terms for using the entire CocoaBlitz framework, so I can go look it up.]

Measure twice, cut once, curse three or four times.
Quote this message in a reply
Moderator
Posts: 435
Joined: 2002.09
Post: #7
Okay, color me annoyed. I had hoped to add looping background music fairly easily, but:

- no code samples do it on Apple's site (they used to, but now they just have a technical note that says "you can probably use Quicktime, maybe."
- Quicktime seems to require a MovieView to give you looping. I can't figure out a way to do that without creating a visible MovieView object on a window somewhere. Either that or you poll to see if your movie has ended yet... I can't imagine I'll get seamless loops that way
- CocoaBlitz CBMovie doesn't quite match my expectation (the methods all take the sender's id, it doesn't load by URL, there are no examples) though it was promising
- searched CocoaDev, iDG FAQ, and Googled for an hour but didn't find what I need.

Why is this difficult at all?? Hasn't this problem been solved a million times over? Is it actually easy but I'm in too much of a hurry to see it? I guess if I had a week to spare I'd figure it out.

I can't believe I wasted an evening on this... I could have worked on my icon or something. But no, I had to get fancy and try and add music at the last minute.
Mad

Measure twice, cut once, curse three or four times.
Quote this message in a reply
Member
Posts: 42
Joined: 2002.09
Post: #8
in the header:

NSMovie *bgMusic;
Movie *theMusic;
NSTimer *checkMusicTimer;

Code:
bgMusic=[[NSMovie alloc] initWithURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingString:@"/In_Touch_2.mp3"]] byReference:YES];
    theMusic=[bgMusic QTMovie];

    GoToBeginningOfMovie (theMusic);
    StartMovie (theMusic);

    musicOn=YES;

    checkMusicTimer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(checkMusic:) userInfo:nil repeats:YES];
}

-(void)checkMusic:(NSTimer *)theTimer
{
    if (IsMovieDone(theMusic) && musicOn){
    GoToBeginningOfMovie (theMusic);
    }
}


Checking only one or a few times (or 100s, really) a second has no effect on performance.

There is probably a techier way of doing this, but the above code works for me.

Visit http://www.theDailyGrind.net for your recommended daily intake of embittered satire.
Quote this message in a reply
Member
Posts: 269
Joined: 2005.04
Post: #9
Damian beat me to it. You use an NSTimer and just check to see if the movie is done. Then you restart it.

NSMovieView is easier to use, but I think its looping is also buggy in Jaguar. You basically have to do the exact same thing when you use a View.
Quote this message in a reply
Member
Posts: 469
Joined: 2002.10
Post: #10
I can't find the old thread on this topic but using timers to loop NSSounds or even using NSSound's notification callback will cause an audible pop as it skips.

CBMovie on the other hand uses Quicktime callbacks and will loop the sounds smoothly. Smile Also CBMovie has controls to change the rate(pitch) and time(playbackhead) of the sound.

here's some code:
Code:
CBMovie* sound = [[CBMovie alloc] initWithFile:soundpath byReference:NO];
        NSAssert(sound, @"sound is nil");

        [sound setLoopMode:NSQTMovieLoopingPlayback];

        [sound start:self];

[edit: who thinks I should post the source to CBMovie?]

---Kelvin--
15.4" MacBook Pro revA
1.83GHz/2GB/250GB
Quote this message in a reply
Moderator
Posts: 435
Joined: 2002.09
Post: #11
Quote:Originally posted by kelvin
I can't find the old thread on this topic but using timers to loop NSSounds or even using NSSound's notification callback will cause an audible pop as it skips.

CBMovie on the other hand uses Quicktime callbacks and will loop the sounds smoothly. Smile Also CBMovie has controls to change the rate(pitch) and time(playbackhead) of the sound.

here's some code:
Code:
CBMovie* sound = [[CBMovie alloc] initWithFile:soundpath byReference:NO];
        NSAssert(sound, @"sound is nil");

        [sound setLoopMode:NSQTMovieLoopingPlayback];

        [sound start:self];

[edit: who thinks I should post the source to CBMovie?]


All these replies are helpful. I'm most worried about loop discontinuity, as I mentioned, so I think NSTimer polling won't work for me.

This morning I finally remembered that I had a copy of Mac Game Programming, which has a Carbon sound looping solution (also using callbacks) and I was going to go with that. But now that I know that Quicktime has callbacks I was able to find some partial examples online just now.

But let's talk CBMovie some more. What's "self" above? Would any Cocoa object do, or does it need to implement a particular interface? My game's objects are all C++ but I wouldn't mind defining a trivial Cocoa helper object to hold the music.

I like the way you baked in the NSMovieView functionality into the CBMovie interface.

Quote:who thinks I should post the source to CBMovie?
I read this, and your thread about possibly open-sourcing CB, and I don't know what to tell you. If you posted the source to that one object I'd use it without CB but you and CB would get a nice thank-you from me in the game credits. But maybe you'd prefer to force people to use all of CB? It could be a marketing tool for you.

I'll add that if you don't post the source I may decide not load your framework to use that one utility. Thanks to your hints I'm probably able to roll my own version now, though using CBMovie would certainly save me time.

In short, I'd love you to release it but I can see why you wouldn't want to. And you won't get any lecture from me about open source, seeing as I've been using the restrictive iDG license for my source code. (I'm having second thoughts about that, but that's a topic for another day.) Do what you feel best, with my thanks either way.

And thanks for all the help, everybody. I was pretty discouraged last night, and ready to move on to the icon and readme, but I think I'll give this another shot. Music would have a dramatic effect on the game, I think.

Measure twice, cut once, curse three or four times.
Quote this message in a reply
Member
Posts: 156
Joined: 2002.11
Post: #12
Matt,

If you are considering a Carbon / C solution, check the source code on this thread: http://www.idevgames.com/forum/showthrea...#post51277

It's all done for you. You'd just need to interface the C code with your Obj C code, but that should be almost trivial ( I mean, if you are indeed working in Obj C ).
Quote this message in a reply
Moderator
Posts: 435
Joined: 2002.09
Post: #13
Quote:Originally posted by FCCovett
Matt,

If you are considering a Carbon / C solution, check the source code on this thread: http://www.idevgames.com/forum/showthrea...#post51277

It's all done for you. You'd just need to interface the C code with your Obj C code, but that should be almost trivial ( I mean, if you are indeed working in Obj C ).


Obj C++ and straight C++, actually. No problem interfacing to some C code.

You know, I looked at your code last night but for some reason I put it away again. I can't remember why. I'll look at it again and tell you if there was something specific that bothered me. Maybe it was something stupid, like I didn't understand the license. I'm treating all my thoughts from last night as highly suspect, now that I've gotten a little sleep. :-)

Thanks!

Measure twice, cut once, curse three or four times.
Quote this message in a reply
Member
Posts: 469
Joined: 2002.10
Post: #14
well... since I don't really have any qualms about CBMovie (as it's not really integral to CB) I'll post the source here for you Smile as a favor though, I would like for you to write up a more professional looking tutorial perhaps. Something that is easier to read and more permanent than a forum post. Maybe even an iDG article... You can use my source files, just put credit where it's due.

CBMovie source

watch those fixed precision values!

---Kelvin--
15.4" MacBook Pro revA
1.83GHz/2GB/250GB
Quote this message in a reply
Member
Posts: 156
Joined: 2002.11
Post: #15
License? Use it as you wish; my code is public domain. The part of it I got from Apple's documentation is not restricted, AFAIK.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Implementing audio in games sammyitch 3 2,522 Dec 21, 2005 06:27 AM
Last Post: unknown
  need advice implementing AI in game. NYGhost 9 3,442 Aug 27, 2004 01:28 PM
Last Post: NYGhost