float CFile::ReadFloat (void) { float f; Read (&f, sizeof (f), 1) ; //Error ("Error reading float in CFile::ReadFloat ()"); return INTEL_FLOAT (f); }
float CFReadFloat (CFILE *file) { float f; CFCriticalError (CFRead (&f, sizeof (f), 1, file) != 1); //Error ("Error reading float in CFReadFloat ()"); return INTEL_FLOAT (f); }
float CFReadFloat (CFILE *file) { float f; if (CFRead (&f, sizeof (f), 1, file) != 1) return (float) nCFileError; //Error ("Error reading float in CFReadFloat()"); return INTEL_FLOAT (f); }
int CFile::WriteFloat (float f) { f = INTEL_FLOAT (f); return Write (&f, sizeof (f), 1); }
int CFWriteFloat (float f, CFILE *file) { f = INTEL_FLOAT (f); return CFWrite (&f, sizeof (f), 1, file); }
// Read // // Returns number of bytes actually read. // // Returns -1 if there is nothing more to be read. This function can return 0, since // sometimes the amount of bytes requested is too small for the ACM decompression to // locate a suitable block int WaveFile::Read(ubyte *pbDest, uint cbSize, int service) { void *dest_buf=NULL, *uncompressed_wave_data; int rc, uncompressed_bytes_written, section, last_section = -1, byte_order = 0; uint src_bytes_used, convert_len, num_bytes_desired=0, num_bytes_read; // nprintf(("Alan","Reqeusted: %d\n", cbSize)); #if BYTE_ORDER == BIG_ENDIAN byte_order = 1; #endif if ( service ) { uncompressed_wave_data = Wavedata_service_buffer; } else { uncompressed_wave_data = Wavedata_load_buffer; } switch ( m_wave_format ) { case WAVE_FORMAT_PCM: num_bytes_desired = cbSize; dest_buf = pbDest; break; case WAVE_FORMAT_ADPCM: { if ( !m_hStream_open ) { if ( !ACM_stream_open(m_pwfmt_original, &m_wfxDest, (void**)&m_hStream, m_bits_per_sample_uncompressed) ) { m_hStream_open = 1; } else { Int3(); } } num_bytes_desired = cbSize; if ( service ) { dest_buf = Compressed_service_buffer; } else { dest_buf = Compressed_buffer; } if ( num_bytes_desired <= 0 ) { num_bytes_desired = 0; // nprintf(("Alan","No bytes required for ADPCM time interval\n")); } else { num_bytes_desired = ACM_query_source_size((void*)m_hStream, cbSize); // nprintf(("Alan","Num bytes desired: %d\n", num_bytes_desired)); } break; } case OGG_FORMAT_VORBIS: num_bytes_desired = cbSize; dest_buf = pbDest; break; case WAVE_FORMAT_IEEE_FLOAT: { num_bytes_desired = cbSize; if (m_wfmt.wBitsPerSample == 32) { dest_buf = pbDest; } else { if (service) { dest_buf = Compressed_service_buffer; } else { dest_buf = Compressed_buffer; } } break; } default: nprintf(("SOUND", "SOUND => Not supporting %d format for playing wave files\n", m_wave_format)); Int3(); break; } // end switch num_bytes_read = 0; convert_len = 0; src_bytes_used = 0; // read data from disk if ( m_data_bytes_left <= 0 ) { num_bytes_read = 0; uncompressed_bytes_written = 0; return -1; } if ( (m_data_bytes_left > 0) && (num_bytes_desired > 0) ) { int actual_read = 0; if ( num_bytes_desired <= (uint)m_data_bytes_left ) { num_bytes_read = num_bytes_desired; } else { num_bytes_read = m_data_bytes_left; } // OGG reading is special if ( m_wave_format == OGG_FORMAT_VORBIS ) { int sign = (m_wfmt.wBitsPerSample == 8) ? 0 : 1; int sample_size = sizeof(float) * m_wfmt.nChannels; while ( !m_abort_next_read && ((uint)actual_read < num_bytes_read)) { float **pcm = NULL; if (m_wfmt.wBitsPerSample == 32) { rc = ov_read_float(&m_snd_info.vorbis_file, &pcm, (num_bytes_read - actual_read) / sample_size, §ion); } else { rc = ov_read(&m_snd_info.vorbis_file, (char *)dest_buf + actual_read, num_bytes_read - actual_read, byte_order, m_wfmt.wBitsPerSample / 8, sign, §ion); } // fail if the bitstream changes, shouldn't get this far if that's the case though if ((last_section != -1) && (last_section != section)) { mprintf(("AUDIOSTR => OGG reading error: We don't handle bitstream changes!\n")); goto READ_ERROR; } if ( rc > 0 ) { if (m_wfmt.wBitsPerSample == 32) { float *out_p = (float*)((ubyte*)dest_buf + actual_read); for (int i = 0; i < rc; i++) { for (int j = 0; j < m_wfmt.nChannels; j++) { *out_p++ = pcm[j][i]; } } actual_read += (rc * m_wfmt.nBlockAlign); } else { actual_read += rc; } last_section = section; } else if ( rc == 0 ) { break; } else if ( rc < 0 ) { if ( dbg_print_ogg_error(m_wFilename, rc) ) { // must be a fatal error goto READ_ERROR; } else { // not fatal, just continue on break; } } } } // IEEE FLOAT is special too, downsampling can give short buffers else if (m_wave_format == WAVE_FORMAT_IEEE_FLOAT) { while ( !m_abort_next_read && ((uint)actual_read < num_bytes_read) ) { rc = mmioRead(m_snd_info.cfp, (char *)dest_buf, num_bytes_read); if (rc <= 0) { break; } #if BYTE_ORDER == BIG_ENDIAN // need to byte-swap before any later conversions float *swap_tmp; for (int i = 0; i < rc; i += sizeof(float)) { swap_tmp = (float *)((ubyte*)dest_buf + i); *swap_tmp = INTEL_FLOAT(swap_tmp); } #endif if (m_wfmt.wBitsPerSample == 32) { actual_read = rc; } else if (m_wfmt.wBitsPerSample == 16) { float *in_p = (float*)dest_buf; short *out_p = (short*)((ubyte*)uncompressed_wave_data + actual_read); int end = rc / sizeof(float); for (int i = 0; i < end; i++) { int i_val = (int)(in_p[i] * 32767.0f + 0.5f); CLAMP(i_val, -32768, 32767); *out_p++ = (short)i_val; } actual_read += (rc >> 1); } else { Assert( m_wfmt.wBitsPerSample == 8 ); float *in_p = (float*)dest_buf; ubyte *out_p = (ubyte*)((ubyte*)uncompressed_wave_data + actual_read); int end = num_bytes_read / sizeof(float); for (int i = 0; i < end; i++) { int i_val = (int)(in_p[i] * 127.0f + 0.5f) + 128; CLAMP(i_val, 0, 255); *out_p++ = (ubyte)i_val; } actual_read += (rc >> 2); } }