bool CSoundData::ReadFromDisk() { assert(m_assigned == true); LARGE_INTEGER start; QueryPerformanceCounter(&start); U32* s = (U32 *) AIL_file_read(m_filename, FILE_READ_WITH_SIZE); if (s == NULL) return false; S32 type = AIL_file_type(s + 1, s[0]); AILSOUNDINFO info; switch (type) { case AILFILETYPE_PCM_WAV: { m_data = s; m_size = *((S32 *) s); m_flag |= FLAG_DATA_SIZE; } break; case AILFILETYPE_ADPCM_WAV: // 3D 사운드는 decompress 해야 함. { AIL_WAV_info(s + 1, &info); AIL_decompress_ADPCM(&info, &m_data, &m_size); AIL_mem_free_lock(s); } break; case AILFILETYPE_MPEG_L3_AUDIO: { AIL_decompress_ASI(s + 1, *((S32 *) s), m_filename, &m_data, &m_size, 0); AIL_mem_free_lock(s); } break; default: assert(!"Unknown File Type"); AIL_mem_free_lock(s); return false; } return true; }
// // Destructor // Item::~Item() { ASSERT(curCacheSize - record->fastFind->Size() >= 0); curCacheSize -= record->fastFind->Size(); items.Unlink(this); AIL_mem_free_lock(data); }
void CSoundData::Destroy() { if (m_data) { AIL_mem_free_lock(m_data); m_data = NULL; } }
void MusicClose(void) { if (!has_midi) return; #ifdef M59_MSS AIL_release_sample_handle(hseqBackground); AIL_release_sample_handle(hseqImmediate); if (pMIDIImmediate) AIL_mem_free_lock(pMIDIImmediate); if (pMIDIBackground) AIL_mem_free_lock(pMIDIBackground); AIL_shutdown(); #else mciSendCommand(MCI_ALL_DEVICE_ID, MCI_CLOSE, 0, 0); #endif }
bool TForm_Main::OpenFile() { // STOP!!! c_Button_Stop->Click(); // Let go... if (m_hSample) { AIL_release_sample_handle(m_hSample); m_hSample = NULL; } // Get rid of the buffer if it's already been loaded if (m_pFileBuffer) { AIL_mem_free_lock(m_pFileBuffer); m_pFileBuffer = NULL; } // Remember the file name m_CurFile = c_Edit_FileName->Text; // Er, don't load if it doesn't exist... if (!FileExists(m_CurFile)) { m_CurFile = ""; return false; } m_hSample = AIL_allocate_sample_handle(m_hDigDriver); if (!m_hSample) { ChangeStatus("Error allocating sample handle"); m_CurFile = ""; return false; } char *pFileName = m_CurFile.c_str(); // Bind to the file m_pFileBuffer = AIL_file_read(pFileName, NULL); if (!m_pFileBuffer) { ChangeStatus("Error opening " + m_CurFile + " (" + IntToStr(AIL_file_error()) + ")"); m_CurFile = ""; return false; } // Get the file size m_iFileSize = AIL_file_size(pFileName); AIL_init_sample(m_hSample); AIL_set_named_sample_file(m_hSample, pFileName, m_pFileBuffer, m_iFileSize, 0); AIL_set_sample_loop_count(m_hSample, 0); AIL_set_sample_volume(m_hSample, 127); AIL_set_sample_processor(m_hSample, DP_FILTER, m_hCurFilter); ChangeStatus("Loaded " + m_CurFile); return true; }
/* * PlayMusicFile: Play given MIDI file and notify given window when done. * If background music had been playing, it picks up where it left off. * Returns 0 if successful, MCI error code otherwise. */ DWORD PlayMusicFile(HWND hWndNotify, char *fname) { if (!has_midi) return 0; #ifdef M59_MSS char *ext; // If a sequence was paused, resume it if (music_pos != 0) { UnpauseMusic(); return 0; } // free memory from previous background music if (pMIDIBackground) AIL_mem_free_lock(pMIDIBackground); // First try MP3 file ext = strstr( _strlwr( fname ), ".mid" ); if( ext != NULL ) strcpy( ext, ".mp3" ); // load the file pMIDIBackground = (BYTE *) AIL_file_read( fname, NULL ); if( !pMIDIBackground ) { // Next try xmi file ext = strstr(fname, ".mp3" ); if( ext != NULL ) strcpy( ext, ".xmi" ); pMIDIBackground = (BYTE *) AIL_file_read( fname, NULL ); if( !pMIDIBackground ) { debug(( "Failed to load music file %s.\n", fname )); return 0; } } // initialize the sequence if (!AIL_set_named_sample_file(hseqBackground, fname, pMIDIBackground, AIL_file_size(fname), 0 ) ) { debug(( "Failed to init music sequence %s.\n", fname )); return 0; } // set to loop indefinitely AIL_set_sample_loop_count( hseqBackground, 0 ); // Set volume float vol = ((float) config.music_volume) / CONFIG_MAX_VOLUME; AIL_set_sample_volume_levels(hseqBackground, vol, vol ); // start playing AIL_start_sample( hseqBackground ); debug(( "Playing music file %s.\n", fname )); playing_music = True; return 0; #else DWORD dwReturn; MCI_PLAY_PARMS mciPlayParms; char temp[81]; if ((dwReturn = OpenMidiFile(fname)) != 0) { debug(("OpenMidiFile error code = %d\n", dwReturn)); mciGetErrorString(dwReturn, temp, 80); debug((temp)); return dwReturn; } /* If already playing music, pick up where we left off */ if (music_pos != 0) UnpauseMusic(); /* * Begin playback. The window procedure function * for the parent window is notified with an * MM_MCINOTIFY message when playback is complete. * The window procedure then closes the device. */ mciPlayParms.dwCallback = (DWORD) hWndNotify; if (dwReturn = mciSendCommand(midi_element, MCI_PLAY, MCI_NOTIFY, (DWORD)(LPVOID) &mciPlayParms)) { mciGetErrorString(dwReturn, temp, 80); debug((temp)); mciSendCommand(midi_element, MCI_CLOSE, 0, 0); return dwReturn; } debug(("Playing music file, element = %d\n", midi_element)); ResetMusicVolume(); playing_music = True; return 0; #endif }
/* * PlayMidiFile: Play given MIDI file and notify given window when done. * Returns 0 if successful, MCI error code otherwise. */ DWORD PlayMidiFile(HWND hWndNotify, char *fname) { if (!has_midi) return 0; #ifdef M59_MSS // If a sample was playing, end it if( AIL_sample_status( hseqImmediate ) != SMP_DONE ) AIL_end_sample( hseqImmediate ); // free memory from previous MIDI file if( pMIDIImmediate ) AIL_mem_free_lock( pMIDIImmediate ); // First try MP3 file char *ext = strstr( _strlwr( fname ), ".mid" ); if( ext != NULL ) strcpy( ext, ".mp3" ); // load the file pMIDIImmediate = (BYTE *) AIL_file_read( fname, NULL ); if( !pMIDIImmediate ) { // Next try xmi file ext = strstr(fname, ".mp3" ); if( ext != NULL ) strcpy( ext, ".xmi" ); pMIDIImmediate = (BYTE *) AIL_file_read( fname, NULL ); if( !pMIDIImmediate ) { debug(( "Failed to load music file %s.\n", fname )); return 0; } } // Initialize the sample if (!AIL_set_named_sample_file(hseqImmediate, fname, pMIDIImmediate, AIL_file_size(fname), 0)) { debug(( "Failed to init music file.\n" )); return 0; } // Set volume float vol = ((float) config.music_volume) / CONFIG_MAX_VOLUME; AIL_set_sample_volume_levels(hseqImmediate, vol, vol ); // start playing AIL_start_sample(hseqImmediate); // Set end-of-sample callback so we can unpause // the background music when done playing. AIL_register_EOS_callback(hseqImmediate, MIDIDoneCallback); debug(( "Playing music file %s.\n", fname )); playing_midi = True; return 0; #else { DWORD dwReturn; MCI_PLAY_PARMS mciPlayParms; if ((dwReturn = OpenMidiFile(fname)) != 0) { return dwReturn; } /* * Begin playback. The window procedure function * for the parent window is notified with an * MM_MCINOTIFY message when playback is complete. * The window procedure then closes the device. */ mciPlayParms.dwCallback = (DWORD) hWndNotify; if (dwReturn = mciSendCommand(midi_element, MCI_PLAY, MCI_NOTIFY, (DWORD)(LPVOID) &mciPlayParms)) { mciSendCommand(midi_element, MCI_CLOSE, 0, 0); return dwReturn; } debug(("Playing MIDI file, element = %d\n", midi_element)); ResetMusicVolume(); playing_midi = True; return 0; } #endif }
void vrpn_Sound_Server_Miles::loadSound(char* filename, vrpn_SoundID id) /*loads a .wav file into memory. gives back id to reference the sound currently (7/28/99) this does not reuse space previously vacated in samples by unloading sounds*/ { if (provider != 0) { fprintf(stdout,"Loading sound: #%d %s\n", id, filename); ChangeSoundIdBox(0,id); //load into handle unsigned long *s; long type; H3DSAMPLE handle; void *sample_address; void *d; AILSOUNDINFO info; LastSoundId = id; //load the .wav file into memory s = (unsigned long *)AIL_file_read(filename, FILE_READ_WITH_SIZE); if (s==0) return; type=AIL_file_type(s+1,s[0]); switch (type) { case AILFILETYPE_PCM_WAV: sample_address = s+1; break; case AILFILETYPE_ADPCM_WAV: AIL_WAV_info(s+1,&info); AIL_decompress_ADPCM(&info,&d,0); AIL_mem_free_lock(s); sample_address = d; break; default: AIL_mem_free_lock(s); return; } //initialize handle handle = AIL_allocate_3D_sample_handle(provider); //tell handle where the .wav file is in memory AIL_set_3D_sample_file(handle, sample_address); //set defaults AIL_set_3D_sample_volume(handle, 100); //default volume is 100 on 0...127 inclusive AIL_set_3D_position(handle,0,0,0); AIL_set_3D_orientation(handle, 0,0,-1,0,1,0); AIL_set_3D_velocity(handle,0,0,-1,0); AIL_set_3D_sample_distances(handle, 200, 20); //load handle into samples addSample(handle, id); /*Sample level environment preferences (ie, EAX_ENVIRONMENT_BATHROOM) could be added in here as a passed-in parameter*/ } else fprintf(stderr,"No provider has been set prior to LoadSound\n"); }
// // Request // // Request sound effect data from the cache // Item * Request(Record *record) { ASSERT(Initialized()); ASSERT(record); ASSERT(record->valid); ASSERT(record->fastFind); // Is this effect too large for the cache if (record->fastFind->Size() > maxCacheSize) { LOG_WARN(("File '%s' is too large for the cache", record->Name())); record->valid = FALSE; return (NULL); } // Record if first time being played if (!record->freq) { actuallyUsed++; } // Increment the frequency of this effect record->freq++; // Step through each cache item for (NList<Item>::Iterator i(&items); *i; i++) { // Is this the one we're after if ((*i)->record == record) { cacheHits++; return (*i); } } // Record a cache miss cacheMisses++; // Make space for this new effect (will also shrink cache if max size decreased) while (items.GetCount() && (maxCacheSize - curCacheSize < record->fastFind->Size())) { // Try and remove a sound effect out of the cache if (!FlushItem()) { // Unable to remove any effects return (NULL); } } ASSERT(maxCacheSize - curCacheSize >= record->fastFind->Size()); // Open the file FileSys::DataFile *dFile = FileSys::Open(record->fastFind); // Should be found since already checked if (!dFile) { LOG_WARN(("File '%s' has vanished!", record->Name())); record->valid = FALSE; return (NULL); } // Let's just be extra careful, someone might swap data files on a server if (dFile->Size() != record->fastFind->Size()) { LOG_WARN(("File '%s' has changed size!", record->Name())); record->valid = FALSE; return (NULL); } // Allocate memory for file data void *data = AIL_mem_alloc_lock(record->fastFind->Size()); if (!data) { return (NULL); } // Read the data from disk if (dFile->Read(data, record->fastFind->Size()) != record->fastFind->Size()) { // Free the memory we just allocated AIL_mem_free_lock(data); // Close the file FileSys::Close(dFile); // We won't try this one again record->valid = FALSE; LOG_WARN(("Error reading effect '%s' into cache", record->Name())); return (NULL); } // Close the file FileSys::Close(dFile); // Return a new cache item return (new Item(record, data)); }