int aifFile::ReadChunk() { if (fd == NULL) return -1; // Read the common header for all aif chunks aifHeader header; if (fcbs->read(&header, 1, sizeof(aifHeader), fd) != sizeof(aifHeader)) { fcbs->close(fd); fd = NULL; return -1; } switch (header.id) { case FORM_ID: int aifId; if (fcbs->read(&aifId, 1, sizeof(int), fd) != sizeof(int)) return -1; // Check aifId signature for valid file if (aifId != AIFF_ID) return -1; break; case COMM_ID: commHeader comm; if (fcbs->read(&comm, 1, sizeof(commHeader), fd) != sizeof(commHeader)) return -1; channels = SWAP16(comm.channels); samplesPerSec = (int)ConvertFromIeeeExtended(comm.sampleRate80); bitsPerSample = SWAP16(comm.sampleSize); status = 1; break; case SSND_ID: ssndHeader ssnd; if (fcbs->read(&ssnd, 1, sizeof(ssndHeader), fd) != sizeof(ssndHeader)) return -1; if (ssnd.offset) { fcbs->seek(fd, SWAP32(ssnd.offset), SEEK_CUR); } chunkSize = SWAP32(header.size) - 8; totalSamples = chunkSize / (bitsPerSample/8); status = 2; break; default: fcbs->seek(fd, SWAP32(header.size), SEEK_CUR); break; } return 0; }
double wxArchive::LoadDouble() { double value = 0; // reads a character from the stream if(CanLoad()) { #if wxUSE_APPLE_IEEE unsigned char buf[10]; m_idstr.Read((void *)buf, 10); value = ConvertFromIeeeExtended(buf); #else #pragma warning "wxArchive::LoadDouble() not using IeeeExtended - will not work!" #endif } return value; }
wxDataInputStream::wxDataInputStream(wxInputStream& s, const wxMBConv& conv) : m_input(&s), m_be_order(false), m_conv(conv.Clone()) #else wxDataInputStream::wxDataInputStream(wxInputStream& s) : m_input(&s), m_be_order(false) #endif { } wxDataInputStream::~wxDataInputStream() { #if wxUSE_UNICODE delete m_conv; #endif // wxUSE_UNICODE } #if wxHAS_INT64 wxUint64 wxDataInputStream::Read64() { wxUint64 tmp; Read64(&tmp, 1); return tmp; } #endif // wxHAS_INT64 wxUint32 wxDataInputStream::Read32() { wxUint32 i32; m_input->Read(&i32, 4); if (m_be_order) return wxUINT32_SWAP_ON_LE(i32); else return wxUINT32_SWAP_ON_BE(i32); } wxUint16 wxDataInputStream::Read16() { wxUint16 i16; m_input->Read(&i16, 2); if (m_be_order) return wxUINT16_SWAP_ON_LE(i16); else return wxUINT16_SWAP_ON_BE(i16); } wxUint8 wxDataInputStream::Read8() { wxUint8 buf; m_input->Read(&buf, 1); return (wxUint8)buf; } double wxDataInputStream::ReadDouble() { #if wxUSE_APPLE_IEEE char buf[10]; m_input->Read(buf, 10); return ConvertFromIeeeExtended((const wxInt8 *)buf); #else return 0.0; #endif }
static int _read_iff(dmoz_file_t *file, song_sample_t *smp, const uint8_t *data, size_t length) { chunk_t chunk; size_t pos = 0; chunk_t vhdr, body, name, comm, auth, anno, ssnd; // butt if (!iff_chunk_read(&chunk, data, length, &pos)) return 0; if (chunk.id != ID_FORM) return 0; // jump "into" the FORM chunk // if (pos < length), there's more data after the FORM chunk -- but I don't care about this scenario pos = 0; length = MIN(length, chunk.size); data = chunk.data->FORM.data; /* the header is already byteswapped, but anything in 'chunk' will need to be swapped as needed because the structure is a const pointing into the data itself */ switch (bswapBE32(chunk.data->FORM.filetype)) { case ID_8SVX: // shut up, gcc ZEROIZE(vhdr); ZEROIZE(body); ZEROIZE(name); ZEROIZE(auth); ZEROIZE(anno); while (iff_chunk_read(&chunk, data, length, &pos)) { switch (chunk.id) { case ID_VHDR: vhdr = chunk; break; case ID_BODY: body = chunk; break; case ID_NAME: name = chunk; break; case ID_AUTH: auth = chunk; break; case ID_ANNO: anno = chunk; break; default: break; } } if (!(vhdr.id && body.id)) return 0; if (vhdr.data->VHDR.compression) { log_appendf(4, "error: compressed 8SVX files are unsupported"); return 0; } if (vhdr.data->VHDR.num_octaves != 1) { log_appendf(4, "warning: 8SVX file contains %d octaves", vhdr.data->VHDR.num_octaves); } if (file) { file->description = "8SVX sample"; file->type = TYPE_SAMPLE_PLAIN; } if (!name.id) name = auth; if (!name.id) name = anno; if (name.id) { if (file) { file->title = calloc(1, name.size + 1); memcpy(file->title, name.data->bytes, name.size); file->title[name.size] = '\0'; } if (smp) { int len = MIN(25, name.size); memcpy(smp->name, name.data->bytes, len); smp->name[len] = 0; } } if (smp) { smp->c5speed = bswapBE16(vhdr.data->VHDR.smp_per_sec); smp->length = body.size; csf_read_sample(smp, SF_BE | SF_PCMS | SF_8 | SF_M, body.data->bytes, body.size); smp->volume = 64*4; smp->global_volume = 64; // this is done kinda weird smp->loop_end = bswapBE32(vhdr.data->VHDR.smp_highoct_repeat); if (smp->loop_end) { smp->loop_start = bswapBE32(vhdr.data->VHDR.smp_highoct_1shot); smp->loop_end += smp->loop_start; if (smp->loop_start > smp->length) smp->loop_start = 0; if (smp->loop_end > smp->length) smp->loop_end = smp->length; if (smp->loop_start + 2 < smp->loop_end) smp->flags |= CHN_LOOP; } // TODO vhdr.data->VHDR.volume ? } return 1; case ID_AIFF: ZEROIZE(comm); ZEROIZE(ssnd); ZEROIZE(name); ZEROIZE(auth); ZEROIZE(anno); while (iff_chunk_read(&chunk, data, length, &pos)) { switch (chunk.id) { case ID_COMM: comm = chunk; break; case ID_SSND: ssnd = chunk; break; case ID_NAME: name = chunk; break; default: break; } } if (!(comm.id && ssnd.id)) return 0; if (file) { file->description = "Audio IFF sample"; file->type = TYPE_SAMPLE_PLAIN; } if (!name.id) name = auth; if (!name.id) name = anno; if (name.id) { if (file) { file->title = calloc(1, name.size + 1); memcpy(file->title, name.data->bytes, name.size); file->title[name.size] = '\0'; } if (smp) { int len = MIN(25, name.size); memcpy(smp->name, name.data->bytes, len); smp->name[len] = 0; } } /* TODO loop points */ if (smp) { uint32_t flags = SF_BE | SF_PCMS; switch (bswapBE16(comm.data->COMM.num_channels)) { default: log_appendf(4, "warning: multichannel AIFF is unsupported"); case 1: flags |= SF_M; break; case 2: flags |= SF_SI; break; } switch ((bswapBE16(comm.data->COMM.sample_size) + 7) & ~7) { default: log_appendf(4, "warning: AIFF has unsupported bit-width"); case 8: flags |= SF_8; break; case 16: flags |= SF_16; break; } // TODO: data checking; make sure sample count and byte size agree // (and if not, cut to shorter of the two) smp->c5speed = ConvertFromIeeeExtended(comm.data->COMM.sample_rate); smp->length = bswapBE32(comm.data->COMM.num_frames); smp->volume = 64*4; smp->global_volume = 64; // the audio data starts 8 bytes into the chunk // (don't care about the block alignment stuff) csf_read_sample(smp, flags, ssnd.data->bytes + 8, ssnd.size - 8); } return 1; } return 0; }