void audiomanager::musicpreload(int id) { if(nosound) return; stopsound(); if(musicvol && (id>=M_FLAGGRAB && id<=M_LASTMINUTE2)) { char *name = musics[id]; conoutf("preloading music #%d : %s", id, name); if(gamemusic->open(name)) { defformatstring(whendone)("musicvol %d", musicvol); musicdonecmd = newstring(whendone); //conoutf("when done: %s", musicdonecmd); const int preloadfadetime = 3; gamemusic->fadein(lastmillis, preloadfadetime); gamemusic->fadeout(lastmillis+2*preloadfadetime, preloadfadetime); if(!gamemusic->playback(false)) { conoutf("could not play music: %s", name); return; } setmusicvol(1); // not 0 ! } else conoutf("could not open music: %s", name); } else setmusicvol(musicvol); // call "musicpreload -1" to ensure musicdonecmd runs - but it should w/o that }
void audiomanager::music(char *name, int millis, char *cmd) { if(nosound) return; stopsound(); if(musicvol && *name) { if(cmd[0]) musicdonecmd = newstring(cmd); if(gamemusic->open(name)) { // fade if(millis > 0) { const int fadetime = 1000; gamemusic->fadein(lastmillis, fadetime); gamemusic->fadeout(lastmillis+millis, fadetime); } // play bool loop = cmd && cmd[0]; if(!gamemusic->playback(loop)) { conoutf("could not play music: %s", name); return; } setmusicvol(musicvol); } else conoutf("could not open music: %s", name); } }
sample *s; int vol; int uses, maxuses; }; struct soundloc { vec loc; bool inuse; soundslot *slot; extentity *ent; }; vector<soundloc> soundlocs; void setmusicvol(int musicvol) { if(nosound) return; if(mod) Mix_VolumeMusic((musicvol*MAXVOL)/255); } VARP(soundvol, 0, 255, 255); VARFP(musicvol, 0, 128, 255, setmusicvol(musicvol)); char *musicfile = NULL, *musicdonecmd = NULL; void stopsound() { if(nosound) return; DELETEA(musicfile); DELETEA(musicdonecmd); if(mod) { Mix_HaltMusic(); Mix_FreeMusic(mod); mod = NULL; } }
void audiomanager::initsound() { if(!audio) { conoutf("audio is disabled"); return; } nosound = true; device = NULL; context = NULL; // list available devices if(alcIsExtensionPresent(NULL, "ALC_ENUMERATION_EXT")) { const ALCchar *devices = alcGetString(NULL, ALC_DEVICE_SPECIFIER); if(devices) { string d; copystring(d, "Audio devices: "); // null separated device string for(const ALchar *c = devices; *c; c += strlen(c)+1) { if(c!=devices) concatstring(d, ", "); concatstring(d, c); } conoutf("%s", d); } } // open device const char *devicename = getalias("openaldevice"); device = alcOpenDevice(devicename && devicename[0] ? devicename : NULL); if(device) { context = alcCreateContext(device, NULL); if(context) { alcMakeContextCurrent(context); alDistanceModel(AL_INVERSE_DISTANCE_CLAMPED); // backend infos conoutf("Sound: %s / %s (%s)", alcGetString(device, ALC_DEVICE_SPECIFIER), alGetString(AL_RENDERER), alGetString(AL_VENDOR)); conoutf("Driver: %s", alGetString(AL_VERSION)); // allocate OpenAL resources sourcescheduler::instance().init(16); // let the stream get the first source from the scheduler gamemusic = new oggstream(); if(!gamemusic->valid) DELETEP(gamemusic); setmusicvol(musicvol); nosound = false; } } if(nosound) { ALERR; if(context) alcDestroyContext(context); if(device) alcCloseDevice(device); conoutf("sound initialization failed!"); } }