9 #include "rpage/aos/inc.prl" 10 #include <exec/types.h> 11 #include <exec/memory.h> 12 #include <devices/audio.h> 24 #define CLOCK_CONSTANT 3579545 25 #define MUSIC_PRIORITY 0 27 extern struct DosLibrary *
DOSBase;
30 struct IOAudio *IOA[4] = {NULL, NULL, NULL, NULL};
45 SoundInfo *LoadPackedSound(
char *filename,
BYTE *packed_block,
BYTE *unpacked_block)
47 #ifdef DEBUG_ENABLE_AUDIO 48 ULONG encoded_block_size = 0, unpacked_block_size = 0, packed_block_size = 0;
49 ULONG frequency = 8000;
51 char encoder_tag[4], compressor_tag[4], tag[4];
53 BYTE *encoded_block = NULL;
56 printf(
"LoadPackedSound(%s)\n", filename);
58 if ((fileHandle = Open(filename, MODE_OLDFILE)))
60 Read(fileHandle, &tag, 4);
61 if (strncmp(tag,
"SMPK", 4) == 0)
64 Read(fileHandle, &tag, 4);
65 if (strncmp(tag,
"SIZE", 4) == 0)
67 Read(fileHandle, &unpacked_block_size, 4);
69 printf(
"unpacked_block_size = %d\n", unpacked_block_size);
72 Read(fileHandle, &tag, 4);
73 Read(fileHandle, &frequency, 4);
76 Read(fileHandle, &encoder_tag, 4);
78 printf(
"encoder found : %c%c%c%c.\n", encoder_tag[0],encoder_tag[1],encoder_tag[2],encoder_tag[3]);
80 Read(fileHandle, &tag, 4);
81 if (strncmp(tag,
"SIZE", 4) == 0)
83 Read(fileHandle, &encoded_block_size, 4);
85 printf(
"encoded_block_size = %d\n", encoded_block_size);
88 Read(fileHandle, &compressor_tag, 4);
90 printf(
"compressor found : %c%c%c%c.\n", compressor_tag[0],compressor_tag[1],compressor_tag[2],compressor_tag[3]);
92 Read(fileHandle, &tag, 4);
93 if (strncmp(tag,
"SIZE", 4) == 0)
95 Read(fileHandle, &packed_block_size, 4);
97 printf(
"packed_block_size = %d\n", packed_block_size);
99 Read(fileHandle, packed_block, packed_block_size);
110 if ((encoded_block_size > 0) && (unpacked_block_size > 0) && (packed_block_size > 0))
113 if (unpacked_block == NULL)
114 unpacked_block = AllocMem(unpacked_block_size, MEMF_CHIP);
116 encoded_block = (
UBYTE *)calloc(encoded_block_size,
sizeof(
UBYTE));
117 assert(unpacked_block != NULL);
119 if (encoded_block != NULL)
124 if (strncmp(compressor_tag,
"MINZ", 4) == 0)
126 else if (strncmp(compressor_tag,
"SHRK", 4) == 0)
127 ShrinklerDecompress(packed_block, encoded_block, NULL, NULL);
128 else if (strncmp(compressor_tag,
"NRV2", 4) == 0)
129 nrv2s_unpack(packed_block, encoded_block);
131 if (strncmp(encoder_tag,
"ADPC", 4) == 0)
133 adpcm_decode(encoded_block, encoded_block_size, unpacked_block);
135 else if (strncmp(encoder_tag,
"GLI2", 4) == 0)
137 printf(
"Gligli MDPCM not supported yet!\n");
139 else if (strncmp(encoder_tag,
"8SVX", 4) == 0)
141 memcpy(unpacked_block, encoded_block, unpacked_block_size);
144 sound = (SoundInfo *)calloc(1,
sizeof(SoundInfo));
145 assert(sound != NULL);
146 sound->SoundBuffer = unpacked_block;
147 sound->FileLength = unpacked_block_size;
148 sound->RecordRate = frequency;
151 encoded_block = NULL;
157 printf(
"LoadPackedSound(), encoded_block calloc() failed!\n");
166 printf(
"!Not a SMPK File!\n");
205 SoundInfo *PrepareSound(STRPTR file)
207 #ifdef DEBUG_ENABLE_AUDIO 209 SoundInfo *info = NULL;
213 info = (SoundInfo *)AllocMem(
sizeof(SoundInfo), MEMF_PUBLIC | MEMF_CLEAR);
220 if (info->FileLength = GetSize(file))
225 (
BYTE *)AllocMem(info->FileLength, MEMF_CHIP | MEMF_CLEAR);
227 if (info->SoundBuffer)
233 if (info->RecordRate = LoadSound(file, info))
255 FreeMem(info->SoundBuffer, info->FileLength);
260 FreeMem(info,
sizeof(SoundInfo));
292 WORD delta_rate,
UWORD repeat)
294 #ifdef DEBUG_ENABLE_AUDIO 302 if (PrepareIOA(CLOCK_CONSTANT / info->RecordRate + delta_rate, volume, repeat,
306 BeginIO((
struct IORequest *)IOA[channel]);
308 printf(
"PlaySound(), IOA[%d] = %x\n", channel, IOA[channel]);
329 void StopSound(
UBYTE channel)
331 #ifdef DEBUG_ENABLE_AUDIO 336 AbortIO((
struct IORequest *)IOA[channel]);
339 if (IOA[channel]->ioa_Request.io_Device)
340 CloseDevice((
struct IORequest *)IOA[channel]);
343 if (IOA[channel]->ioa_Request.io_Message.mn_ReplyPort)
344 DeletePort(IOA[channel]->ioa_Request.io_Message.mn_ReplyPort);
346 printf(
"StopSound(), IOA[%d] = %x\n", channel, IOA[channel]);
348 if (IOA[channel] != NULL)
350 FreeMem(IOA[channel],
sizeof(
struct IOAudio));
369 void RemoveSound(SoundInfo *info)
371 #ifdef DEBUG_ENABLE_AUDIO 380 printf(
"RemoveSound(), SoundBuffer = %x, FileLength = %d\n", info->SoundBuffer, info->FileLength);
382 if (info->SoundBuffer != NULL)
384 FreeMem(info->SoundBuffer, info->FileLength);
385 info->SoundBuffer = NULL;
397 void RemoveSoundStruct(SoundInfo *info)
399 #ifdef DEBUG_ENABLE_AUDIO 430 #ifdef DEBUG_ENABLE_AUDIO 434 struct MsgPort *port;
437 IOA[channel] = (
struct IOAudio *)AllocMem(
sizeof(
struct IOAudio),
438 MEMF_PUBLIC | MEMF_CLEAR);
444 if ((port = (
struct MsgPort *)CreatePort(
"Sound Port", 0)) == NULL)
448 if (IOA[channel] != NULL)
450 FreeMem(IOA[channel],
sizeof(
struct IOAudio));
462 IOA[channel]->ioa_Request.io_Message.mn_Node.ln_Pri = MUSIC_PRIORITY;
465 IOA[channel]->ioa_Request.io_Message.mn_ReplyPort = port;
469 IOA[channel]->ioa_Data = &ch;
472 IOA[channel]->ioa_Length =
sizeof(
UBYTE);
475 if (OpenDevice(AUDIONAME, 0, (
struct IORequest *)IOA[channel], 0))
482 if (IOA[channel] != NULL)
484 FreeMem(IOA[channel],
sizeof(
struct IOAudio));
494 IOA[channel]->ioa_Request.io_Flags = ADIOF_PERVOL;
495 IOA[channel]->ioa_Request.io_Command = CMD_WRITE;
496 IOA[channel]->ioa_Period = period;
497 IOA[channel]->ioa_Volume = volume;
498 IOA[channel]->ioa_Cycles = cycles;
503 if (info->FileLength > 131000)
504 IOA[channel]->ioa_Length = 131000;
506 IOA[channel]->ioa_Length = info->FileLength;
510 IOA[channel]->ioa_Data = info->SoundBuffer;
533 UWORD LoadSound(STRPTR filename, SoundInfo *info)
535 #ifdef DEBUG_ENABLE_AUDIO 541 if (SizeIFF(filename))
544 return (ReadIFF(filename, info));
550 if ((file_ptr = fopen(filename,
"r")) == 0)
554 if (fread((
char *)&length,
sizeof(
ULONG), 1, file_ptr) == 0)
563 if (fread((
char *)&record_rate,
sizeof(
UWORD), 1, file_ptr) == 0)
572 if (fread((
char *)info->SoundBuffer, length, 1, file_ptr) == 0)
585 return (record_rate);
601 ULONG GetSize(STRPTR filename)
603 #ifdef DEBUG_ENABLE_AUDIO 608 if ((length = SizeIFF(filename)) == 0)
612 if ((file_ptr = fopen(filename,
"r")) == 0)
616 if (fread((
char *)&length,
sizeof(
ULONG), 1, file_ptr) == 0)
643 ULONG SizeIFF(STRPTR filename)
645 #ifdef DEBUG_ENABLE_AUDIO 647 STRPTR empty_string =
" ";
652 if (file_ptr = fopen(filename,
"r"))
654 fread((
char *)empty_string, 4, 1, file_ptr);
655 if (strcmp(empty_string,
"FORM") == 0)
658 fread((
char *)empty_string, 4, 1, file_ptr);
659 fread((
char *)empty_string, 4, 1, file_ptr);
662 if (strcmp(empty_string,
"8SVX") == 0)
664 MoveTo(
"VHDR", file_ptr);
665 fread((
char *)&dummy,
sizeof(LONG), 1, file_ptr);
666 fread((
char *)&Header,
sizeof(Header), 1, file_ptr);
673 return (Header.oneShotHiSamples + Header.repeatHiSamples);
697 UWORD ReadIFF(STRPTR filename, SoundInfo *info)
699 #ifdef DEBUG_ENABLE_AUDIO 701 STRPTR empty_string =
" ";
706 if (file_ptr = fopen(filename,
"r"))
708 fread((
char *)empty_string, 4, 1, file_ptr);
709 if (strcmp(empty_string,
"FORM") == 0)
712 fread((
char *)empty_string, 4, 1, file_ptr);
713 fread((
char *)empty_string, 4, 1, file_ptr);
716 if (strcmp(empty_string,
"8SVX") == 0)
718 MoveTo(
"VHDR", file_ptr);
719 fread((
char *)&dummy,
sizeof(LONG), 1, file_ptr);
720 fread((
char *)&Header,
sizeof(Header), 1, file_ptr);
722 MoveTo(
"BODY", file_ptr);
723 fread((
char *)&dummy,
sizeof(LONG), 1, file_ptr);
724 fread((
char *)info->SoundBuffer,
725 Header.oneShotHiSamples + Header.repeatHiSamples, 1, file_ptr);
731 return (Header.samplesPerSec);
751 BOOL MoveTo(STRPTR check_string, FILE *file_ptr)
753 #ifdef DEBUG_ENABLE_AUDIO 754 STRPTR empty_string =
" ";
759 while (!feof(file_ptr))
761 fread((
char *)empty_string, 4, 1, file_ptr);
764 if (strcmp(check_string, empty_string) == 0)
768 fread((
char *)&skip,
sizeof(LONG), 1, file_ptr);
769 for (loop = 0; loop < skip; loop++)
770 fread((
char *)&dummy, 1, 1, file_ptr);
778 BYTE *adpcm_decode(
UBYTE *Source,
int Length,
BYTE *Destination)
780 #ifdef DEBUG_ENABLE_AUDIO 781 const ULONG JoinCode = 0;
782 WORD EstMax = (WORD)(JoinCode & 0xffff);
783 UWORD Delta = (
UWORD)((JoinCode & 0xffff0000) >> 16);
785 const UBYTE Bits = 4;
787 if(!Delta) Delta = 5;
792 UBYTE sampleCount = 24/Bits;
793 ULONG temp = (Source[0] << 16) | (Source[1] << 8) | Source[2];
796 while(sampleCount--) {
797 WORD newEstMax = (Delta >> 1);
798 UBYTE Shifter = (temp >> sampleCount*Bits);
799 UBYTE b = (Shifter & bitmask[Bits-1]);
801 if ((Bits == 4) && ((Shifter & 0xf) == 0))
808 lDelta = Delta * Matrix[Bits-2][Shifter & bitmask[Bits-1]];
810 if(Shifter & (1<<(Bits-1))) {
811 newEstMax = -newEstMax;
813 EstMax = (EstMax + newEstMax) & 0xffff;
815 Delta = (
UWORD)((LONG)(lDelta + 8192) >> 14);
817 if(Delta < 5) Delta = 5;
819 newEstMax = EstMax >> 6;
821 *Destination++ = 127;
822 else if( -128 > newEstMax) {
823 *Destination++ = -128;
826 *Destination++ = newEstMax;
const char * err_no_size_found
struct DosLibrary * DOSBase
size_t tinfl_decompress_mem_to_mem(void *pOut_buf, size_t out_buf_len, const void *pSrc_buf, size_t src_buf_len, int flags)