// allocates a voice for the sample "wav_index" (index into zelda.dat) // if a voice is already allocated (and/or playing), then it just returns true // Returns true: voice is allocated // false: unsuccessful bool sfx_init(int index) { // check index if(index<=0 || index>=WAV_COUNT) return false; if(sfx_voice[index]==-1) { if(sfxdat) { if(index<Z35) { sfx_voice[index]=allocate_voice((SAMPLE*)sfxdata[index].dat); } else { sfx_voice[index]=allocate_voice((SAMPLE*)sfxdata[Z35].dat); } } else { sfx_voice[index]=allocate_voice(&customsfxdata[index]); } voice_set_volume(sfx_voice[index], sfx_volume); } return sfx_voice[index] != -1; }
//Load Sound .wav to memory int AddVoice(const char *filename, int sfx) { SAMPLE *samp; int v; samp=load_sample(filename); if(!samp) return 0; if((v=allocate_voice(samp))==-1) { destroy_sample(samp); return 0; } if(sfx) { voice_len_sfx++; VoicesSfx=(int*)realloc(VoicesSfx,voice_len_sfx*sizeof(int)); VoicesSfx[voice_len_sfx-1]=v; voice_set_volume(v,Effect_volume); } else { voice_len_music++; VoicesMusic=(int*)realloc(VoicesMusic,voice_len_music*sizeof(int)); VoicesMusic[voice_len_music-1]=v; voice_set_volume(v,Music_volume); } return v; }
void try_sample_backwards(int sample_index, int x, int y, int volume, int priority, int freq) { if (!snd_data) return; SAMPLE *s = (SAMPLE *)(snd_data[sample_index].dat); int voice = allocate_voice(s); if (!playfield->visible(x,y)) { volume /= 2; priority /= 2; } if (voice >=0) { voice_set_volume(voice,volume); voice_set_priority(voice,priority); voice_set_frequency(voice, voice_get_frequency(voice) * freq / 1000); voice_set_playmode(voice, PLAYMODE_BACKWARD); voice_start(voice); release_voice(voice); } }
void try_loop(short *voice, int sample_index, int x, int y, int volume, int priority, int freq) { if (!snd_data) return; SAMPLE *s = (SAMPLE *)(snd_data[sample_index].dat); if (*voice <0) { *voice = allocate_voice(s); if (*voice >= 0) { voice_start(*voice); voice_set_frequency(*voice, voice_get_frequency(*voice) * freq / 1000); } } if (playfield && !playfield->visible(x,y)) { volume /= 2; priority /= 2; } if (*voice >=0) { voice_set_volume(*voice,volume); voice_set_priority(*voice,priority); voice_set_playmode(*voice, PLAYMODE_LOOP); } }
struct Animation *SeedPlayerAnimation(void) { struct Animation *Anim = (struct Animation *)malloc(sizeof(struct Animation)); SAMPLE *Sound; Anim->Animation[0] = ObtainBitmap("skater2"); Anim->Animation[1] = ObtainBitmap("skater3"); Anim->Animation[2] = ObtainBitmap("skater4"); Anim->CBitmap = Anim->Still = ObtainBitmap("skater1"); Anim->Slow = ObtainBitmap("skateslow"); Anim->Medium = ObtainBitmap("skatemed"); Anim->Fast = ObtainBitmap("skatefast"); if ((Sound = ObtainSample("skating"))) { Anim->SkateVoice = allocate_voice(Sound); voice_set_playmode(Anim->SkateVoice, PLAYMODE_BIDIR | PLAYMODE_LOOP); } else Anim->SkateVoice = -1; return Anim; }
void cSoundPlayer::playSound(SAMPLE *sample, int pan, int vol) { if (maximumVoices < 0) return; assert(sample); if (vol < 0) return; assert(pan >= 0 && pan < 256); assert(vol > 0 && vol <= 256); // allocate voice int voice = allocate_voice(sample); if (voice != -1) { voice_set_playmode(voice,0); voice_set_volume(voice, vol); voice_set_pan(voice, pan); voice_start(voice); for (int i = 0; i < maximumVoices; i++) { if (voices[i] == -1) { voices[i] = voice; break; } } } }
/* (`allegro_init()' is already defined by Allegro itself.) */ static int allegro_init_sound(const char *param, int *speed, int *fragsize, int *fragnr, int *channels) { unsigned int i; /* No stereo capability. */ *channels = 1; if (allegro_startup(*speed) < 0) return 1; fragment_size = *fragsize * sizeof(SWORD); buffer_len = fragment_size * *fragnr; buffer = create_sample(16, 0, *speed, buffer_len / sizeof(SWORD)); for (i = 0; i < buffer_len / 2; i++) *((WORD *)buffer->data + i) = 0x8000; voice = allocate_voice(buffer); if (voice < 0) { log_debug("Cannot allocate Allegro voice!\n"); destroy_sample(buffer); return 1; } buffer_offset = 0; been_suspended = 1; written_samples = 0; voice_set_playmode(voice, PLAYMODE_LOOP); voice_set_volume(voice, 255); voice_set_pan(voice, 128); return 0; }
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; }
void saPlayBufferedStreamedSampleBase( int channel, signed char *data, int len, int freq, int volume, int bits , int pan ){ /* This version works at low level, creating a sample, and following its advancement directly in the voice_position... */ int i; short *dout; short *dfin; signed short *din; //fprintf(stderr,"saPlayBuffer %d freq %d bits %d pan %d len %d\n",channel,freq,bits,pan,len); if( audio_sample_rate == 0 || channel >= NUMVOICES ) return; if( SndMachine == NULL ) return; if( !playing[channel] ){ #ifdef USE_COMPENS int fin = stream_buffer_max * freq * 2 / fps; #else int fin = stream_buffer_max * len; #endif if( lpWave[channel] ){ destroy_sample( lpWave[channel] ); lpWave[channel] = 0; } if (!(lpWave[channel] = create_sample(16,0,freq,fin/2))){ lpWave[channel] = 0; return; } // memset( lpWave[channel]->data, 0, fin ); dout=lpWave[channel]->data; dfin=(short*) (((char*)lpWave[channel]->data)+fin); // Fill the buffer with 0 (signed) in case the soundcards reads what // is after the sample... while (dout < dfin) *(dout++) = 0x8000; vend[channel] = dfin; counter[channel] = 0; hVoice[channel] = allocate_voice( lpWave[channel] ); if (hVoice[channel]<0) { allegro_message("allocate_voice failed !\n"); exit(1); } voice_set_playmode(hVoice[channel],PLAYMODE_LOOP); playing[channel] = 1; /* use front surface */ init_mixing_buff(len); /**** make sound temp. buffer ****/ if (enh_stereo && SamplePan[channel] == PAN_LEFT) dout=(short*) (((char*)lpWave[channel]->data)+len*(MODEB_UPDATE_COUNT+1)); //+len*MODEB_UPDATE_COUNT); else dout=(short*) (((char*)lpWave[channel]->data)+len*MODEB_UPDATE_COUNT); //+len*MODEB_UPDATE_COUNT); din = ((signed short*)data); // memcpy( dout, din, len ); for (i=0; i<len; i+=2){ *(dout++) = *(din++)^0x8000; } update_recording(channel,data); if (dout ==dfin){ dout=(short*) (((char*)lpWave[channel]->data)); } #ifdef DUMP_CHANNELS fwrite( lpWave[channel]->data+len*MODEB_UPDATE_COUNT, 1, len, stream_out[channel]); #endif vout[channel] = dout; saSetVolume(channel,SampleVol[channel]); saSetPan(channel,SamplePan[channel]); voice_set_position(hVoice[channel],0); voice_start(hVoice[channel]); pos_counter[channel] = 0; } else{ int pos = voice_get_position(hVoice[channel]); int th_pos; int count = (enh_stereo && SamplePan[channel] == PAN_LEFT ? MODEB_UPDATE_COUNT + 1 : MODEB_UPDATE_COUNT); // this difference between the theorical position and the actual // position is because clearly the reported position is directly // dependant of when the sound driver updated the voice for the // last time. Luckily the difference is big only when the voice // starts. After starting it gets more or less updated when it // should, depending on external factors too like the cpu load. dout=vout[channel]; th_pos = (dout - ((INT16 *)lpWave[channel]->data)- count*len/2); if (th_pos < 0) th_pos += stream_buffer_max * len/2; // if there is more than count frames between pos and th_pos, then // wait for the voice. if (pos < th_pos) { if (th_pos - pos < count * len/2) { more_stream = -1; // drop next frame } } din = ((signed short*)data); dfin = vend[channel]; // memcpy(dout,din,len); for (i=0; i<len; i+=2){ *(dout++) = *(din++)^0x8000; } update_recording(channel,data); if (dout >=dfin){ dout=(short*) (((char*)lpWave[channel]->data)); } #ifdef DUMP_CHANNELS fwrite( lpWave[channel]->data+len*s_pos, 1, len, stream_out[channel]); #endif vout[channel] = dout; // more than count frames of advance : more stream ! pos -= count*len/2; if (pos > th_pos && pos > 0) { // send more more_stream = 1; } } }
int main() { int audio, working_format, bitcap,stereocap,ratecap; int ofs = 0; int len; int interval; SAMPLE *sample; int foo; int size = 1024*1024; unsigned char *buf; init(); show_mouse(screen); bitcap = get_sound_input_cap_bits(); if (bitcap == 0) allegro_message("No Audio input capabilty?"); stereocap = get_sound_input_cap_stereo(); if (stereocap == 0) allegro_message("No Sterecordingreo "); ratecap = get_sound_input_cap_rate(bitcap, 0); working_format = get_sound_input_cap_parm(ratecap, bitcap, 0); if (working_format == 0) allegro_message("We Are not a go!"); else if (working_format == 1) allegro_message("We Are a go but can't record and playback at the same time!"); else if (working_format == 2) allegro_message("We Are a go!"); BITMAP *catli=NULL,*catmo=NULL,*catmc=NULL; catli=load_bitmap("listen.bmp",NULL); catmo=load_bitmap("mo.bmp",NULL); catmc=load_bitmap("mc.bmp",NULL); sample = create_sample(bitcap, 0, ratecap, size); buf=(unsigned char*)(sample->data); allegro_message("Start Recording"); len = start_sound_input(ratecap, 16, 0); masked_blit(catli,screen,0,0,200,50,500,500); interval = 1000 / ((len / 2) / ratecap); interval *= 9; interval /= 10; while (!keypressed() && ofs < size) { foo = read_sound_input(buf); if (foo > 0) { ofs += len; buf += len; } rest(interval); } if (keypressed()) { foo = read_sound_input(buf); if (foo > 0) { ofs += len; buf += len; } } stop_sound_input(); BITMAP *bit=NULL,*bit2=NULL; bit=load_bitmap("play.bmp",NULL); masked_blit(bit,screen,0,0,100,100,400,400); bit2=load_bitmap("pause.bmp",NULL); masked_blit(bit2,screen,0,0,100,200,400,400); audio= allocate_voice(sample); int u=0; while (!key[KEY_ESC]) { if((mouse_x>=100 && mouse_x<=200)&&(mouse_y>=100 && mouse_y<=200)&& mouse_b&0x1) { play_sample(sample,255,128,1400,0); //voice_start(audio); while(!((mouse_x>=100 && mouse_x<=200)&&(mouse_y>=200 && mouse_y<=300)&& mouse_b&0x1u<=10)&& u<size) { masked_blit(catmo,screen,0,0,200,100,500,500); rest(300); masked_blit(catmc,screen,0,0,200,100,500,500); rest(300); u++; } } if((mouse_x>=100 && mouse_x<=200)&&(mouse_y>=200 && mouse_y<=300)&& mouse_b&0x1) { voice_stop(audio); masked_blit(catmc,screen,0,0,200,100,500,500);} /* put your code here */ } deinit(); return 0; }
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); }
int osd_start_audio_stream(int stereo) { #ifdef USE_SEAL int channel; #endif if (stereo) stereo = 1; /* make sure it's either 0 or 1 */ stream_cache_stereo = stereo; /* determine the number of samples per frame */ samples_per_frame = (double)Machine->sample_rate / (double)Machine->drv->frames_per_second; /* compute how many samples to generate this frame */ samples_left_over = samples_per_frame; samples_this_frame = (UINT32)samples_left_over; samples_left_over -= (double)samples_this_frame; audio_buffer_length = NUM_BUFFERS * samples_per_frame + 20; if (Machine->sample_rate == 0) return 0; #ifdef USE_SEAL for (channel = 0;channel <= stereo;channel++) { if (ACreateAudioVoice(&hVoice[channel]) != AUDIO_ERROR_NONE) return 0; if ((lpWave[channel] = (LPAUDIOWAVE)malloc(sizeof(AUDIOWAVE))) == 0) { ADestroyAudioVoice(hVoice[channel]); return 0; } lpWave[channel]->wFormat = AUDIO_FORMAT_16BITS | AUDIO_FORMAT_MONO | AUDIO_FORMAT_LOOP; lpWave[channel]->nSampleRate = nominal_sample_rate; lpWave[channel]->dwLength = 2*audio_buffer_length; lpWave[channel]->dwLoopStart = 0; lpWave[channel]->dwLoopEnd = lpWave[channel]->dwLength; if (ACreateAudioData(lpWave[channel]) != AUDIO_ERROR_NONE) { free(lpWave[channel]); lpWave[channel] = 0; return 0; } memset(lpWave[channel]->lpData,0,lpWave[channel]->dwLength); APrimeVoice(hVoice[channel],lpWave[channel]); ASetVoiceFrequency(hVoice[channel],nominal_sample_rate); } if (stereo) { /* SEAL doubles volume for panned channels, so we have to compensate */ ASetVoiceVolume(hVoice[0],32); ASetVoiceVolume(hVoice[1],32); ASetVoicePanning(hVoice[0],0); ASetVoicePanning(hVoice[1],255); AStartVoice(hVoice[0]); AStartVoice(hVoice[1]); } else { ASetVoiceVolume(hVoice[0],64); ASetVoicePanning(hVoice[0],128); AStartVoice(hVoice[0]); } #endif #ifdef USE_ALLEGRO mysample = create_sample(16,stereo,nominal_sample_rate,audio_buffer_length); if (mysample == 0) return 0; myvoice = allocate_voice(mysample); voice_set_playmode(myvoice,PLAYMODE_LOOP); if (stereo) { INT16 *buf = mysample->data; int p = 0; while (p != audio_buffer_length) { buf[2*p] = (INT16)0x8000; buf[2*p+1] = (INT16)0x8000; p++; } } else { INT16 *buf = mysample->data; int p = 0; while (p != audio_buffer_length) { buf[p] = (INT16)0x8000; p++; } } voice_start(myvoice); #endif stream_playing = 1; voice_pos = 0; return samples_this_frame; }