NSSound initWithData?
NSSound initWithData?
- Subject: NSSound initWithData?
- From: Jason Wright <email@hidden>
- Date: Thu, 30 Oct 2003 20:23:45 -0500
(Apologies if this is a dupe, I didn't see the original appear in the
archives nor did I receive a copy back from the list).
The code snippet below is my attempt to use NSSound initWithData to
play 5 seconds of silence. I craft what I believe is a valid WAV file,
but no sound is played (and 'play' returns NO). Am I missing something
here? I'm relatively new to programming under MacOS, but no stranger
to programming generally.
I'd greatly appreciate an appropriate kick in the right direction.
Also, let me know if this is not the correct forum for this question as
well.
--Jason L. Wright
NSMutableData *data;
u_int8_t *noise;
u_int32_t v;
unsigned totallen = 44 + (5 * 44100 * sizeof(int16_t));
data = [NSMutableData dataWithCapacity: totallen];
noise = [data mutableBytes];
/* 'RIFF' */
noise[0] = 'R';
noise[1] = 'I';
noise[2] = 'F';
noise[3] = 'F';
/* total size, including header */
v = sizeof(int16_t) * (5 * 44100) + 36;
noise[4] = (v & 0x000000ff) >> 0;
noise[5] = (v & 0x0000ff00) >> 8;
noise[6] = (v & 0x00ff0000) >> 16;
noise[7] = (v & 0xff000000) >> 24;
/* 'WAVE' */
noise[8] = 'W';
noise[9] = 'A';
noise[10] = 'V';
noise[11] = 'E';
/* format chunk: start */
noise[12] = 'f';
noise[13] = 'm';
noise[14] = 't';
noise[15] = ' ';
/* format chunk: size */
v = 16;
noise[16] = (v & 0x000000ff) >> 0;
noise[17] = (v & 0x0000ff00) >> 8;
noise[18] = (v & 0x00ff0000) >> 16;
noise[19] = (v & 0xff000000) >> 24;
/* format chunk: format tag (uncompressed) */
v = 1;
noise[20] = (v & 0x00ff) >> 0;
noise[21] = (v & 0xff00) >> 8;
/* format chunk: channels */
v = 1;
noise[22] = (v & 0x00ff) >> 0;
noise[23] = (v & 0xff00) >> 8;
/* format chunk: frequency */
v = 44100;
noise[24] = (v & 0x000000ff) >> 0;
noise[25] = (v & 0x0000ff00) >> 8;
noise[26] = (v & 0x00ff0000) >> 16;
noise[27] = (v & 0xff000000) >> 24;
/* format chunk: bytes/second */
v = 44100 * sizeof(int16_t);
noise[28] = (v & 0x000000ff) >> 0;
noise[29] = (v & 0x0000ff00) >> 8;
noise[30] = (v & 0x00ff0000) >> 16;
noise[31] = (v & 0xff000000) >> 24;
/* format chunk: block alignment */
v = sizeof(int16_t);
noise[32] = (v & 0x00ff) >> 0;
noise[33] = (v & 0xff00) >> 8;
/* format chunk: bits/sample */
v = sizeof(int16_t) * 8;
noise[34] = (v & 0x00ff) >> 0;
noise[35] = (v & 0xff00) >> 8;
/* data chunk: start */
noise[36] = 'd';
noise[37] = 'a';
noise[38] = 't';
noise[39] = 'a';
/* data chunk: data size */
v = 5 * 44100 * sizeof(int16_t);
noise[40] = (v & 0x000000ff) >> 0;
noise[41] = (v & 0x0000ff00) >> 8;
noise[42] = (v & 0x00ff0000) >> 16;
noise[43] = (v & 0xff000000) >> 24;
memset(&noise[44], '\0', 5 * 44100 * sizeof(int16_t));
x = false;
y = true;
s = [[NSString alloc] initWithCString:"Stop"];
data = [[NSData alloc] initWithBytesNoCopy:noise length:(44 +
(5 * 44100 * sizeof(int16_t)))];
snd = [[NSSound alloc] initWith
Data:data];
[snd setName:[[NSString alloc] initWithCString:"blah.wav"]];
if ([snd play] == NO) {
printf("it didn't start playing.\n");
} else {
printf("It played...\n");
}
_______________________________________________
coreaudio-api mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/coreaudio-api
Do not post admin requests to the list. They will be ignored.