void thread_load(const std::string& file) { #if !TARGET_IPHONE_SIMULATOR && !TARGET_OS_IPHONE #if defined(__ANDROID__) Mix_Chunk* chunk = Mix_LoadWAV_RW(sys::read_sdl_rw_from_asset(module::map_file("sounds/" + file).c_str()),1); #else Mix_Chunk* chunk = Mix_LoadWAV(module::map_file("sounds/" + file).c_str()); #endif { threading::lock l(cache_mutex); threaded_cache[file] = chunk; } #else std::string wav_file = file; wav_file.replace(wav_file.length()-3, wav_file.length(), "wav"); sound s("sounds_wav/" + wav_file); { threading::lock l(cache_mutex); threaded_cache[file] = s; } #endif }
void playSoundWav(uint8_t channel, const uint8_t *data, uint8_t volume, int loops = 0) { uint32_t size = READ_LE_UINT32(data + 4) + 8; // check format for QuickLoad bool canQuickLoad = (AUDIO_S16SYS == AUDIO_S16LSB); if (canQuickLoad) { if (memcmp(data + 8, "WAVEfmt ", 8) == 0 && READ_LE_UINT32(data + 16) == 16) { const uint8_t *fmt = data + 20; const int format = READ_LE_UINT16(fmt); const int channels = READ_LE_UINT16(fmt + 2); const int rate = READ_LE_UINT32(fmt + 4); const int bits = READ_LE_UINT16(fmt + 14); debug(DBG_SND, "wave format %d channels %d rate %d bits %d", format, channels, rate, bits); canQuickLoad = (format == 1 && channels == 2 && rate == kMixFreq && bits == 16); } } if (canQuickLoad) { Mix_Chunk *chunk = Mix_QuickLoad_WAV(const_cast<uint8_t *>(data)); playSound(channel, volume, chunk, loops); } else { SDL_RWops *rw = SDL_RWFromConstMem(data, size); Mix_Chunk *chunk = Mix_LoadWAV_RW(rw, 1); playSound(channel, volume, chunk, loops); } }
// Allocate an SDL chunk and cache it. Mix_Chunk *CSoundHandler::GetSoundChunk(soundBase::soundID soundID) { // Find its name auto fname = sounds[soundID]; if (fname.empty()) return nullptr; // Load and insert try { auto data = CResourceHandler::get()->load(ResourceID(std::string("SOUNDS/") + fname, EResType::SOUND))->readAll(); SDL_RWops *ops = SDL_RWFromMem(data.first.release(), data.second); Mix_Chunk *chunk; chunk = Mix_LoadWAV_RW(ops, 1); // will free ops soundChunks.insert(std::pair<soundBase::soundID, Mix_Chunk *>(soundID, chunk)); return chunk; } catch(std::exception &e) { logGlobal->warnStream() << "Cannot get sound " << soundID << " chunk: " << e.what(); return nullptr; } }
int AUDs_LoadWaveFile( int num ) { if (SoundDisabled) return AUD_RETURN_SUCCESS; ASSERT( ( num >= 0 ) && ( num < MAX_SAMPLES ) ); if ( !SoundAvailable ) return AUD_RETURN_SUCCESS; // read the sample and assign the list entry sample_s* pSample = SND_ReadSample( &SampleInfo[ num ] ); SND_ConvertRate( pSample, aud_sample_quality ); // because SDL_RWFromFP doesn't work cross platform, we need // to get a little bit kinky // set up a buffer for the file char *tmp_sample_buffer = NULL; size_t tmp_sample_size = 0; // get the size of this sample tmp_sample_size = SYS_GetFileLength(SampleInfo[num].file); if (tmp_sample_size <= 0) { return FALSE; // return on error } // alloc space for the buffer tmp_sample_buffer = (char *)ALLOCMEM(tmp_sample_size + 1); // open the sample file FILE *wave = SYS_fopen(SampleInfo[num].file, "rb"); if (wave == NULL) return FALSE; // read the file into the temp_sample_buffer int read_rc = SYS_fread((void *)tmp_sample_buffer, 1, tmp_sample_size, wave); if (read_rc <= 0) { MSGOUT("ERROR: Error reading sample %s.", SampleInfo[num].file); return FALSE; } SDL_RWops *waverwops = SDL_RWFromMem((void *)tmp_sample_buffer, tmp_sample_size); snd_chunks[num] = Mix_LoadWAV_RW(waverwops, 0); SYS_fclose(wave); FREEMEM( tmp_sample_buffer ); FREEMEM( pSample->samplebuffer ); pSample->samplebuffer = NULL; #ifdef SHOW_SAMPLE_LOADING_INFO MSGOUT("loaded %-12s to slot %03d: rate: %05d, numch: %02d, type: %s, bytes: %07d", SampleInfo[ num ].file, num, pSample->samplerate, pSample->numchannels, ( pSample->samplesize == 8 ) ? "8" : "16" , pSample->samplebytes ); #endif // SHOW_SAMPLE_LOADING_INFO // store the slot submitted to the play functions SampleInfo[ num ].samplepointer = (char *)num; //CAVEAT: for now we store the number of refframes in size SampleInfo[ num ].size = FLOAT2INT( (float) FRAME_MEASURE_TIMEBASE * (float) pSample->samplebytes / (float) ( pSample->samplerate * pSample->alignment ) + 0.5f ); FREEMEM( pSample ); return AUD_RETURN_SUCCESS; }
bool Resource_Manager :: init_1() { //DEFAULT - The following are always loaded img_original = IMG_Load("resource/Robo-Intro.bmp"); img_intro = SDL_DisplayFormat(img_original); SDL_FreeSurface(img_original); if (img_intro==NULL) { std::cout << "Can't find intro image" << std::endl; ok = false; } img_original = IMG_Load("resource/Robo-spritesheet-x95-y120.bmp"); img_p1 = SDL_DisplayFormat(img_original); SDL_SetColorKey(img_p1, SDL_SRCCOLORKEY, SDL_MapRGB(img_p1->format, 255, 0, 255)); SDL_FreeSurface(img_original); if (img_p1==NULL) { std::cout << "Can't find p1's image" << std::endl; ok = false; } img_original = IMG_Load("resource/flamedragon.bmp"); img_npc = SDL_DisplayFormat(img_original); SDL_SetColorKey(img_npc, SDL_SRCCOLORKEY, SDL_MapRGB(img_npc->format, 255, 0, 255)); SDL_FreeSurface(img_original); if (img_npc==NULL) { std::cout << "Can't find npc's image" << std::endl; ok = false; } img_original = IMG_Load("resource/street.bmp"); img_background = SDL_DisplayFormat(img_original); SDL_FreeSurface(img_original); if(img_background == NULL) { std::cout << "Can't find background's image" << std::endl; ok = false; } img_bullet_right = IMG_Load("resource/bullet_right.png"); SDL_SetAlpha(img_bullet_right, SDL_SRCALPHA, 0); if (img_bullet_right==NULL) { std::cout << "Can't find right bullet's image" << std::endl; ok = false; } img_bullet_left = IMG_Load("resource/bullet_left.png"); SDL_SetAlpha(img_bullet_left, SDL_SRCALPHA, 0); if (img_bullet_left==NULL) { std::cout << "Can't find left bullet's image" << std::endl; ok = false; } if(Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 256) == -1) { std::cout << "Unable to open audio" << std::endl; ok =false; } snd_machine_gun = Mix_LoadWAV_RW(SDL_RWFromFile("resource/machine_gun.wav", "rb"), 1); if(snd_machine_gun==NULL) { std::cout << "Can't load machine_gun.wav" << std::endl; ok = false; } return ok; };
__declspec(dllexport) Mix_Chunk * __stdcall Mix_LoadWAV_VB6(const char*file) { return Mix_LoadWAV_RW(SDL_RWFromFile(file, "rb"), 1); }
int ONScripter::playSound(const char *filename, int format, bool loop_flag, int channel) { if ( !audio_open_flag ) return SOUND_NONE; long length = script_h.cBR->getFileLength( filename ); if (length == 0) return SOUND_NONE; unsigned char *buffer; if (format & SOUND_MUSIC && length == music_buffer_length && music_buffer ){ buffer = music_buffer; } else{ buffer = new(std::nothrow) unsigned char[length]; if (buffer == NULL){ fprintf( stderr, "failed to load [%s] because file size [%lu] is too large.\n", filename, length); return SOUND_NONE; } script_h.cBR->getFile( filename, buffer ); } if (format & SOUND_MUSIC){ music_info = Mix_LoadMUS_RW( SDL_RWFromMem( buffer, length ) ); Mix_VolumeMusic( music_volume ); Mix_HookMusicFinished( musicFinishCallback ); if ( Mix_PlayMusic( music_info, music_play_loop_flag?-1:0 ) == 0 ){ music_buffer = buffer; music_buffer_length = length; return SOUND_MUSIC; } } if (format & SOUND_CHUNK){ Mix_Chunk *chunk = Mix_LoadWAV_RW(SDL_RWFromMem(buffer, length), 1); if (playWave(chunk, format, loop_flag, channel) == 0){ delete[] buffer; return SOUND_CHUNK; } } /* check WMA */ if ( buffer[0] == 0x30 && buffer[1] == 0x26 && buffer[2] == 0xb2 && buffer[3] == 0x75 ){ delete[] buffer; return SOUND_OTHER; } if (format & SOUND_MIDI){ FILE *fp; if ( (fp = fopen(TMP_MUSIC_FILE, "wb")) == NULL){ fprintf(stderr, "can't open temporaly MIDI file %s\n", TMP_MUSIC_FILE); } else{ fwrite(buffer, 1, length, fp); fclose( fp ); ext_music_play_once_flag = !loop_flag; if (playMIDI(loop_flag) == 0){ delete[] buffer; return SOUND_MIDI; } } } delete[] buffer; return SOUND_OTHER; }
Sound::Sound() { if (SDL_Init(SDL_INIT_EVERYTHING) != 0) { SDL_Log("Unable to initialize SDL: %s", SDL_GetError()); return; } if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, 1024) == -1) { printf("%s", Mix_GetError()); } for(int i = 0; i < nbChannelSound; i++){ activeChannel[i] = false; } menu = Mix_LoadMUSType_RW(SDL_RWFromMem(music_menu_mp3, music_menu_mp3_len), MUS_MP3, 0); battle = Mix_LoadMUSType_RW(SDL_RWFromMem(music_battle_mp3, music_battle_mp3_len), MUS_MP3, 0); bipSound = Mix_LoadWAV_RW(SDL_RWFromMem(sound_bip_wav, sound_bip_wav_len), 0); bombeBounceSound = Mix_LoadWAV_RW(SDL_RWFromMem(sound_bounce_wav, sound_bounce_wav_len), 0); playerBurnSound = Mix_LoadWAV_RW(SDL_RWFromMem(sound_burn_wav, sound_burn_wav_len), 0); cancelSound = Mix_LoadWAV_RW(SDL_RWFromMem(sound_cancel_wav, sound_cancel_wav_len), 0); endSound = Mix_LoadWAV_RW(SDL_RWFromMem(sound_end_wav, sound_end_wav_len), 0); fireSound = Mix_LoadWAV_RW(SDL_RWFromMem(sound_fire_wav, sound_fire_wav_len), 0); hole1Sound = Mix_LoadWAV_RW(SDL_RWFromMem(sound_hole1_wav, sound_hole1_wav_len), 0); hole2Sound = Mix_LoadWAV_RW(SDL_RWFromMem(sound_hole2_wav, sound_hole2_wav_len), 0); hole3Sound = Mix_LoadWAV_RW(SDL_RWFromMem(sound_hole3_wav, sound_hole3_wav_len), 0); playerKickSound = Mix_LoadWAV_RW(SDL_RWFromMem(sound_kick_wav, sound_kick_wav_len), 0); louisSound = Mix_LoadWAV_RW(SDL_RWFromMem(sound_louis_wav, sound_louis_wav_len), 0); mineSound = Mix_LoadWAV_RW(SDL_RWFromMem(sound_mine_wav, sound_mine_wav_len), 0); teleporterCloseSound = Mix_LoadWAV_RW(SDL_RWFromMem(sound_teleporter_close_wav, sound_teleporter_close_wav_len), 0); teleporterOpenSound = Mix_LoadWAV_RW(SDL_RWFromMem(sound_teleporter_open_wav, sound_teleporter_open_wav_len), 0); validSound = Mix_LoadWAV_RW(SDL_RWFromMem(sound_valide_wav, sound_valide_wav_len), 0); Mix_PlayMusic(menu, -1); Mix_VolumeMusic (MIX_MAX_VOLUME); Mix_AllocateChannels(nbChannelSound); Mix_Volume(0, MIX_MAX_VOLUME / 2); Mix_Volume(3, MIX_MAX_VOLUME / 2); mineOffsetChannel = 14; }
extern DECLSPEC Mix_Chunk * SDLCALL Mix_LoadWAV_helper( char *file) { return Mix_LoadWAV_RW(SDL_RWFromFile(file, "rb"), 1); }
void Sound::loadFromMemory(const char * data, int length){ SDL_RWops * ops = SDL_RWFromConstMem(data, length); this->data.chunk = Mix_LoadWAV_RW(ops, 1); own = new int; *own = 1; }
/* Initialize */ void init_audio () { #if HAVE_LIBSDL_MIXER Uint16 audio_format; LDAT *ldat; int w; /* Initialize SDL_mixer library */ audio_format = MIX_DEFAULT_FORMAT; if (Mix_OpenAudio (luola_options.audio_rate, audio_format, 2, luola_options.audio_chunks) < 0) { fprintf (stderr,"Cannot open audio: %s\n", SDL_GetError ()); audio_available = 0; return; } else { audio_available = 1; Mix_QuerySpec (&luola_options.audio_rate, &audio_format, NULL); } /* Continue to the next song if it ends */ Mix_HookMusicFinished (playlist_forward); /* Load samples */ ldat = ldat_open_file(getfullpath(DATA_DIRECTORY,"sfx.ldat")); if(!ldat) { fprintf(stderr,"Can't load sound effects!"); } else { for (w = 0; w < SAMPLE_COUNT; w++) { samples[w] = Mix_LoadWAV_RW(ldat_get_item(ldat,"SFX",w),0); if (samples[w] == NULL) { fprintf (stderr,"Couldn't get SFX %d\n", w); } } } ldat_free(ldat); /* Load playlist */ playlist = NULL; { FILE *fp; char tmps[512]; char *line = NULL; fp = fopen (getfullpath (HOME_DIRECTORY, "battle.pls"), "r"); if (!fp) { fprintf (stderr,"No playlist file battle.pls\n"); return; } for (; fgets (tmps, sizeof (tmps) - 1, fp); free (line)) { line = strip_white_space (tmps); if (line == NULL || strlen (line) == 0) continue; if (line[0] == '#') continue; if(playlist) dllist_append(playlist,strdup(line)); else playlist = dllist_append(NULL,strdup(line)); } fclose (fp); playlist_original = NULL; /* Load the first song */ load_music(); } #endif }
void ConfigManager::buildSoundIndex() { int need_to_reserve = 0; int total_channels = 32; bool newBuild = main_sfx_index.empty(); #ifdef DEBUG_BUILD ElapsedTimer loadingTime; loadingTime.start(); #endif if(newBuild) { //build array table main_sfx_index.resize(static_cast<size_t>(main_sound.size()) - 1); for(unsigned long i = 1; i < main_sound.size(); i++) { obj_sound_index sound; if(main_sound.contains(i)) { obj_sound &snd = main_sound[i]; #if defined(__unix__) || defined(__APPLE__) || defined(_WIN32) FileMapper fileMap; if(fileMap.open_file(snd.absPath.c_str())) { sound.chunk = Mix_LoadWAV_RW(SDL_RWFromMem(fileMap.data(), static_cast<int>(fileMap.size())), 1); fileMap.close_file(); } #else sound.chunk = Mix_LoadWAV(snd.absPath.toUtf8().data()); #endif sound.path = snd.absPath; if(!sound.chunk) pLogWarning("Fail to load sound-%d: %s", i, Mix_GetError()); else need_to_reserve += (snd.channel >= 0 ? 1 : 0); sound.channel = snd.channel; } main_sfx_index[static_cast<size_t>(i) - 1] = sound; } } else { for(unsigned long i = 1; (i < main_sound.size()) && (i <= static_cast<unsigned long>(main_sfx_index.size())); i++) { if(main_sound.contains(i)) { obj_sound_index &sound = main_sfx_index[static_cast<size_t>(i) - 1]; obj_sound &snd = main_sound[i]; sound.setPath(snd.absPath); if(sound.need_reload) { #if defined(__unix__) || defined(__APPLE__) || defined(_WIN32) FileMapper fileMap; if(fileMap.open_file(snd.absPath.c_str())) { sound.chunk = Mix_LoadWAV_RW(SDL_RWFromMem(fileMap.data(), static_cast<int>(fileMap.size())), static_cast<int>(fileMap.size())); fileMap.close_file(); } #else sound.chunk = Mix_LoadWAV(snd.absPath.toUtf8().data()); #endif } if(!sound.chunk) pLogWarning("Fail to load sound-%d: %s", i, Mix_GetError()); else need_to_reserve += (snd.channel >= 0 ? 1 : 0); sound.channel = snd.channel; } } } if(need_to_reserve > 0) { total_channels = (total_channels + need_to_reserve + 32); total_channels = Mix_AllocateChannels(total_channels); } //Final channel definition (use reserved channels at end of channels set) //int set_channel = (total_channels-1); //for(int i=0; i < main_sfx_index.size(); i++) //{ // obj_sound_index &sound = main_sfx_index[i]; // if(sound.channel>=0) // { // sound.channel = set_channel--; // } //} if(need_to_reserve == total_channels) need_to_reserve = 0; //else // need_to_reserve=set_channel; #ifndef DEBUG_BUILD Mix_ReserveChannels(need_to_reserve) #endif #define RESERVE_CHANS_COMMAND Mix_ReserveChannels(need_to_reserve) D_pLogDebug("Loading of sounds passed in %d milliseconds", static_cast<int>(loadingTime.elapsed())); D_pLogDebug("Reserved audio channels: %d", RESERVE_CHANS_COMMAND); D_pLogDebug("SFX Index entries: %d", main_sfx_index.size()); #undef RESERVE_CHANS_COMMAND SDL_ClearError(); }
static void load_sound(const char *filename) { const struct ZipFileIndex *zip_index = NULL; struct player_sound *sound; unsigned long hash; const char *mem; int loaded = 0; int i; hash = crc32(0, (void *)filename, strlen(filename)); hash %= sizeof(sounds) / sizeof(sounds[0]); for (sound = sounds[hash]; sound != NULL; sound = sound->next) { if (strcmp(filename, sound->filename) == 0) break; } if (sound != NULL) { fprintf(stderr, "load_sound: %s already loaded?\n", filename); return; } for (i = 0; i < apk_index_pos; i++) { if (strstr(apk_index[i].filename, filename) != NULL) { zip_index = &apk_index[i]; break; } } if (zip_index == NULL) { fprintf(stderr, "load_sound: %s not in apk?\n", filename); return; } sound = calloc(1, sizeof(*sound)); if (sound == NULL) return; sound->filename = strdup(filename); sound->chunk_channel = -1; mem = (const char *)worldofgoo_priv.apk_in_mem + zip_index->offset; sound->rw = SDL_RWFromMem((void *)mem, zip_index->length); if (strstr(filename, "music/") != NULL) { sound->music = Mix_LoadMUS_RW(sound->rw); loaded = (sound->music != NULL); } else { sound->chunk = Mix_LoadWAV_RW(sound->rw, 0); if (sound->chunk != NULL) { loaded = 1; /* SDL_mixer can't resample 44100 to 32000 Hz :( */ if (strncmp(mem, "OggS", 4) == 0 && *(unsigned short *)(mem + 0x28) == 44100) lame_resample_44100_32000(sound->chunk); } } if (loaded) { MODULE_DEBUG_PRINTF("loaded %s\n", filename); sound->next = sounds[hash]; sounds[hash] = sound; } else { fprintf(stderr, "failed to load %s\n", filename); free(sound); } }
void playSoundAiff(uint8_t channel, const uint8_t *data, uint8_t volume) { const uint32_t size = READ_BE_UINT32(data + 4) + 8; SDL_RWops *rw = SDL_RWFromConstMem(data, size); Mix_Chunk *chunk = Mix_LoadWAV_RW(rw, 1); playSound(channel, volume, chunk); }