|
Newbie
Join Date: Apr 2006
Posts: 5
Rep Power: 0 
|
XCode-increase amplitude code required
hello
i have an XCode prgram which i have started. I want to get the program to read AIFF file (which i have working now), and increse the amplitude by a slider variable. Basically the slider will control how much the amplitude will be increased OR decreased (by a MAXIMUM of 3 times).
I WOULD REALLY APPRECIATE THE HELP!!!!
Can any PLEASE give me the code for this i have the following code to show you what i have so far:
CARBON APPLICATION BY JAMES GREGSON
#include <Carbon/Carbon.h>
#define kControlSignature '????'
// menus
#define kFileMenuID 20
#define kFileOpenMenuID 2
#define kOpenFile 'open'
#define kSaveAsFile 'svas'
#define kQuit 'quit'
// Text boxes
#define kFORM_ID_Id 1
#define kFORM_ID 'fmid'
#define kFORM_chunksizeID 2
#define kFORM_chunksize 'fmcs'
#define kFORM_typeId 3
#define kFORM_type 'fmtp'
#define kCOMM_ID_Id 4
#define kCOMM_ID 'cmid'
#define kCOMM_chunksizeID 5
#define kCOMM_chunksize 'cmcs'
#define kCOMM_numChannelsID 6
#define kCOMM_numChannels 'numc'
#define kCOMM_numSampleFramesID 7
#define kCOMM_numSampleFrames 'numf'
#define kCOMM_bitDepthID 8
#define kCOMM_bitDepth 'bitd'
#define kCOMM_sampleRateID 9
#define kCOMM_sampleRate 'sprt'
// took from ieee
# define UnsignedToFloat(u) (((double)((long)(u - 2147483647L - 1))) + 2147483648.0)
// this is needed for the sample rate
// global variables
NavEventUPP gNavEventHandlerPtr;
ControlHandle FORM_ID_ControlHandle;
ControlID FORM_ID_Id = { kControlSignature, kFORM_ID_Id };
ControlHandle FORM_chunksizeControlHandle;
ControlID FORM_chunksizeID = { kControlSignature, kFORM_chunksizeID };
ControlHandle FORM_typeControlHandle;
ControlID FORM_typeId = { kControlSignature, kFORM_typeId };
ControlHandle COMM_ID_ControlHandle;
ControlID COMM_ID_Id = { kControlSignature, kCOMM_ID_Id };
ControlHandle COMM_chunksizeControlHandle;
ControlID COMM_chunksizeID = { kControlSignature, kCOMM_chunksizeID };
ControlHandle COMM_numChannelsControlHandle;
ControlID COMM_numChannelsID = { kControlSignature, kCOMM_numChannelsID };
ControlHandle COMM_numSampleFramesControlHandle;
ControlID COMM_numSampleFramesID = { kControlSignature, kCOMM_numSampleFramesID };
ControlHandle COMM_bitDepthControlHandle;
ControlID COMM_bitDepthID = { kControlSignature, kCOMM_bitDepthID };
ControlHandle COMM_sampleRateControlHandle;
ControlID COMM_sampleRateID = { kControlSignature, kCOMM_sampleRateID };
pascal OSStatus CommandEventHandler ( EventHandlerCallRef handlerRef, EventRef event, void *userData);
//OSStatus DisplayOpenInputFileDialog(void);
pascal void ThisOpenDialogEventCallback(NavEventCallbackMessage callBackSelector,
NavCBRecPtr callBackParms,
void* callBackUD);
short OpenMyInputfile( NavReplyRecord *reply );
void ReadMyInputFile(short MyInputFileRefNum);
/*pascal void ThisSaveDialogEventCallback(NavEventCallbackMessage callBackSelector,
NavCBRecPtr callBackParms,
void* callBackUD);*/
/**********/
pascal void ThisSaveDialogEventCallback(NavEventCallbackMessage callBackSelector,
NavCBRecPtr callBackParms,
void* callBackUD)
{
OSStatus err = 0;
NavReplyRecord reply;
NavUserAction UserAction = 0;
switch (callBackSelector)
{
case kNavCBUserAction :err= NavDialogGetReply
(callBackParms->context, & reply);
UserAction=
NavDialogGetUserAction(callBackParms->context );
switch ( UserAction )
{
case kNavUserActionSaveAs :
err = CreateOutputAIFFfile( &reply );
break;
}
err = NavDisposeReply( &reply );
break;
case kNavCBTerminate : NavDialogDispose (callBackParms->context);
DisposeNavEventUPP(gNavEventHandlerPtr);
break;
}
return err;
}
/**********/
// put this in 2
double ConvertFromIeeeExtended(unsigned char* bytes);
WindowRef window;
int main(int argc, char* argv[])
{
IBNibRef nibRef;
WindowRef window;
OSStatus err;
EventTargetRef target;
EventHandlerUPP handlerUPP;
EventTypeSpec cmdEvent = { kEventClassCommand, kEventProcessCommand };
// Create a Nib reference passing the name of the nib file (without the .nib extension)
// CreateNibReference only searches into the application bundle.
err = CreateNibReference(CFSTR("main"), &nibRef);
require_noerr( err, CantGetNibRef );
// Once the nib reference is created, set the menu bar. "MainMenu" is the name of the menu bar
// object. This name is set in InterfaceBuilder when the nib is created.
err = SetMenuBarFromNib(nibRef, CFSTR("MenuBar"));
require_noerr( err, CantSetMenuBar );
// Then create a window. "MainWindow" is the name of the window object. This name is set in
// InterfaceBuilder when the nib is created.
err = CreateWindowFromNib(nibRef, CFSTR("MainWindow"), &window);
require_noerr( err, CantCreateWindow );
// We don't need the nib reference anymore.
DisposeNibReference(nibRef);
target = GetWindowEventTarget ( window );
handlerUPP = NewEventHandlerUPP ( CommandEventHandler );
InstallEventHandler ( target, handlerUPP, 1, &cmdEvent, (void *)window, NULL );
// The window was created hidden so show it.
ShowWindow( window );
// Call the event loop
RunApplicationEventLoop();
CantCreateWindow:
CantSetMenuBar:
CantGetNibRef:
return err;
}
pascal OSStatus CommandEventHandler ( EventHandlerCallRef handlerRef, EventRef event, void *userData)
{
OSStatus result = eventNotHandledErr;
HICommand command;
WindowRef window;
window = ( WindowRef ) userData;
GetControlByID ( window,&FORM_ID_Id, &FORM_ID_ControlHandle);
GetControlByID ( window,&FORM_chunksizeID, &FORM_chunksizeControlHandle);
GetControlByID ( window,&FORM_typeId ,&FORM_typeControlHandle);
GetControlByID ( window,&COMM_ID_Id ,&COMM_ID_ControlHandle);
GetControlByID ( window,&COMM_chunksizeID, &COMM_chunksizeControlHandle);
GetControlByID ( window,&COMM_numChannelsID, &COMM_numChannelsControlHandle);
GetControlByID ( window,&COMM_numSampleFramesID,&COMM_numSampleFramesControlHandle);
GetControlByID ( window,&COMM_bitDepthID, &COMM_bitDepthControlHandle);
GetControlByID ( window,&COMM_sampleRateID, &COMM_sampleRateControlHandle);
GetEventParameter (event, kEventParamDirectObject, typeHICommand, NULL, sizeof (HICommand), NULL, &command);
switch (command.commandID )
{
case kOpenFile: result = DisplayOpenInputFileDialog();
break;
case kSaveAsFile: result = DisplaySaveAsOutputFileDialog();
break;
}
return result;
}
OSStatus DisplayOpenInputFileDialog()
{
OSStatus err;
NavDialogRef openDialog;
NavDialogCreationOptions dialogAttributes;
err = noErr;
err = NavGetDefaultDialogCreationOptions(&dialogAttributes);
dialogAttributes.modality = kWindowModalityAppModal;
gNavEventHandlerPtr = NewNavEventUPP(ThisOpenDialogEventCallback);
err = NavCreateGetFileDialog(&dialogAttributes, NULL, gNavEventHandlerPtr,
NULL, NULL, NULL, &openDialog);
err = NavDialogRun( openDialog);
if (err != noErr)
{
NavDialogDispose( openDialog);
DisposeNavEventUPP(gNavEventHandlerPtr);
}
}
OSStatus DisplaySaveAsOutputFileDialog()
{
OSStatus err;
NavDialogRef saveDialog;
NavDialogCreationOptions dialogAttributes;
err = NavGetDefaultDialogCreationOptions(&dialogAttributes);
dialogAttributes.modality = kWindowModalityAppModal;
gNavEventHandlerPtr = NewNavEventUPP(ThisSaveDialogEventCallback);
err = NavCreatePutFileDialog(&dialogAttributes, 'AIFF', '????',
gNavEventHandlerPtr, NULL, &saveDialog);
err = NavDialogRun( saveDialog);
if (err != noErr)
{
NavDialogDispose( saveDialog);
DisposeNavEventUPP(gNavEventHandlerPtr);
}
return err;
}
pascal void ThisOpenDialogEventCallback(NavEventCallbackMessage callBackSelector,
NavCBRecPtr callBackParms,
void* callBackUD)
{
OSStatus err = 0;
NavReplyRecord reply;
NavUserAction userAction = 0;
short MyInputFileRefNum;
switch (callBackSelector)
{
case kNavCBUserAction : err = NavDialogGetReply
(callBackParms->context, &reply);
userAction = NavDialogGetUserAction
(callBackParms->context );
switch ( userAction )
{
case kNavUserActionOpen : MyInputFileRefNum = OpenMyInputfile( &reply );
ReadMyInputFile(MyInputFileRefNum);
break;
}
err = NavDisposeReply( &reply );
break;
case kNavCBTerminate : NavDialogDispose (callBackParms->context);
DisposeNavEventUPP(gNavEventHandlerPtr);
break;
}
}
/*pascal void ThisSaveDialogEventCallback(NavEventCallbackMessage callBackSelector,
NavCBRecPtr callBackParms,
void* callBackUD)
{
OSStatus err = 0;
NavReplyRecord reply;
NavUserAction UserAction = 0;
switch (callBackSelector)
{
case kNavCBUserAction :err= NavDialogGetReply
(callBackParms->context, & reply);
UserAction=
NavDialogGetUserAction(callBackParms->context );
switch ( UserAction )
{
case kNavUserActionSaveAs :
err = CreateOutputAIFFfile( &reply );
break;
}
err = NavDisposeReply( &reply );
break;
case kNavCBTerminate : NavDialogDispose (callBackParms->context);
DisposeNavEventUPP(gNavEventHandlerPtr);
break;
}
return err;
}*/
short OpenMyInputfile( NavReplyRecord *reply )
{
OSStatus err;
AEDesc newDescriptor;
FSRef MyfileRef;
short MyInputFileRefNum;
FSSpec MyfileFSSpec;
err = AECoerceDesc(&reply->selection, typeFSRef, &newDescriptor);
err = AEGetDescData(&newDescriptor, (void *) (&MyfileRef), sizeof(FSRef) );
err = FSGetCatalogInfo(&MyfileRef, kFSCatInfoNone, NULL, NULL, &MyfileFSSpec, NULL);
err = FSpOpenDF (&MyfileFSSpec, fsCurPerm, &MyInputFileRefNum);
return MyInputFileRefNum;
}
void ReadMyInputFile(short MyInputFileRefNum)
{
OSStatus err;
long count = 2;
CFStringRef theCFString;
Str255 formTypeString, channelTypeString, chunksizeString, SampleFramesString, bitDepthString, sampleRateString;
int i;
char byte, string[5];
long formType, chunkSize, SampleFrames;
double sampleRate;
short numOfChannels, bitDepth;
// read the FORM chunk
// first the ID...
count = 1;
for (i=0;i<4;i++)
{
FSRead(MyInputFileRefNum,&count, &byte);
string[i] = byte;
}
string[4] = '\0' ;
theCFString = CFStringCreateWithCString (kCFAllocatorDefault, &string, kCFStringEncodingASCII);
SetControlData( FORM_ID_ControlHandle, kControlEntireControl, kControlEditTextCFStringTag,
sizeof(CFStringRef), &theCFString );
//dont need to display these we have to READ and WRITE at the same time
// then the chunk size
count = 4;
FSRead(MyInputFileRefNum,&count, &formType);
NumToString(formType, formTypeString );
SetControlData( FORM_chunksizeControlHandle, 0, kControlEditTextTextTag,
formTypeString[0], (Ptr)(formTypeString+1) );
// then the type
count = 1;
for (i=0;i<4;i++)
{
FSRead(MyInputFileRefNum,&count, &byte);
string[i] = byte;
}
string[4] = '\0' ;
theCFString = CFStringCreateWithCString (kCFAllocatorDefault, &string, kCFStringEncodingASCII);
SetControlData( FORM_typeControlHandle, kControlEntireControl, kControlEditTextCFStringTag,
sizeof(CFStringRef), &theCFString );
// we've finished with the FORM chunk
// put the details on screen
DrawOneControl (FORM_ID_ControlHandle);
DrawOneControl (FORM_chunksizeControlHandle);
DrawOneControl (FORM_typeControlHandle);
// read the comm chunk....???
// first the chunk id?? ?
count = 1;
for (i=0;i<4;i++)
{
FSRead(MyInputFileRefNum,&count, &byte);
string[i] = byte;
}
string[4] = '\0' ;
theCFString = CFStringCreateWithCString (kCFAllocatorDefault, &string, kCFStringEncodingASCII);
SetControlData( COMM_ID_ControlHandle, kControlEntireControl, kControlEditTextCFStringTag,
sizeof(CFStringRef), &theCFString );
// comm chunk size
count = 4;
FSRead(MyInputFileRefNum,&count, &chunkSize);
NumToString(chunkSize, chunksizeString );
SetControlData( COMM_chunksizeControlHandle, 0, kControlEditTextTextTag,
chunksizeString[0], (Ptr)(chunksizeString+1) );
// num channels
count = 2;
FSRead(MyInputFileRefNum,&count, &numOfChannels);
NumToString(numOfChannels, channelTypeString );
SetControlData( COMM_numChannelsControlHandle, 0, kControlEditTextTextTag,
channelTypeString[0], (Ptr)(channelTypeString+1) );
// num sample frames
count = 4;
FSRead(MyInputFileRefNum,&count, &SampleFrames);
NumToString(SampleFrames, SampleFramesString );
SetControlData( COMM_numSampleFramesControlHandle, 0, kControlEditTextTextTag,
SampleFramesString[0], (Ptr)(SampleFramesString+1) );
// num sample size (bitdepth)
count = 2;
FSRead(MyInputFileRefNum,&count, &bitDepth);
NumToString(bitDepth, bitDepthString );
SetControlData( COMM_bitDepthControlHandle, 0, kControlEditTextTextTag,
bitDepthString[0], (Ptr)(bitDepthString+1) );
// sample rate... ... ...
count = 1;
for (i=0;i<10;i++)
{
FSRead(MyInputFileRefNum,&count, &byte);
sampleRate = byte;
}
sampleRate = ConvertFromIeeeExtended(&sampleRate);
NumToString(sampleRate, sampleRateString );
SetControlData( COMM_sampleRateControlHandle, 0, kControlEditTextTextTag,
sampleRateString[0], (Ptr)(sampleRateString+1) );
// the sound data chunk must be taken care of next
}
// put this bit in from convert to Ieee
double ConvertFromIeeeExtended(unsigned char* bytes)
{
double f;
int expon;
unsigned long hiMant, loMant;
expon = ((bytes[0] & 0x7F) << 8) | (bytes[1] & 0xFF);
hiMant = ((unsigned long)(bytes[2] & 0xFF) << 24)
| ((unsigned long)(bytes[3] & 0xFF) << 16)
| ((unsigned long)(bytes[4] & 0xFF) << 8)
| ((unsigned long)(bytes[5] & 0xFF));
loMant = ((unsigned long)(bytes[6] & 0xFF) << 24)
| ((unsigned long)(bytes[7] & 0xFF) << 16)
| ((unsigned long)(bytes[8] & 0xFF) << 8)
| ((unsigned long)(bytes[9] & 0xFF));
if (expon == 0 && hiMant == 0 && loMant == 0) {
f = 0;
}
else {
if (expon == 0x7FFF) { /* Infinity or NaN */
f = HUGE_VAL;
}
else {
expon -= 16383;
f = ldexp(UnsignedToFloat(hiMant), expon-=31);
f += ldexp(UnsignedToFloat(loMant), expon-=32);
}
}
if (bytes[0] & 0x80)
return -f;
else
return f;
}
THANKYOU FOR READING!! I WOULD REALLY APPRECIATE THE HELP
|