//Unload sounds from memory void DisposeVoices() { int i; SAMPLE* samp; for(i=0; i<voice_len_music; i++) { if((samp=voice_check(VoicesMusic[i]))!=NULL) //Addionnal check { destroy_sample(samp); } deallocate_voice(VoicesMusic[i]); } for(i=0; i<voice_len_sfx; i++) { if((samp=voice_check(VoicesSfx[i]))!=NULL) //Addionnal check { destroy_sample(samp); } deallocate_voice(VoicesSfx[i]); } free(VoicesMusic); VoicesMusic=NULL; voice_len_music=0; free(VoicesSfx); VoicesSfx=NULL; voice_len_sfx=0; }
void osd_stop_audio_stream(void) { #ifdef USE_SEAL int i; #endif if (Machine->sample_rate == 0) return; #ifdef USE_SEAL /* stop and release voices */ for (i = 0;i < SOUND_CHANNELS;i++) { if (lpWave[i]) { AStopVoice(hVoice[i]); ADestroyAudioData(lpWave[i]); free(lpWave[i]); lpWave[i] = 0; ADestroyAudioVoice(hVoice[i]); } } #endif #ifdef USE_ALLEGRO voice_stop(myvoice); deallocate_voice(myvoice); destroy_sample(mysample); mysample = 0; #endif stream_playing = 0; }
void kill_sfx() { for(int i=0; i<WAV_COUNT; i++) if(sfx_voice[i]!=-1) { deallocate_voice(sfx_voice[i]); sfx_voice[i]=-1; } }
// clean up finished samples void sfx_cleanup() { for(int i=0; i<WAV_COUNT; i++) if(sfx_voice[i]!=-1 && voice_get_position(sfx_voice[i])<0) { deallocate_voice(sfx_voice[i]); sfx_voice[i]=-1; } }
void saDestroyChannel( int chan ) { if( lpWave[chan] ){ voice_stop(hVoice[chan]); destroy_sample( lpWave[chan] ); lpWave[chan] = 0; playing[chan] = 0; deallocate_voice(hVoice[chan]); } }
// stops an sfx and deallocates the voice void stop_sfx(int index) { if(index<=0 || index>=WAV_COUNT) return; if(sfx_voice[index]!=-1) { deallocate_voice(sfx_voice[index]); sfx_voice[index]=-1; } }
void kill_loop(short *voice) { if (!snd_data) return; if (*voice < 0) return; voice_stop(*voice); deallocate_voice(*voice); *voice = -1; }
void remove_mod (void) { int index; stop_mod(); remove_int (mod_interrupt); for (index=0; index<MAX_ALLEG_VOICE; index++) { if (voice_table[index] >= 0) deallocate_voice (voice_table[index]); voice_table[index] = -1; } mod_init = FALSE; }
void destroy_it(MODULUS *j) { if (song->Music == j) stop_it(); //remove patterns: for (int i=0; i<j->NumPatterns; i++) { free(j->Pattern[i].Note); } if (j->Pattern) free(j->Pattern); //remove instruments; if (j->Instrument) free(j->Instrument); //remove samples; for (int i=0; i<j->NumSamples; i++) { destroy_sample(j->Sample[i].Sample); } if (j->Sample) free(j->Sample); //remove orders: if (j->Order) free(j->Order); //remove channels: for (int i=0; i<64; i++) { if (j->Channel[i].VChannel) { MODULUS_VCHANNEL *vchn = song->Music->Channel[i].VChannel; MODULUS_VCHANNEL *prev = NULL; if (!vchn) continue; for (;;) { deallocate_voice(vchn->voice); prev = vchn; vchn = vchn->next; free(prev); if (!vchn) break; } } } free(j); }
void cSoundPlayer::destroySound(int voice, bool force) { if (maximumVoices < 0) return; if (voice < 0) return; if (voices[voice] < 0) return; if (force) { deallocate_voice(voices[voice]); } else { release_voice(voices[voice]); } for (int i = 0; i < maximumVoices; i++) { if (voices[i] == voice) { voices[i] = -1; break; } } }
void triple_buffer(BITMAP **page) { int i = 0, w, repeat, active_page = 0; int time_diff, bar_number, *used_voices = NULL, hw_voices; int text_bar_height, half_bar_size; unsigned int radius; coord_t dot[2], dot_old[2], cal[2], cal_tmp[2]; wiimote *wiimote; char *fonts[NUM_MAX_FONTS] = FONTS; FONT *font_msg, *font_notes; BITMAP *background; bars *bar; /* dot_old data initialization. dot_old's X coord is not used */ dot_old[0].y = SCREEN_H; dot_old[1].y = SCREEN_H; /* Wiimote initialization*/ wiimote = *wiiuse_init(1); /* Load fonts, size of font is related to SCREEN_W */ do { font_msg = load_font(fonts[i], NULL, NULL); __ASSERT(font_msg, ERROR_CANT_LOAD_FONT(fonts[i])); w = text_length(font_msg, WIIMOTE_CONNECTION_MSG); } while (w >= SCREEN_W && ++i < (NUM_MAX_FONTS-1)); /* Notes' font is smaller than messages' font */ font_notes = load_font(fonts[i], NULL, NULL); __ASSERT(font_notes, ERROR_CANT_LOAD_FONT(fonts[i])); /* Initialize and calculate bars size, point radius, text bar height, and calculate half bar size for text output under bars */ bar = bar_create(NUM_BARS); radius = RADIUS(bar); text_bar_height = TEXT_BAR_HEIGHT; half_bar_size = HALF_BAR_SIZE(bar); /* Control the max number of available hardware voices. Though ALSA driver voices are up to 64, max hardware voices generally are 8-16. Checking the allocated voices after the driver allocation seems to be the only way */ hw_voices = get_mixer_voices(); if (hw_voices < NUM_BARS) used_voices = reallocate_voices(bar, hw_voices); /* Install timer, 10 ticks a second */ LOCK_VARIABLE(timer); LOCK_FUNCTION(inc_timer); install_int_ex(inc_timer, BPS_TO_TIMER(10)); /* Load background from file */ background = load_tga("images/back.tga", NULL); __ASSERT(background, ERROR_CANT_LOAD_IMAGE("back.tga")); /* Enables vertical syncronization*/ vsync(); i = 0; /* First frame, this lasts until wiimote is connected, the user is prompted to activate the wiimote by pressing 1 & 2 keys on the wiimote */ while(i == 0 && !keypressed()) { active_page = start_credits3buf(page, active_page); clear_keybuf(); title3buf(page, active_page, font_msg); clear_keybuf(); repeat = 0; while(repeat++ < 8 && i == 0 && !keypressed()) { stretch_blit(background, page[active_page], 0, 0, background->w, background->h, 0, 0, SCREEN_W, SCREEN_H); // background if(repeat%2 == 1) textout_centre_ex(page[active_page], font_msg, WIIMOTE_CONNECTION_MSG, SCREEN_W/2, SCREEN_H/2, makecol(0, 0, 0), -1); // text prompt release_bitmap(page[active_page]); /* make sure the last flip request has actually happened */ do { } while (poll_scroll()); /* post a request to display the page we just drew */ request_video_bitmap(page[active_page]); /* update counters to point to the next page */ switch (active_page) { case 0: active_page = 1; break; case 1: active_page = 2; break; case 2: active_page = 0; break; } /* Search for a wiimote */ i = wiiuse_find(&wiimote, 1, 1); } } /* Try to connect to the wiimote */ __ASSERT(wiiuse_connect(&wiimote, 1) > 0, ERROR_CANT_OPEN_WIIMOTE); /* Activate the first led on the wiimote */ wiiuse_set_leds(wiimote, WIIMOTE_LED_1); /* Activate the ir module on the wiimote */ wiiuse_set_ir(wiimote, TRUE); wiiuse_motion_sensing(wiimote, FALSE); wiiuse_set_ir_sensitivity(wiimote, 1); wiiuse_set_flags(wiimote, WIIUSE_CONTINUOUS, 0); cal[0].x = 0; cal[0].y = 0; cal[1].x = 1023; cal[1].y = 767; /* CALIBRATION LOOP */ for(repeat=0;repeat<2;repeat++) { clear_keybuf(); while(!keypressed()) { if (wiiuse_poll(&wiimote, 1)) // if there are datas pending from/to wiimote or ESC is pressed if (key[KEY_ESC] || wiimote->event == WIIUSE_DISCONNECT || wiimote->event == WIIUSE_UNEXPECTED_DISCONNECT || IS_PRESSED(wiimote, WIIMOTE_BUTTON_HOME)) { // if ESC is pressed, if wiimote update fails, or if HOME key on wiimote is pressed wiiuse_disconnect(wiimote); destroy_bitmap(background); destroy_font(font_msg); destroy_font(font_notes); for (i=0;i<NUM_BARS;i++) { deallocate_voice(bar[i].voice); destroy_sample(bar[i].sound); } free(bar); // YO!! :-) free(used_voices); return; } /* background */ stretch_blit(background, page[active_page], 0, 0, background->w, background->h, 0, 0, SCREEN_W, SCREEN_H); if (wiimote->ir.dot[0].visible) { // if ir source is visible /* Read coords from the wiimote's ir*/ dot[0] = transpose(wiimote->ir.dot[0], cal, 0); } switch(repeat) { case 0: textout_centre_ex(page[active_page], font_msg, WIIMOTE_CAL_ASX, SCREEN_W/2, SCREEN_H/2, makecol(0, 0, 0), -1); // text prompt rect(page[active_page], dot[0].x, dot[0].y, SCREEN_W+1, SCREEN_H+1, makecol(0, 0, 0)); break; case 1: textout_centre_ex(page[active_page], font_msg, WIIMOTE_CAL_BDX, SCREEN_W/2, SCREEN_H/2, makecol(0, 0, 0), -1); // text prompt rect(page[active_page], dot[1].x, dot[1].y, SCREEN_W+1, SCREEN_H+1, makecol(0, 0, 0)); rect(page[active_page], -1, -1, dot[0].x, dot[0].y, makecol(0, 0, 0)); break; } circlefill(page[active_page], dot[0].x, dot[0].y, radius, makecol(0, 0, 0)); release_bitmap(page[active_page]); /* make sure the last flip request has actually happened */ do { } while (poll_scroll()); /* post a request to display the page we just drew */ request_video_bitmap(page[active_page]); /* update counters to point to the next page */ switch (active_page) { case 0: active_page = 1; break; case 1: active_page = 2; break; case 2: active_page = 0; break; } } cal_tmp[repeat].x = wiimote->ir.dot[0].x; cal_tmp[repeat].y = wiimote->ir.dot[0].y; dot[1] = dot[0]; } __ASSERT((cal_tmp[0].x < cal_tmp[1].x && cal_tmp[0].y > cal_tmp[1].y), ERROR_WHILE_CALIBRATING); cal[0].x = cal_tmp[0].x; cal[0].y = 767-cal_tmp[0].y; cal[1].x = cal_tmp[1].x; cal[1].y = 767-cal_tmp[1].y; /* MAIN LOOP */ while (TRUE) { /* Draw a frame */ if (wiiuse_poll(&wiimote, 1) || key[KEY_ESC]) // if there are datas pending from/to wiimote or ESC is pressed if (key[KEY_ESC] || wiimote->event == WIIUSE_DISCONNECT || wiimote->event == WIIUSE_UNEXPECTED_DISCONNECT || IS_PRESSED(wiimote, WIIMOTE_BUTTON_HOME)) { // if ESC is pressed, if wiimote update fails, or if HOME key on wiimote is pressed wiiuse_disconnect(wiimote); destroy_bitmap(background); destroy_font(font_msg); destroy_font(font_notes); for (i=0;i<NUM_BARS;i++) { deallocate_voice(bar[i].voice); destroy_sample(bar[i].sound); } free(bar); // YO!! :-) free(used_voices); return; } /* background */ stretch_blit(background, page[active_page], 0, 0, background->w, background->h, 0, 0, SCREEN_W, SCREEN_H); /* Xylophone's bars and notes names */ for(i=0;i<NUM_BARS;i++) { if (bar[i].t_start != -1 && (time_diff = timer-bar[i].t_start) > TICKS_TO_BLACK) // if color animation ends bar[i].t_start = -1; if (bar[i].t_start == -1) // if no color animation bar[i].color = 0; else bar[i].color = COLORVAL(time_diff); // if color animation is running /* Draw bar */ rectfill(page[active_page], bar[i].min.x, bar[i].min.y, bar[i].max.x, bar[i].max.y, makecol(bar[i].color, bar[i].color, bar[i].color)); /* Print bar's associated note */ textout_centre_ex(page[active_page], font_notes, bar[i].note, (bar[i].min.x + half_bar_size), text_bar_height, makecol(0, 0, 0), -1); } // da normalizzare e da contenere nello schermo, ir for(i=0;i<MAX_IR_DOTS;i++) { if (wiimote->ir.dot[i].visible) { // if ir source is visible /* Read coords from the wiimote's ir*/ dot[i] = transpose(wiimote->ir.dot[i], cal, radius); /* If the ir source is under the bars and in previous frame it was above the bars, then play the sound and start the animation */ if (dot[i].y > bar[0].min.y-radius) { if(dot_old[i].y <= bar[0].min.y-radius) { /* This calculates on which bar the ir source actually is */ bar_number = is_onbar(bar, dot[i].x, NUM_BARS); /* play bar_number's sound with specified volume */ play_bar_voice(bar, bar_number, volume(dot[i].y-dot_old[i].y), used_voices, hw_voices); } /* The dot have not to go under the bars or out of the screen */ circlefill(page[active_page], dot[i].x, bar[0].min.y-radius, radius, makecol(0, 0, 0)); } else circlefill(page[active_page], dot[i].x, dot[i].y, radius, makecol(0, 0, 0)); dot_old[i].y = dot[i].y; } else dot_old[i].y = SCREEN_H; } release_bitmap(page[active_page]); /* make sure the last flip request has actually happened */ do { } while (poll_scroll()); /* post a request to display the page we just drew */ request_video_bitmap(page[active_page]); /* update counters to point to the next page */ switch (active_page) { case 0: active_page = 1; break; case 1: active_page = 2; break; case 2: active_page = 0; break; } } }
int install_mod(int max_chn) { int index; int temp=0; if (mod_init == TRUE) // don't need to initialize many times return 1; register_datafile_jgmod(); if ( (max_chn > MAX_ALLEG_VOICE) || (max_chn <= 0) ) return -1; if (fake_sample == null) fake_sample = (SAMPLE *)jgmod_calloc (sizeof (SAMPLE)); // use to trick allegro // into giving me voice if (fake_sample == null) // channels { sprintf (jgmod_error, "Unable to setup initialization sample"); return -1; } fake_sample->freq = 1000; fake_sample->loop_start = 0; fake_sample->loop_end = 0; fake_sample->priority = JGMOD_PRIORITY; #ifdef ALLEGRO_DATE // for compatibility with Allegro 3.0 fake_sample->stereo = FALSE; #endif fake_sample->bits = 8; fake_sample->len = 0; fake_sample->param = -1; fake_sample->data = jgmod_calloc (0); if (fake_sample->data == null) { sprintf (jgmod_error, "JGMOD : Not enough memory to setup initialization sample"); free (fake_sample); return -1; } for (index=0; index<max_chn; index++) //allocate all the voices { voice_table[index] = allocate_voice (fake_sample); if (voice_table[index] == -1) temp = -1; else { ci[index].volume = 0; voice_set_volume (voice_table[index], 0); voice_start (voice_table[index]); } } if (temp == -1) { for (index=0; index<max_chn; index++) if (voice_table[index] != -1) { deallocate_voice (voice_table[index]); voice_table[index] = -1; } sprintf (jgmod_error, "JGMOD : Unable to allocate enough voices"); return -1; } mi.max_chn = max_chn; mi.is_playing = FALSE; mi.speed_ratio = 100; mi.pitch_ratio = 100; mod_init = TRUE; atexit (remove_mod); lock_jgmod_player(); // lock functions and variables in player.c lock_jgmod_player2(); // in player2.c lock_jgmod_player3(); // in player3.c lock_jgmod_player4(); // in player4.c lock_jgmod_mod(); // and mod.c return 1; }
static void allegro_close(void) { voice_stop(voice); deallocate_voice(voice); destroy_sample(buffer); }
void GoPlaySound(int num, long pan, long vol, byte flags, int priority) { char txt[32]; int i, best, count; // load the sample if it isn't already if (soundbuf[num].sample == NULL) { sprintf(txt, "sound\\snd%03d.wav", num); soundbuf[num].sample = load_sample(txt); if (soundbuf[num].sample == NULL) return; // can't play the sound, it won't load for some reason } priority += vol; // the quieter a sound, the lower the priority if (flags & SND_MAXPRIORITY) priority = MAX_SNDPRIORITY; if (flags & SND_ONE) { for (i = 0; i < MAX_SOUNDS_AT_ONCE; i++) if (playBuffer[i].soundNum == num) { // if you want to cut it off, or it isn't playing, then start anew if ((flags & SND_CUTOFF) || (!(playBuffer[i].flags & SND_PLAYING))) { playBuffer[i].pan = pan; playBuffer[i].vol = vol; playBuffer[i].flags = flags | SND_PLAYING; playBuffer[i].priority = priority; JamulSoundPlay(playBuffer[i].voice, playBuffer[i].pan, playBuffer[i].vol, SOUND_CUTOFF); return; // good job } else return; // can't be played because can't cut it off } // if you fell through to here, it isn't playing, so go ahead as normal } if (flags & SND_FEW) { count = 0; for (i = 0; i < MAX_SOUNDS_AT_ONCE; i++) if (playBuffer[i].soundNum == num && (playBuffer[i].flags & SND_PLAYING)) count++; if (count >= MAX_FEW_SOUNDS) { for (i = 0; i < MAX_SOUNDS_AT_ONCE; i++) if (playBuffer[i].soundNum == num) { if ((flags & SND_CUTOFF) && (playBuffer[i].flags & SND_PLAYING)) { playBuffer[i].pan = pan; playBuffer[i].vol = vol; playBuffer[i].flags = flags | SND_PLAYING; playBuffer[i].priority = priority; JamulSoundPlay(playBuffer[i].voice, playBuffer[i].pan, playBuffer[i].vol, SOUND_CUTOFF); return; // good job } } return; // failed for some reason } } best = -1; for (i = 0; i < MAX_SOUNDS_AT_ONCE; i++) { if (playBuffer[i].soundNum == -1 || (!(playBuffer[i].flags & SND_PLAYING))) { best = i; break; // can't beat that } if ((playBuffer[i].priority < priority) || (playBuffer[i].soundNum == num && (flags & SND_CUTOFF))) { if (best == -1 || playBuffer[i].priority < playBuffer[best].priority) best = i; } } if (best == -1) return; // sound is not worthy to be played if (playBuffer[best].soundNum != num) // if it was already playing that sound, don't waste time { playBuffer[best].soundNum = num; if (playBuffer[best].voice != -1) { deallocate_voice(playBuffer[best].voice); // slash & burn } playBuffer[best].voice = allocate_voice(soundbuf[num].sample); } else { voice_set_position(playBuffer[best].voice, 0); } if (playBuffer[best].voice == -1) return; // can't play it playBuffer[best].priority = priority; playBuffer[best].pan = pan; playBuffer[best].vol = vol; playBuffer[best].flags = flags | SND_PLAYING; JamulSoundPlay(playBuffer[best].voice, playBuffer[best].pan, playBuffer[best].vol, 0); }