PDA

View Full Version : sound Programming


assoul
2004.09.27, 06:44 PM
Hey

I am new to programming on the mac environment and what i have to do is write a program that reads this list of aiff files and plays each one by turn. At the end of each file play a user enters some data and this goes to a text file and i use another program to process the text file. I am stuck with how to play these aiff files using a c program..It has to be compatible wid os 9 so i am using carbon C. anyways i cant quite figure out how to play files and make them wait till a user types in the data and presses carriage return before the next file on the list is played. Basically I need to know how to play aiff files in mac using C

Thanks and responses much appreciated

geek

assoul
2004.09.27, 06:48 PM
Just to let u know...I am programming in C

skyhawk
2004.09.27, 07:02 PM
use OpenAL

blobbo
2004.09.27, 09:18 PM
Or quicktime. But it'll be you vs. the documentation. They make very little sense.

ThemsAllTook
2004.09.28, 10:11 AM
Either QuickTime or the Sound Manager would probably be your best bet if you're going for OS 9 compatibility. The QuickTime docucmentation is rather poor, though, so getting started with the Sound Manager may be easier.

Sound Manager Documentation (http://developer.apple.com/documentation/macos8/MultimediaGraphics/SoundManager/soundmanager.html)

Alex Diener

Josh
2004.09.28, 11:37 AM
FMOD is pretty good.

assoul
2004.09.28, 05:35 PM
thanks guys i will try out your suggestions and keep my fingers crossed

ERaZer
2004.09.28, 05:40 PM
FMOD is pretty good.
What's the license on FMOD?

Josh
2004.09.28, 10:24 PM
What's the license on FMOD?
Not a clue. :)

assoul
2004.10.01, 02:15 PM
Hey guys I used sound manager as suggested hacked together some code but i have this weird problem....my program goes like this

#include <stdio.h>
#include <Gestalt.h>
#include <Resources.h>
#include <Devices.h>
#include <ToolUtils.h>
#include <conio.h>
#include <SoundInput.h>
#include <stdlib.h>
#include <String.h>
#include "rmallws.c"
#include "snip_str.h"
#include "sniptype.h"
#include "extkword.h"

void soundPlay(char *);
char *rmallws(char *);

main()
{
FILE *fptr;
char filename[20],*str;
char str1[15];
fptr = fopen("List1.2","r");
if(fptr == NULL)
printf("Error Can't open file");
else
{
while(fgets(filename,20,fptr) != NULL)
{
strcpy(str1,"\\p:");
str = strtok(filename,"\t");
while(1) {
str = strtok(NULL,"\t");
if(str == NULL)
break;
strcat(str1,str);
/*rmallws(str1);*/
printf("%s\n",str1);



}soundPlay(str1);
}
}


/*getch();*/
return 0;
}
void soundPlay(char *str1)
{
OSErr osErr;
FSSpec fileSysSpec;
SInt16 fileRefNum;

osErr = FSMakeFSSpec(0,0,(const unsigned char *)str1,&fileSysSpec);

if(osErr == noErr)
osErr = FSpOpenDF(&fileSysSpec,fsRdPerm,&fileRefNum);
if(osErr == noErr)
SetFPos(fileRefNum,fsFromStart,0);
if(osErr == noErr)
osErr = SndStartFilePlay(NULL,fileRefNum,0,20480,NULL,NULL ,NULL,false);

FSClose(fileRefNum);
}
this just exits without playing the sound

however when i replace soundPlay(str1) with soundPlay((char *)"\p:filename") it works

any idea why this is happening....please HELP!!!!!

assoul
2004.10.01, 02:21 PM
the filename in \p:filename is not the same as the variable filename[20] what i meant was

if i call soundPlay((char *)"\p:F0205.aif") it plays the sound...not if i concatenate the string to give me the same....y i need to do this is cos of a file list im stuck here

NCarter
2004.10.01, 02:50 PM
Your problem is that you're trying to mix C strings and Pascal strings. C strings, as you probably know, are just a bunch of chars in a row, terminated with a NULL '\0' character. Pascal strings (which are required for many Carbon functions) are an unsigned char (0-255) which specifies how many characters follow, then the characters in sequence and possibly no NULL terminator. Typing a string as "\pText" tells the compiler to substitute the length byte for the \p (4 in this case). So, with this in mind, bad things are likely to happen when you call C functions like strtok() on your Pascal strings!

You've got a couple of choices: you can either use C strings for everything and convert the result into a Pascal string just before passing it to FSMakeFSSpec(), or you can use Pascal strings for everything and use various OS functions (http://developer.apple.com/documentation/Carbon/Reference/passtrings_reference/index.html?http://developer.apple.com/documentation/Carbon/Reference/passtrings_reference/passtrings_ref/Introduction.html) to process them.

Taxxodium
2004.10.01, 03:37 PM
I haven't tried this myself but according to the developer site at Apple, you can mix carbon in cocoa and vice versa. Which means that you can probably use Cocoa classes in your C app.

Why bother with Cocoa, you ask? Well, to play a sound in Cocoa all you need to do is [[NSSound soundNamed:@"myFile.aif"] play] and that's it.

EDIT: Ignore the above, I just read your app needs to be compatible with OS 9. So yes, Sound Manager/QuickTime is the way to go.

FCCovett
2004.10.01, 03:44 PM
Search for the source code of Glypha 3 (the source code comes with the game, which is free). It shows how to use Sound Manager on OS 9.

assoul
2004.10.01, 03:48 PM
well as per your suggestion i used the c2pstrcpy() function but i get a linker error saying undefined c2pstrcpy code referenced from main....i have used c strings thrughout and converting it to a pacal string Str255 and passing this string to the FSSpecMake

NCarter
2004.10.01, 04:22 PM
well as per your suggestion i used the c2pstrcpy() function but i get a linker error saying undefined c2pstrcpy code referenced from main....
What are you linking with? I'd imagine that function is in CarbonLib.

You could always try CopyCStringToPascal() instead.

assoul
2004.10.01, 06:02 PM
It is a stand alone application...I included TextUtils.h . I get the same error for CopyCStringToPAscal as well

Am i missing any other library files?

NCarter
2004.10.01, 06:10 PM
Are you not linking to CarbonLib? You need to! Drop it into your project if it isn't already there.

By the way, what development environment are you using? CodeWarrior? MPW?

assoul
2004.10.01, 06:16 PM
code warrior and where do i find the carbonlib??
and how do i include it in my project

NCarter
2004.10.01, 06:55 PM
On my CW8 installation the CarbonLib stub is here:

"Metrowerks CodeWarrior 8:Metrowerks CodeWarrior:MacOS Support:Universal:Libraries:StubLibraries:CarbonLi b"

To use it, just drag and drop it into your project window and build your app. I'm surprised that it isn't there already. What libraries are included in your project? None?

assoul
2004.10.11, 06:53 PM
I did exactly as i neil told me but nothing happening i build my application in mac os classic in codewarrior then include carbonlib stub into my libraries...i have the default libraries included when a classic app is built and also the cabon lib now i get lotta warnings about variables and functions being already defined in the other included libraries and my program hangs

list of included libraries

msi_aii_ppc_d.lib
msi_aii_ppc.lib
utcutils
textcommon
interfacelib
carbonlib
mathlib
unicodeconverter
msi_hfsplus.wke

i get no complilation errors but the program gives me many warnings on running and hangs

there is an obvious conflict in adding carbonlib and interfacelib....but if i remove interfacelib i cannot use sndstartfileplay which is absolutely necessary for this program
HELP!!!

NCarter
2004.10.12, 02:20 AM
there is an obvious conflict in adding carbonlib and interfacelib....but if i remove interfacelib i cannot use sndstartfileplay which is absolutely necessary for this program
You can't have CarbonLib and InterfaceLib in the same program because they're both defining many of the same symbols. As you've noticed, SndStartFilePlay() does not exist in Carbon. Unfortunately, you can't work around this by pulling it in from InterfaceLib because it contains code which is not compatible with Carbon. Instead, you need to write some code to emulate the missing function.

This Apple technote (http://developer.apple.com/technotes/tn/tn1198.html) explains how you can emulate SndStartFilePlay() with QuickTime. If you follow its advice you'll be able to leave out InterfaceLib (and the other Classic libraries) altogether.

assoul
2004.10.13, 06:18 PM
Thanks Neil that was ery helpful. I used the quicktime functions and followed the carbon porting guide to carbonize my app . got rid off all the conflicting libraries and now i get linker errors for functions referenced from msi_aii_ppc_d.lib .... and im not sure y this is happening. also im not quite clear abput the plst resource or how and where it should be added and if it has anything to do with my error.

to think this was a simple program its getting worse and worse

thanks in advance for your help

NCarter
2004.10.14, 12:02 PM
now i get linker errors for functions referenced from msi_aii_ppc_d.lib .... and im not sure y this is happening. also im not quite clear abput the plst resource or how and where it should be added and if it has anything to do with my error.
I don't know what msi_aii_ppc_d.lib is. My project has these files instead:

MSL_Runtime_PPC.Lib, MSL_Runtime_PPC_D.Lib
MSL_C_Carbon.Lib, MSL_C_Carbon_D.Lib
MSL_C++_PPC.Lib, MSL_C++_PPC_D.Lib

(Obviously the _D versions are in the debug target and the others are in the release target.)

I also don't recognise msi_hfsplus.wke. I would imagine that some or all of those files are only used when building for Mac OS 9 and earlier; you probably need some other similarly named files to build for OSX. CodeWarrior's release notes should hopefully give you some clues about which files you're looking for.

Which version of CW are you using?

also im not quite clear abput the plst resource or how and where it should be added and if it has anything to do with my error.
Read this (http://developer.apple.com/qa/qa2001/qa1034.html) (and the document it links to) to find out how to add a plst resource using the Rez compiler. Note that you only really need a plst resource if you're not building a bundled app; if your app is bundled, you can just place the Info.plist file in your bundle and add a carb 0 resource.

to think this was a simple program its getting worse and worse
Sadly, porting from the old Mac Toolbox to Carbon isn't as easy as you might hope. If you're lucky, you won't have to change much; if you're not, you might have to change the way the entire program works. At any rate, it sounds like you're getting pretty close now... don't give up! :)

assoul
2004.10.14, 05:30 PM
i followed the whole porting ritual...mine is a standalone application so i did not add a plist resource....anyways i get 36 linker errors that go something like this

undefined DIBadMount
referenced from userbreak in msl_all_ppc_d.lib
referenced from siouxhandleoneevent in msl_all_ppc_d.lib

and so on

what do i do here

i am using cw8

do i have to do smthn about the opaque toolboxes thing....im not quite clear wat i should be doing

NCarter
2004.10.15, 05:39 PM
i followed the whole porting ritual...mine is a standalone application so i did not add a plist resource....
If you're building a standalone (non-bundled) application, you do need a plst resource. That said, you can often still get away with just a carb 0 resource.

anyways i get 36 linker errors that go something like this

undefined DIBadMount
referenced from userbreak in msl_all_ppc_d.lib
referenced from siouxhandleoneevent in msl_all_ppc_d.lib
Ah, now I understand - I was confused by the way you spelt the library name in your previous post! You're using MSL_All_PPC_D.Lib, and you should be using MSL_All_Carbon_D.Lib (and the equivalent non-debug version). Look in "Metrowerks CodeWarrior 8/Metrowerks CodeWarrior/MacOS Support/Libraries/Runtime/Libs" to find the correct libraries.

do i have to do smthn about the opaque toolboxes thing....im not quite clear wat i should be doing
I think you've probably already dealt with this, otherwise your code wouldn't be compiling. For any data structure which is opaque in Carbon, you needed to switch from accessing it directly to using the appropriate accessor functions instead. Did you do that?

assoul
2004.10.15, 07:01 PM
firstly thanx for the patience in answerin my post ... u dunno how helpful its been
I just have a stupid question...wouldnt using the carbon stationery provided by CW8 solve these issues...tho i have to run this app on a os 9 machine....so does that affect anything

NCarter
2004.10.15, 07:26 PM
firstly thanx for the patience in answerin my post ... u dunno how helpful its been
No problem, I'm pleased to be able to help! I remember having these problems myself several years ago. :)

I just have a stupid question...wouldnt using the carbon stationery provided by CW8 solve these issues...tho i have to run this app on a os 9 machine....so does that affect anything
Yes, using the Carbon stationary and dropping your source files into it is probably the simplest way of getting a properly configured Carbon project. You might still have to fix some things, but it should be a bit easier than retrofitting the Carbon libraries into an old project.

The result will work on Mac OS 9 providing you use the Mac OS Carbon stationary and not the Mac OS X Mach-O version (of course).

assoul
2004.10.24, 05:17 PM
what am i doing wrong?

i am using quicktime to pla the sound file on carbon

this is the code

#include <stdio.h>
#include <Gestalt.h>
#include <Resources.h>
#include <Devices.h>
#include <ToolUtils.h>
#include <conio.h>
#include <SoundInput.h>
#include <stdlib.h>
#include <String.h>
#include <QuickTime.h>
#include <TextUtils.h>
#include<Movies.h>
#include<Sound.h>
#include<Carbon.h>


main(){

FSSpec theSpec;
OSErr err;
Movie theSound;
short fileRefNum;

err = FSMakeFSSpec(0,0,"\p:F0205.aif",&theSpec);

err = OpenMovieFile (&theSpec, &fileRefNum, fsRdPerm);

/* I printed out the error code here I got -2020....what does that mean */

if (noErr == err) {
err = NewMovieFromFile (&theSound, fileRefNum, 0, nil,
newMovieActive, nil);
}

if (noErr == err) {
GoToBeginningOfMovie (theSound);
}

if (noErr == err) {
StartMovie (theSound);
}

if (noErr == err) {
while (!IsMovieDone (theSound)) {
MoviesTask (theSound, 0);
}
}

}

it jusyt quits without playing the sound

this is code from the developer site .

any sugestions

NCarter
2004.10.25, 07:34 AM
err = OpenMovieFile (&theSpec, &fileRefNum, fsRdPerm);

/* I printed out the error code here I got -2020....what does that mean */

-2020 is the error code for movieToolboxUninitialized, which indicates that you didn't initialise the Movie Toolbox before calling its functions.

Call EnterMovies() (http://developer.apple.com/documentation/QuickTime/APIREF/entermovies.htm) once before OpenMovieFile() to fix this problem.

assoul
2004.10.27, 02:44 AM
finally got the soundplayer i needed...thanks a lot....i got a new issue tho

is there some way of reading user input from the keyboard thro the standard console dubug and writing it to a file

i seem stumped here cos the documentation is vague at best

NCarter
2004.11.01, 03:43 PM
Sorry about the slow response... I've been busy with my uDevGames entry.

is there some way of reading user input from the keyboard thro the standard console dubug and writing it to a file

i seem stumped here cos the documentation is vague at best
I'm not totally sure what you mean, but I presume you're talking about reading data from the Terminal. If that's the case, you'll need to read about standard C/C++ I/O functions such as fgets() (C) or iostream (C++). Sorry, this isn't my field of expertise - I skipped over that bit when I taught myself to program!