void campaign_editor::OnLoad() { char buf[512]; size_t size, offset; if (Cur_campaign_mission < 0){ return; } if (Campaign_modified){ if (Campaign_wnd->save_modified()){ return; } } cf_find_file_location(Campaign.missions[Cur_campaign_mission].name, CF_TYPE_MISSIONS, 512, buf, &size, &offset, false); FREDDoc_ptr->SetPathName(buf); Campaign_wnd->DestroyWindow(); // if (FREDDoc_ptr->OnOpenDocument(Campaign.missions[Cur_campaign_mission].name)) { // Bypass_clear_mission = 1; // Campaign_wnd->DestroyWindow(); // // } else { // MessageBox("Failed to load mission!", "Error"); // } }
void debriefing_editor_dlg::OnPlay() { char path[MAX_PATH_LEN + 1]; GetDlgItem(IDC_VOICE)->GetWindowText(m_voice); int size, offset; cf_find_file_location((char *) (LPCSTR) m_voice, CF_TYPE_ANY, m_voice.GetLength(), path, &size, &offset ); PlaySound(path, NULL, SND_ASYNC | SND_FILENAME); }
void event_editor::OnPlay() { char path[MAX_PATH_LEN + 1]; GetDlgItem(IDC_WAVE_FILENAME)->GetWindowText(m_wave_filename); int size, offset; cf_find_file_location((char *)(LPCSTR)m_wave_filename, CF_TYPE_ANY, path, &size, &offset ); PlaySound(path, NULL, SND_ASYNC | SND_FILENAME); }
// Open BOOL WaveFile::Open (LPSTR pszFilename) { int done = FALSE; WORD cbExtra = 0; BOOL fRtn = SUCCESS; // assume success PCMWAVEFORMAT pcmwf; char fullpath[_MAX_PATH]; m_total_uncompressed_bytes_read = 0; m_max_uncompressed_bytes_to_read = AS_HIGHEST_MAX; int FileSize, FileOffset; if ( !cf_find_file_location(pszFilename, CF_TYPE_ANY, fullpath, &FileSize, &FileOffset )) { goto OPEN_ERROR; } cfp = mmioOpen(fullpath, NULL, MMIO_ALLOCBUF | MMIO_READ); if ( cfp == NULL ) { goto OPEN_ERROR; } // Skip the "RIFF" tag and file size (8 bytes) // Skip the "WAVE" tag (4 bytes) mmioSeek( cfp, 12+FileOffset, SEEK_SET ); // Now read RIFF tags until the end of file uint tag, size, next_chunk; while(done == FALSE) { if ( mmioRead(cfp, (char *)&tag, sizeof(uint)) != sizeof(uint) ) break; if ( mmioRead(cfp, (char *)&size, sizeof(uint)) != sizeof(uint) ) break; next_chunk = mmioSeek( cfp, 0, SEEK_CUR ); next_chunk += size; switch( tag ) { case 0x20746d66: // The 'fmt ' tag mmioRead( cfp, (char *)&pcmwf, sizeof(PCMWAVEFORMAT) ); if ( pcmwf.wf.wFormatTag != WAVE_FORMAT_PCM ) { mmioRead( cfp, (char *)&cbExtra, sizeof(short) ); } // Allocate memory for WAVEFORMATEX structure + extra bytes if ( (m_pwfmt_original = (WAVEFORMATEX *) malloc ( sizeof(WAVEFORMATEX)+cbExtra )) != NULL ){ Assert(m_pwfmt_original != NULL); // Copy bytes from temporary format structure memcpy (m_pwfmt_original, &pcmwf, sizeof(pcmwf)); m_pwfmt_original->cbSize = cbExtra; // Read those extra bytes, append to WAVEFORMATEX structure if (cbExtra != 0) { mmioRead( cfp, (char *)((ubyte *)(m_pwfmt_original) + sizeof(WAVEFORMATEX)), cbExtra ); } } else { Int3(); // malloc failed goto OPEN_ERROR; } break; case 0x61746164: // the 'data' tag m_nDataSize = size; // This is size of data chunk. Compressed if ADPCM. m_data_bytes_left = size; m_data_offset = mmioSeek( cfp, 0, SEEK_CUR); done = TRUE; break; default: // unknown, skip it break; } // end switch mmioSeek( cfp, next_chunk, SEEK_SET ); } // At this stage, examine source format, and set up WAVEFORATEX structure for DirectSound. // Since DirectSound only supports PCM, force this structure to be PCM compliant. We will // need to convert data on the fly later if our souce is not PCM switch ( m_pwfmt_original->wFormatTag ) { case WAVE_FORMAT_PCM: m_wave_format = WAVE_FORMAT_PCM; m_wfmt.wBitsPerSample = m_pwfmt_original->wBitsPerSample; break; case WAVE_FORMAT_ADPCM: m_wave_format = WAVE_FORMAT_ADPCM; m_wfmt.wBitsPerSample = 16; break; default: nprintf(("SOUND", "SOUND => Not supporting %d format for playing wave files\n")); //Int3(); goto OPEN_ERROR; break; } // end switch // Set up the WAVEFORMATEX structure to have the right PCM characteristics m_wfmt.wFormatTag = WAVE_FORMAT_PCM; m_wfmt.nChannels = m_pwfmt_original->nChannels; m_wfmt.nSamplesPerSec = m_pwfmt_original->nSamplesPerSec; m_wfmt.cbSize = 0; m_wfmt.nBlockAlign = (unsigned short)(( m_wfmt.nChannels * m_wfmt.wBitsPerSample ) / 8); m_wfmt.nAvgBytesPerSec = m_wfmt.nBlockAlign * m_wfmt.nSamplesPerSec; // Init some member data from format chunk m_nBlockAlign = m_pwfmt_original->nBlockAlign; m_nUncompressedAvgDataRate = m_wfmt.nAvgBytesPerSec; // Cue for streaming Cue (); // Successful open goto OPEN_DONE; OPEN_ERROR: // Handle all errors here nprintf(("SOUND","SOUND ==> Could not open wave file %s for streaming\n",pszFilename)); fRtn = FAILURE; if (cfp != NULL) { // Close file mmioClose( cfp, 0 ); cfp = NULL; } if (m_pwfmt_original) { free(m_pwfmt_original); m_pwfmt_original = NULL; } OPEN_DONE: return (fRtn); }
// Open bool WaveFile::Open(char *pszFilename, bool keep_ext) { int rc = -1; WORD cbExtra = 0; bool fRtn = true; // assume success PCMWAVEFORMAT pcmwf; int FileSize, FileOffset; char fullpath[MAX_PATH]; char filename[MAX_FILENAME_LEN]; const int NUM_EXT = 2; const char *audio_ext[NUM_EXT] = { ".ogg", ".wav" }; m_total_uncompressed_bytes_read = 0; m_max_uncompressed_bytes_to_read = AS_HIGHEST_MAX; // NOTE: we assume that the extension has already been stripped off if it was supposed to be!! strcpy_s( filename, pszFilename ); // if we are supposed to load the file as passed... if (keep_ext) { for (int i = 0; i < NUM_EXT; i++) { if ( stristr(pszFilename, audio_ext[i]) ) { rc = i; break; } } // not a supported extension format ... somebody screwed up their tbls :) if (rc < 0) goto OPEN_ERROR; cf_find_file_location(pszFilename, CF_TYPE_ANY, sizeof(fullpath) - 1, fullpath, &FileSize, &FileOffset); } // ... otherwise we just find the best match else { rc = cf_find_file_location_ext(filename, NUM_EXT, audio_ext, CF_TYPE_ANY, sizeof(fullpath) - 1, fullpath, &FileSize, &FileOffset); } if (rc < 0) { goto OPEN_ERROR; } else { // set proper filename for later use (assumes that it doesn't already have an extension) strcat_s( filename, audio_ext[rc] ); } m_snd_info.cfp = mmioOpen( fullpath, NULL, MMIO_ALLOCBUF | MMIO_READ ); if (m_snd_info.cfp == NULL) goto OPEN_ERROR; m_snd_info.true_offset = FileOffset; m_snd_info.size = FileSize; // if in a VP then position the stream at the start of the file if (FileOffset > 0) mmioSeek( m_snd_info.cfp, FileOffset, SEEK_SET ); // if Ogg Vorbis... if (rc == 0) { if ( ov_open_callbacks(&m_snd_info, &m_snd_info.vorbis_file, NULL, 0, mmio_callbacks) == 0 ) { // got an Ogg Vorbis, so lets read the info in ov_info(&m_snd_info.vorbis_file, -1); // we only support one logical bitstream if ( ov_streams(&m_snd_info.vorbis_file) != 1 ) { mprintf(("AUDIOSTR => OGG reading error: We don't handle bitstream changes!\n")); goto OPEN_ERROR; } m_wave_format = OGG_FORMAT_VORBIS; m_wfmt.wFormatTag = WAVE_FORMAT_PCM; m_wfmt.nChannels = (WORD) m_snd_info.vorbis_file.vi->channels; m_wfmt.nSamplesPerSec = m_snd_info.vorbis_file.vi->rate; switch (Ds_sound_quality) { case DS_SQ_HIGH: m_wfmt.wBitsPerSample = Ds_float_supported ? 32 : 16; break; case DS_SQ_MEDIUM: m_wfmt.wBitsPerSample = 16; break; default: m_wfmt.wBitsPerSample = 8; break; } m_wfmt.cbSize = 0; m_wfmt.nBlockAlign = (ushort)(( m_wfmt.nChannels * m_wfmt.wBitsPerSample ) / 8); m_wfmt.nAvgBytesPerSec = m_wfmt.nSamplesPerSec * m_wfmt.nBlockAlign; m_nBlockAlign = m_wfmt.nBlockAlign; m_nUncompressedAvgDataRate = m_wfmt.nAvgBytesPerSec; // location of start of file in VP m_data_offset = 0; m_nDataSize = m_data_bytes_left = ((int)ov_pcm_total(&m_snd_info.vorbis_file, -1) * m_wfmt.nBlockAlign); } else { mprintf(("AUDIOSTR => OGG reading error: Not a valid Vorbis file!\n")); } } // if Wave... else if (rc == 1) { bool done = false; // Skip the "RIFF" tag and file size (8 bytes) // Skip the "WAVE" tag (4 bytes) mmioSeek( m_snd_info.cfp, 12+FileOffset, SEEK_SET ); // Now read RIFF tags until the end of file uint tag, size, next_chunk; while ( !done ) { if ( !audiostr_read_uint(m_snd_info.cfp, &tag) ) break; if ( !audiostr_read_uint(m_snd_info.cfp, &size) ) break; next_chunk = mmioSeek(m_snd_info.cfp, 0, SEEK_CUR ); next_chunk += size; switch (tag) { case 0x20746d66: // The 'fmt ' tag { audiostr_read_word(m_snd_info.cfp, &pcmwf.wf.wFormatTag); audiostr_read_word(m_snd_info.cfp, &pcmwf.wf.nChannels); audiostr_read_dword(m_snd_info.cfp, &pcmwf.wf.nSamplesPerSec); audiostr_read_dword(m_snd_info.cfp, &pcmwf.wf.nAvgBytesPerSec); audiostr_read_word(m_snd_info.cfp, &pcmwf.wf.nBlockAlign); audiostr_read_word(m_snd_info.cfp, &pcmwf.wBitsPerSample); if (pcmwf.wf.wFormatTag == WAVE_FORMAT_ADPCM) audiostr_read_word(m_snd_info.cfp, &cbExtra); // Allocate memory for WAVEFORMATEX structure + extra bytes if ( (m_pwfmt_original = (WAVEFORMATEX *) vm_malloc(sizeof(WAVEFORMATEX)+cbExtra)) != NULL ) { Assert(m_pwfmt_original != NULL); // Copy bytes from temporary format structure memcpy (m_pwfmt_original, &pcmwf, sizeof(pcmwf)); m_pwfmt_original->cbSize = cbExtra; // Read those extra bytes, append to WAVEFORMATEX structure if (cbExtra != 0) mmioRead( m_snd_info.cfp, ((char *)(m_pwfmt_original) + sizeof(WAVEFORMATEX)), cbExtra ); } else { Int3(); // malloc failed goto OPEN_ERROR; } break; } case 0x61746164: // the 'data' tag { m_nDataSize = size; // This is size of data chunk. Compressed if ADPCM. m_data_bytes_left = size; m_data_offset = mmioSeek( m_snd_info.cfp, 0, SEEK_CUR ); done = true; break; } default: // unknown, skip it break; } // end switch mmioSeek( m_snd_info.cfp, next_chunk, SEEK_SET ); } // make sure that we did good if ( !done || (m_pwfmt_original == NULL) ) goto OPEN_ERROR; // At this stage, examine source format, and set up WAVEFORATEX structure for DirectSound. // Since DirectSound only supports PCM, force this structure to be PCM compliant. We will // need to convert data on the fly later if our souce is not PCM switch (m_pwfmt_original->wFormatTag) { case WAVE_FORMAT_PCM: m_wave_format = WAVE_FORMAT_PCM; m_wfmt.wBitsPerSample = m_pwfmt_original->wBitsPerSample; break; case WAVE_FORMAT_ADPCM: m_wave_format = WAVE_FORMAT_ADPCM; m_wfmt.wBitsPerSample = (Ds_sound_quality) ? 16: 8; m_bits_per_sample_uncompressed = m_wfmt.wBitsPerSample; break; case WAVE_FORMAT_IEEE_FLOAT: { m_wave_format = WAVE_FORMAT_IEEE_FLOAT; switch (Ds_sound_quality) { case DS_SQ_HIGH: m_wfmt.wBitsPerSample = (Ds_float_supported) ? 32 : 16; break; case DS_SQ_MEDIUM: m_wfmt.wBitsPerSample = 16; break; default: m_wfmt.wBitsPerSample = 8; break; } break; } default: nprintf(("SOUND", "SOUND => Not supporting %d format for playing wave files\n", m_pwfmt_original->wFormatTag)); goto OPEN_ERROR; break; } // end switch // Set up the WAVEFORMATEX structure to have the right PCM characteristics m_wfmt.wFormatTag = WAVE_FORMAT_PCM; m_wfmt.nChannels = m_pwfmt_original->nChannels; m_wfmt.nSamplesPerSec = m_pwfmt_original->nSamplesPerSec; m_wfmt.cbSize = 0; m_wfmt.nBlockAlign = (ushort)(( m_wfmt.nChannels * m_wfmt.wBitsPerSample ) / 8); m_wfmt.nAvgBytesPerSec = m_wfmt.nBlockAlign * m_wfmt.nSamplesPerSec; // Init some member data from format chunk m_nBlockAlign = m_pwfmt_original->nBlockAlign; m_nUncompressedAvgDataRate = m_wfmt.nAvgBytesPerSec; Assert( (m_wfmt.nChannels == 1) || (m_wfmt.nChannels == 2) ); } // something unkown??? else { Int3(); } m_al_format = openal_get_format(m_wfmt.wBitsPerSample, m_wfmt.nChannels); if (m_al_format != AL_INVALID_VALUE) { // Cue for streaming Cue(); goto OPEN_DONE; } OPEN_ERROR: // Handle all errors here nprintf(("SOUND","SOUND ==> Could not open wave file %s for streaming\n", filename)); fRtn = false; if (m_snd_info.cfp != NULL) { // Close file mmioClose( m_snd_info.cfp, 0 ); m_snd_info.cfp = NULL; m_snd_info.true_offset = 0; m_snd_info.size = 0; } if (m_pwfmt_original) { vm_free(m_pwfmt_original); m_pwfmt_original = NULL; } OPEN_DONE: strncpy(m_wFilename, filename, MAX_FILENAME_LEN-1); if (fRtn) nprintf(("SOUND", "AUDIOSTR => Successfully opened: %s\n", filename)); return (fRtn); }
// Open BOOL WaveFile::Open(LPSTR pszFilename) { int done = FALSE, rc = 0; WORD cbExtra = 0; BOOL fRtn = SUCCESS; // assume success PCMWAVEFORMAT pcmwf; char fullpath[_MAX_PATH]; m_total_uncompressed_bytes_read = 0; m_max_uncompressed_bytes_to_read = AS_HIGHEST_MAX; int FileSize, FileOffset; if (!cf_find_file_location(pszFilename, CF_TYPE_ANY, sizeof(fullpath) - 1, fullpath, &FileSize, &FileOffset)) { goto OPEN_ERROR; } m_snd_info.cfp = mmioOpen(fullpath, NULL, MMIO_ALLOCBUF | MMIO_READ); if (m_snd_info.cfp == NULL) { goto OPEN_ERROR; } m_snd_info.true_offset = FileOffset; m_snd_info.size = FileSize; // if in a VP then position the stream at the start of the file if (FileOffset > 0) { mmioSeek(m_snd_info.cfp, FileOffset, SEEK_SET); } // first check for an OGG if ((rc = ov_open_callbacks(&m_snd_info, &m_snd_info.vorbis_file, NULL, 0, mmio_callbacks)) == 0) { // got an OGG so lets read the info in ov_info(&m_snd_info.vorbis_file, -1); // we only support one logical bitstream if (ov_streams(&m_snd_info.vorbis_file) != 1) { mprintf(("AUDIOSTR => OGG reading error: We don't handle bitstream changes!\n")); goto OPEN_ERROR; } m_wave_format = OGG_FORMAT_VORBIS; m_wfmt.wFormatTag = WAVE_FORMAT_PCM; m_wfmt.nChannels = (WORD)m_snd_info.vorbis_file.vi->channels; m_wfmt.nSamplesPerSec = m_snd_info.vorbis_file.vi->rate; m_wfmt.cbSize = 0; if (UserSampleBits == 16 || UserSampleBits == 8) m_wfmt.wBitsPerSample = UserSampleBits; //Decode at whatever the user specifies; only 16 and 8 are supported. else if (UserSampleBits > 16) m_wfmt.wBitsPerSample = 16; else m_wfmt.wBitsPerSample = 8; m_wfmt.nBlockAlign = (ushort)((m_wfmt.nChannels * m_wfmt.wBitsPerSample) / 8); m_wfmt.nAvgBytesPerSec = m_wfmt.nSamplesPerSec * m_wfmt.nBlockAlign; m_nBlockAlign = m_wfmt.nBlockAlign; m_nUncompressedAvgDataRate = m_wfmt.nAvgBytesPerSec; // location of start of file in VP m_data_offset = 0; m_nDataSize = m_data_bytes_left = ((int)ov_pcm_total(&m_snd_info.vorbis_file, -1) * m_wfmt.nBlockAlign); // Cue for streaming Cue(); // successful open goto OPEN_DONE; } // not an OGG so assume that it's WAVE else { // extra check, if it's not ogg then but if the error was a bad ogg then bail if (rc && (rc != OV_ENOTVORBIS)) goto OPEN_ERROR; // Skip the "RIFF" tag and file size (8 bytes) // Skip the "WAVE" tag (4 bytes) mmioSeek(m_snd_info.cfp, 12 + FileOffset, SEEK_SET); /*char buf[4]; mmioRead(m_snd_info.cfp, buf, sizeof(char)*4); if(strnicmp("RIFF", buf, 4)) goto OPEN_ERROR; //Skip file length mmioSeek( m_snd_info.cfp, 4, SEEK_CUR); mmioRead(m_snd_info.cfp, buf, sizeof(char)*4); if(strnicmp("WAVE", buf, 4)) goto OPEN_ERROR;*/ // Now read RIFF tags until the end of file uint tag, size, next_chunk; while (done == FALSE) { if (mmioRead(m_snd_info.cfp, (char*)&tag, sizeof(uint)) != sizeof(uint)) break; if (mmioRead(m_snd_info.cfp, (char*)&size, sizeof(uint)) != sizeof(uint)) break; next_chunk = mmioSeek(m_snd_info.cfp, 0, SEEK_CUR); next_chunk += size; switch (tag) { case 0x20746d66: // The 'fmt ' tag mmioRead(m_snd_info.cfp, (char*)&pcmwf, sizeof(PCMWAVEFORMAT)); if (pcmwf.wf.wFormatTag != WAVE_FORMAT_PCM) { mmioRead(m_snd_info.cfp, (char*)&cbExtra, sizeof(short)); } // Allocate memory for WAVEFORMATEX structure + extra bytes if ((m_pwfmt_original = (WAVEFORMATEX*)vm_malloc(sizeof(WAVEFORMATEX) + cbExtra)) != NULL) { Assert(m_pwfmt_original != NULL); // Copy bytes from temporary format structure memcpy(m_pwfmt_original, &pcmwf, sizeof(pcmwf)); m_pwfmt_original->cbSize = cbExtra; // Read those extra bytes, append to WAVEFORMATEX structure if (cbExtra != 0) { mmioRead(m_snd_info.cfp, (char*)((ubyte*)(m_pwfmt_original)+ sizeof(WAVEFORMATEX)), cbExtra); } } else { Int3(); // malloc failed goto OPEN_ERROR; } break; case 0x61746164: // the 'data' tag m_nDataSize = size; // This is size of data chunk. Compressed if ADPCM. m_data_bytes_left = size; m_data_offset = mmioSeek(m_snd_info.cfp, 0, SEEK_CUR); done = TRUE; break; default: // unknown, skip it break; } // end switch mmioSeek(m_snd_info.cfp, next_chunk, SEEK_SET); } // At this stage, examine source format, and set up WAVEFORATEX structure for DirectSound. // Since DirectSound only supports PCM, force this structure to be PCM compliant. We will // need to convert data on the fly later if our souce is not PCM switch (m_pwfmt_original->wFormatTag) { case WAVE_FORMAT_PCM: m_wave_format = WAVE_FORMAT_PCM; m_wfmt.wBitsPerSample = m_pwfmt_original->wBitsPerSample; break; case WAVE_FORMAT_ADPCM: m_wave_format = WAVE_FORMAT_ADPCM; if (UserSampleBits == 16 || UserSampleBits == 8) m_wfmt.wBitsPerSample = UserSampleBits; //Decode at whatever the user specified, if it's 16 or 8 else if (UserSampleBits > 16) m_wfmt.wBitsPerSample = 16; else m_wfmt.wBitsPerSample = 8; break; default: nprintf(("SOUND", "SOUND => Not supporting %d format for playing wave files\n", m_pwfmt_original->wFormatTag)); //Int3(); goto OPEN_ERROR; break; } // end switch // Set up the WAVEFORMATEX structure to have the right PCM characteristics m_wfmt.wFormatTag = WAVE_FORMAT_PCM; m_wfmt.nChannels = m_pwfmt_original->nChannels; m_wfmt.nSamplesPerSec = m_pwfmt_original->nSamplesPerSec; m_wfmt.cbSize = 0; m_wfmt.nBlockAlign = (unsigned short)((m_wfmt.nChannels * m_wfmt.wBitsPerSample) / 8); m_wfmt.nAvgBytesPerSec = m_wfmt.nBlockAlign * m_wfmt.nSamplesPerSec; // Init some member data from format chunk m_nBlockAlign = m_pwfmt_original->nBlockAlign; m_nUncompressedAvgDataRate = m_wfmt.nAvgBytesPerSec; // Cue for streaming Cue(); // Successful open goto OPEN_DONE; } OPEN_ERROR: // Handle all errors here nprintf(("SOUND", "SOUND ==> Could not open wave file %s for streaming\n", pszFilename)); fRtn = FAILURE; if (m_snd_info.cfp != NULL) { // Close file mmioClose(m_snd_info.cfp, 0); m_snd_info.cfp = NULL; m_snd_info.true_offset = 0; m_snd_info.size = 0; } if (m_pwfmt_original) { vm_free(m_pwfmt_original); m_pwfmt_original = NULL; } OPEN_DONE: return (fRtn); }