int SourceSample::Mix(Uint8 *data, int len) { if(_buffer == NULL) return 0; int volume = (int)(_volume * SDL_MIX_MAXVOLUME); nebu_assert(len < _buffersize); if(len < _buffersize - _position) { SDL_MixAudio(data, _buffer + _position, len, volume); _position += len; } else { SDL_MixAudio(data, _buffer + _position, _buffersize - _position, volume); len -= _buffersize - _position; // printf("end of sample reached!\n"); if(_loop) { if(_loop != 255) _loop--; _position = 0; SDL_MixAudio(data, _buffer + _position, len, volume); _position += len; } else { _isPlaying = 0; } } return 1; }
void sdlMix(Uint8 *stream, int len, SoundDataRing* data, bool queueMode) { int mixed=0; while(len>0 && data->size()>0) { QElement& elm = data->front(); int elm_left=elm.len-elm.pos; //mix all that is possible from front then continue into next if( len > elm_left) { SDL_MixAudio(stream+mixed, &elm.data[elm.pos], elm_left, SDL_MIX_MAXVOLUME); //done with this one data->pop(); //take rest from next element len-=elm_left; if (queueMode)mixed+=elm_left; } //take everything from this element else { SDL_MixAudio(stream+mixed, &elm.data[elm.pos], len, SDL_MIX_MAXVOLUME); elm.pos+=len; len-=elm_left; } } }
void SDLAudioDevice::real_callback(Uint8 *stream, int len) { // fprintf(stderr, "sdl cbk: %d bytes needed, size:%d\n", len, audio_offset); if (len <= audio_offset) { SDL_MixAudio(stream, (const Uint8 *)audio_buffer, len, SDL_MIX_MAXVOLUME); audio_offset -= len; if (audio_offset > 0) { memcpy(audio_buffer, (audio_buffer + len), audio_offset); } else audio_offset = 0; } else { if (audio_offset > 0) { SDL_MixAudio(stream, (const Uint8 *)audio_buffer, audio_offset, SDL_MIX_MAXVOLUME); len -= audio_offset; } memset(stream + audio_offset, silence, len); audio_offset = 0; } }
/** * Callback used to provide data to the audio subsystem. * * @param userdata N/A * @param stream Output stream * @param len Length of data to be placed in the output stream */ void audioCallback (void * userdata, unsigned char * stream, int len) { (void)userdata; int count; if (!music_paused) { // Read the next portion of music into the audio stream #if defined(USE_MODPLUG) if (musicFile) ModPlug_Read(musicFile, stream, len); #elif defined(USE_XMP) if (xmp_get_player(xmpC, XMP_PLAYER_STATE) == XMP_STATE_PLAYING) xmp_play_buffer(xmpC, stream, len, 0); #endif } for (count = 0; count < 32; count++) { if (sounds[count].data && (sounds[count].position >= 0)) { // Add the next portion of the sound clip to the audio stream if (len < sounds[count].length - sounds[count].position) { // Play as much of the clip as possible SDL_MixAudio(stream, sounds[count].data + sounds[count].position, len, soundsVolume * SDL_MIX_MAXVOLUME / MAX_VOLUME); sounds[count].position += len; } else { // Play the remainder of the clip SDL_MixAudio(stream, sounds[count].data + sounds[count].position, sounds[count].length - sounds[count].position, soundsVolume * SDL_MIX_MAXVOLUME / MAX_VOLUME); sounds[count].position = -1; } } } return; }
static void sdl_fill_audio( void *udata, uint8_t *stream, int len ) { consumer_sdl self = udata; // Get the volume double volume = mlt_properties_get_double( self->properties, "volume" ); pthread_mutex_lock( &self->audio_mutex ); // Block until audio received #ifdef __APPLE__ while ( self->running && len > self->audio_avail ) pthread_cond_wait( &self->audio_cond, &self->audio_mutex ); #endif if ( self->audio_avail >= len ) { // Place in the audio buffer if ( volume != 1.0 ) SDL_MixAudio( stream, self->audio_buffer, len, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) ); else memcpy( stream, self->audio_buffer, len ); // Remove len from the audio available self->audio_avail -= len; // Remove the samples memmove( self->audio_buffer, self->audio_buffer + len, self->audio_avail ); } else { // Just to be safe, wipe the stream first memset( stream, 0, len ); // Mix the audio SDL_MixAudio( stream, self->audio_buffer, self->audio_avail, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) ); // No audio left self->audio_avail = 0; } // We're definitely playing now self->playing = 1; pthread_cond_broadcast( &self->audio_cond ); pthread_mutex_unlock( &self->audio_mutex ); }
/*这个是从网上拷贝的加了缓冲区的音频回调函数, 说实话播放效果没听出有什么不同,不过避免了因为SDL 音频缓冲区过小而导致的段错误*/ void my_play_sdl_audio_callback2(void *userdata, Uint8 *stream, int max_len) { AVCodecContext *aCodecCtx = (AVCodecContext *) userdata; int len, len1, audio_size; static uint8_t audio_buf[(AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2]; static unsigned int audio_buf_size = 0; static unsigned int audio_buf_index = 0; len = max_len; while (len > 0) { if (audio_buf_index >= audio_buf_size) { /* We have already sent all our data; get more */ audio_size = ff_play_getaudiopkt2play((void *) audio_buf, max_len); if (audio_size <= 0) { /* If error, output silence */ audio_buf_size = 1024; // arbitrary? memset(audio_buf, 0, audio_buf_size); } else { audio_buf_size = audio_size; } audio_buf_index = 0; } len1 = audio_buf_size - audio_buf_index; if (len1 > len) len1 = len; SDL_MixAudio(stream,audio_buf,len1,SDL_MIX_MAXVOLUME); //memcpy(stream, (uint8_t *) audio_buf + audio_buf_index, len1); len -= len1; stream += len1; audio_buf_index += len1; } }
/* Play some of a stream previously started with OGG_play() */ int OGG_playAudio(OGG_music *music, Uint8 *snd, int len) { int mixable; while ( (len > 0) && music->playing ) { if ( ! music->len_available ) { OGG_getsome(music); } mixable = len; if ( mixable > music->len_available ) { mixable = music->len_available; } if ( music->volume == MIX_MAX_VOLUME ) { memcpy(snd, music->snd_available, mixable); } else { SDL_MixAudio(snd, music->snd_available, mixable, music->volume); } music->len_available -= mixable; music->snd_available += mixable; len -= mixable; snd += mixable; } return len; }
int AudioHandler::DecodeAudio(uint8_t* audioBuffer) { AVPacket audioPacket; AVFrame *frame = av_frame_alloc(); int frameFinished = 0; int audioDecodedSize, dataSize = 0; while (!isQuit) { packetQueue->Get(&audioPacket); audioDecodedSize = avcodec_decode_audio4(codecContext, frame, &frameFinished, &audioPacket); if(audioDecodedSize < 0) { av_free_packet(&audioPacket); fprintf(stderr, "Failed to decode audio frame\n"); break; } if (frameFinished) { dataSize = av_samples_get_buffer_size(NULL, codecContext->channels, frame->nb_samples, codecContext->sample_fmt, 1); //memcpy(audioBuffer, frame->data[0], dataSize); SDL_MixAudio(audioBuffer, frame->data[0], dataSize, SDL_MIX_MAXVOLUME); UpdateClock(&audioPacket, dataSize); av_free_packet(&audioPacket); } return dataSize; } av_free(frame); return 0; }
void audio_callback(void *userdata, Uint8 *stream, int len) { AVCodecContext *aCodecCtx = (AVCodecContext *)userdata; int len1, audio_size; static uint8_t audio_buf[(AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2]; static unsigned int audio_buf_size = 0; static unsigned int audio_buf_index = 0; while(len > 0) { if(audio_buf_index >= audio_buf_size) { audio_size = audio_decode_frame(aCodecCtx, audio_buf,sizeof(audio_buf)); if(audio_size < 0) { audio_buf_size = 1024; memset(audio_buf, 0, audio_buf_size); } else { audio_buf_size = audio_size; } audio_buf_index = 0; } len1 = audio_buf_size - audio_buf_index; if(len1 > len) len1 = len; SDL_MixAudio(stream, (uint8_t * )audio_buf + audio_buf_index, len1, volume); len -= len1; stream += len1; audio_buf_index += len1; } }
static void my_audio_callback(void *userdata, unsigned char *stream, int len) { if (!l_PluginInit) return; int newsamplerate = OutputFreq * 100 / speed_factor; int oldsamplerate = GameFreq; if (buffer_pos > (len * oldsamplerate) / newsamplerate) { int input_used; if (VolumeControlType == VOLUME_TYPE_SDL) { input_used = resample(buffer, buffer_pos, oldsamplerate, mixBuffer, len, newsamplerate); SDL_MixAudio(stream, mixBuffer, len, VolSDL); } else { input_used = resample(buffer, buffer_pos, oldsamplerate, stream, len, newsamplerate); } memmove(buffer, &buffer[input_used], buffer_pos - input_used); buffer_pos -= input_used; } else { underrun_count++; DebugMessage(M64MSG_VERBOSE, "Audio buffer underrun (%i).",underrun_count); memset(stream , 0, len); buffer_pos = 0; } }
void introAudioCallback(void *userdata, Uint8 *stream, int len) { if (played>=audio_len) return; SDL_MixAudio(stream, &audio_buf[played], played+len>audio_len?audio_len-played:len, volume); played+=len; }
void my_audio_callback(void *userdata, Uint8 *stream, int len) { int newsamplerate = OutputFreq * 100 / speed_factor; int oldsamplerate = GameFreq; if (buffer_pos > (len * oldsamplerate) / newsamplerate) { int input_used; if (VolumeControlType == VOLUME_TYPE_SDL) { input_used = resample(buffer, buffer_pos, oldsamplerate, mixBuffer, len, newsamplerate); SDL_MixAudio(stream, mixBuffer, len, VolSDL); } else { input_used = resample(buffer, buffer_pos, oldsamplerate, stream, len, newsamplerate); } memmove(buffer, &buffer[input_used], buffer_pos - input_used); buffer_pos -= input_used; } else { #ifdef DEBUG underrun_count++; fprintf(stderr, "[JttL's SDL Audio plugin] Debug: Audio buffer underrun (%i).\n",underrun_count); #endif memset(stream , 0, len); buffer_pos = 0; } }
void SDLAudioDriver::PlaySample(AudioFile &file) { int16_t *sampleBuffer; int sampleCount; bool bFreeSampleBuffer = false; if (file.sampleRate != this->obtainedAudioSpec.freq || file.bitsPerSample != 16 || file.isSignedPCM == false) { sampleBuffer = static_cast<int16_t*>(convertAudio(file.channelSamples[0], file.sampleCount, file.sampleRate, file.bitsPerSample, file.isSignedPCM, sampleCount, this->obtainedAudioSpec.freq, 16, true)); bFreeSampleBuffer = true; } else { sampleBuffer = reinterpret_cast<int16_t*>(file.channelSamples[0]); sampleCount = file.sampleCount; bFreeSampleBuffer = false; } { SDL_LockAudio(); if ((int)this->sampleBuffer.size() < sampleCount) { this->sampleBuffer.resize(sampleCount); } SDL_MixAudio((Uint8*)&this->sampleBuffer[0], (const Uint8*)sampleBuffer, sampleCount*2, SDL_MIX_MAXVOLUME); SDL_UnlockAudio(); } if (bFreeSampleBuffer) { delete[] sampleBuffer; } }
/*========================================================================= // Name: AudioCallback() // Desc: Audio callback function (very important!) //=======================================================================*/ static void AudioCallback(void *user_data, Uint8 *audio, int length) { int i; /* Clear audiobuffer */ memset(audio, 0, length); /* Mix all sounds in the array together! */ for (i=0; i<MAX_PLAYING_SOUNDS; i++) { if (sounds[i].active) { /* Only if sound is active... */ Uint8 *sound_buf; Uint32 sound_len; sound_buf = sounds[i].sound->samples; sound_buf += sounds[i].position; if ((sounds[i].position + length) > sounds[i].sound->length) sound_len = sounds[i].sound->length - sounds[i].position; else sound_len = length; /* Mix sound into stream */ SDL_MixAudio(audio, sound_buf, sound_len, VOLUME_PER_SOUND); /* Update sound buffer position */ sounds[i].position += length; /* Sound has reached end? */ if (sounds[i].position >= sounds[i].sound->length) sounds[i].active = 0; } } }
/** * Callback used to provide data to the audio subsystem. * * @param userdata N/A * @param stream Output stream * @param len Length of data to be placed in the output stream */ void audioCallback (void * userdata, unsigned char * stream, int len) { (void)userdata; int count; #ifdef USE_MODPLUG // Read the next portion of music into the audio stream if (musicFile) ModPlug_Read(musicFile, stream, len); #endif for (count = 0; count < nSounds; count++) { if (sounds[count].position >= 0) { // Add the next portion of the sound clip to the audio stream if (len < sounds[count].length - sounds[count].position) { // Play as much of the clip as possible SDL_MixAudio(stream, sounds[count].data + sounds[count].position, len, soundsVolume * SDL_MIX_MAXVOLUME / MAX_VOLUME); sounds[count].position += len; } else { // Play the remainder of the clip SDL_MixAudio(stream, sounds[count].data + sounds[count].position, sounds[count].length - sounds[count].position, soundsVolume * SDL_MIX_MAXVOLUME / MAX_VOLUME); sounds[count].position = -1; } } } return; }
void mixAudio(void* userData, uint8_t* stream, int length) { // play the background music if(gameAudio->musicPlaying.sound == NULL) return; if(gameAudio->musicPlaying.position == gameAudio->musicPlaying.sound->length) gameAudio->musicPlaying.position = 0; uint32_t amount = gameAudio->musicPlaying.sound->length - gameAudio->musicPlaying.position; if(amount > length) amount = length; SDL_MixAudio( stream, &gameAudio->musicPlaying.sound->buffer[gameAudio->musicPlaying.position], amount, (int) (gameSystem->getFloat("audioMusicVolume") * gameAudio->musicPlaying.volume * (float) SDL_MIX_MAXVOLUME) ); gameAudio->musicPlaying.position += amount; // play audio effects in queue size_t i = 0; while(i < gameAudio->effectsPlaying.size()) { if(gameAudio->effectsPlaying[i].position == gameAudio->effectsPlaying[i].sound->length) { gameAudio->effectsPlaying.erase(gameAudio->effectsPlaying.begin() + i); continue; } amount = gameAudio->effectsPlaying[i].sound->length - gameAudio->effectsPlaying[i].position; if(amount > length) amount = length; SDL_MixAudio( stream, &gameAudio->effectsPlaying[i].sound->buffer[gameAudio->effectsPlaying[i].position], amount, (int) (gameSystem->getFloat("audioEffectsVolume") * gameAudio->effectsPlaying[i].volume * (float) SDL_MIX_MAXVOLUME) ); gameAudio->effectsPlaying[i].position += amount; ++i; } }
void callback(void *unused, Uint8 *stream,int len){ Uint32 amnt=s[cur_snd].dlen-s[cur_snd].dpos; if (amnt>len){ amnt=len; } SDL_MixAudio(stream,&s[cur_snd].data[s[cur_snd].dpos],amnt,SDL_MIX_MAXVOLUME); s[cur_snd].dpos=(s[cur_snd].dpos+amnt)%s[cur_snd].dlen; }
void adsrEffect0(int chan, void *stream, int len, void *udata){ int i; Uint8 *out = (Uint8*) stream; float data; struct sid_6581_voice* voice = (struct sid_6581_voice*) udata; float device_silence = 127.0f ; (voice->control & SID_TRIA) ? voice->smpls = voice->osc->triangle : 0 ; (voice->control & SID_SAWT) ? voice->smpls = voice->osc->sawtooth : 0 ; (voice->control & SID_PULS) ? voice->smpls = voice->osc->pulse : 0 ; (voice->control & SID_NOIS) ? voice->smpls = voice->osc->noise : 0 ; if(voice->smp_index < WAVEFORM_LENGTH) { for(i = 0; i < len; i++) { if(!(voice->control & SID_GATE)) { // gate closed if(!voice->env->release_cycle_state){ voice->env->release_cycle_state = 1; voice->env->ads_cycle_state = 0; voice->env->gen_release_cycle_data_counter=0; voice->env->release_cycle_state = 1; printf("change state: ads_cycle_state = 0; release_cycle_state = 1;\n"); } // data = (float) smpls[(int)smp_index] ; // data = ((( 127.0 - (float) smpls[(int)voice->smp_index] ) * gen_release_cycle_data(voice)) ) + 127.0; data = ((( 127.0 - (float) voice->smpls[(int)voice->smp_index] ) * gen_release_cycle_data(voice)) ) + 127.0; } else { // gate open voice->env->release_cycle_state = 0; if(!voice->env->ads_cycle_state){ voice->env->ads_cycle_state = 1; voice->env->gen_ads_cycle_data_adsrcounter=0; printf("change state: ads_cycle_state = 1; release_cycle_state = 0;\n"); } // data = (float) smpls[(int)smp_index] ; // data= ((( 127.0 - (float) smpls[(int)smp_index] ) * gen_ads_cycle_data(*voice)) ) + 127.0; // data= ((( 127.0 - (float) smpls[(int)voice->smp_index] ) * gen_ads_cycle_data(voice)) ) + 127.0; data= ((( 127.0 - (float) voice->smpls[(int)voice->smp_index] ) * gen_ads_cycle_data(voice)) ) + 127.0; } // out[i] = round(data); // out[i] = = round(data); voice->buffer[i] = round(data); voice->smp_index += (int) round( sid2frequency(*voice)/((float)SMPFREQ/(float)WAVEFORM_LENGTH) ); // + freq; // smp_index += (int) round(ton2frequency(tonstufe)/((float)SMPFREQ/(float)WAVEFORM_LENGTH)); // + freq; if(voice->smp_index < 0) exit(4); if(voice->smp_index >= WAVEFORM_LENGTH){ voice->smp_index = ((int)floor(voice->smp_index)) % WAVEFORM_LENGTH; // = 0; // smp_index = smp_index % WAVEFORM_LENGTH; // = 0; } } } SDL_MixAudio(stream, voice->buffer, len, sid.filter->volume * SDL_MIX_MAXVOLUME / (2*15)); // SDL_MixAudio(stream, voice->buffer, len, sid.filter->volume * SDL_MIX_MAXVOLUME / 2); }
void audio_fill(void * udata, Uint8 * stream, int len) { if ( audio_len == 0 ) return; len = ( len > audio_len ? audio_len : len ); SDL_MixAudio(stream, audio_pos, len, SDL_MIX_MAXVOLUME); audio_pos += len; audio_len -= len; }
void SDL_sound_handler::mix(boost::int16_t* outSamples, boost::int16_t* inSamples, unsigned int nSamples, float volume) { Uint8* out = reinterpret_cast<Uint8*>(outSamples); Uint8* in = reinterpret_cast<Uint8*>(inSamples); unsigned int nBytes = nSamples*2; SDL_MixAudio(out, in, nBytes, SDL_MIX_MAXVOLUME*volume); }
void mixaudio(void *unused, Uint8 *stream, int len) { int i; for (i=0; i<2048; i++, alpha+=0.1){ data[i] = sin(alpha/note[np])*32; } SDL_MixAudio(stream, data, 2048, SDL_MIX_MAXVOLUME); np++; if (np==27) np = 0; }
void system_sound_update(int nframes) { int i; int consumed; /* SDL_LockAudio(); */ /* number of unread frames */ i = FRAME_DIFF(sound_frame_write, sound_frame_read); /* number of frames read since last call */ consumed = FRAME_DIFF(sound_frame_read, sound_frame_read_old); sound_frame_read_old = sound_frame_read; /* SDL_UnlockAudio(); */ for (; i<nframes; i++) { if (mute || paused) memset(sound_buffer+sound_frame_write, silence_value, bpf); else { dac_update(dac_data, dac_bpf); /* convert to standard format */ acvt.buf = dac_data; acvt.len = dac_bpf; if (SDL_ConvertAudio(&acvt) == -1) { fprintf(stderr, "DAC data conversion failed: %s\n", SDL_GetError()); return; } /* get sound data */ sound_update((_u16 *)(sound_buffer+sound_frame_write), bpf); /* mix both streams into one */ SDL_MixAudio(sound_buffer+sound_frame_write, dac_data, bpf, SDL_MIX_MAXVOLUME); } sound_frame_write = FRAME_INC(sound_frame_write); if (sound_frame_write == sound_frame_read) { fprintf(stderr, "your machine is much too slow.\n"); /* XXX: handle this */ exit(1); } SDL_SemPost(rsem); } if (nframes > 1) { for (i=0; i<consumed; i++) SDL_SemWait(wsem); } else SDL_SemWait(wsem); }
//Callback void mixaudio(void *unused, Uint8 *stream, int len) { amount = (snd.dlen-snd.dpos); if(amount > len){amount = len;} if(amount == 0){return;} //Nothing to play SDL_MixAudio(stream, &snd.data[snd.dpos], amount, SDL_MIX_MAXVOLUME); snd.dpos += amount; }
/* Audio Callback * The audio function callback takes the following parameters: * stream: A pointer to the audio buffer to be filled * len: The length (in bytes) of the audio buffer * */ void fill_audio(void *udata,Uint8 *stream,int len){ //SDL 2.0 SDL_memset(stream, 0, len); if(audio_len==0) /* Only play if we have data left */ return; len=(len>audio_len?audio_len:len); /* Mix as much data as possible */ SDL_MixAudio(stream,audio_pos,len,SDL_MIX_MAXVOLUME); audio_pos += len; audio_len -= len; }
void fill_audio(void *udata, uint8_t *stream, int len) { int i; if (len > bsize) len = bsize; for(i = 0; i<len; i++) buf[i] = getsample(); SDL_MixAudio(stream, buf, len, SDL_MIX_MAXVOLUME); }
// The audio function callback takes the following parameters: // stream: A pointer to the audio buffer to be filled // len: The length (in bytes) of the audio buffer void SOUND_FillAudio(void *, unsigned char *stream, int len) { // Only play if we have data left if (audio_len == 0) return; // Mix as much data as possible len = (len > audio_len) ? audio_len : len; SDL_MixAudio(stream, audio_pos, len, SDL_MIX_MAXVOLUME); audio_pos += len; audio_len -= len; }
static void sound_play_cb(void *userdata, BYTE *stream, int len) { int length; length = (len > DEFAULT_SAMPLES * SAMPLEALIGN) ?(DEFAULT_SAMPLES * SAMPLEALIGN):len; soundmix_getpcm((short *)sndbuf[nsndbuf], length / SAMPLEALIGN); SDL_MixAudio(stream, sndbuf[nsndbuf], length, SDL_MIX_MAXVOLUME); nsndbuf = (nsndbuf + 1) % NSNDBUF; (void)userdata; }
static void audio_callback(void *userdata, uint8 * stream, int len) { if ((playing!=SDL_AUDIO_PLAYING) || (!userdata)) return; AUDIOPAR *par = (AUDIOPAR *)userdata; if (!par->buffer) return; uint8 *buffer = Atari2HostAddr(par->buffer); if (cvt.needed) { /* Convert Atari audio to host audio */ par->len = (uint32 ) (len / cvt.len_ratio); /* Current buffer too small ? */ if (cvt_buf_len<par->len) { if (cvt.buf) { free(cvt.buf); cvt.buf=NULL; } } /* Allocate needed buffer */ if (cvt.buf==NULL) { cvt.buf=(uint8 *)malloc(par->len); cvt_buf_len = par->len; } memcpy(cvt.buf, buffer, par->len); cvt.len = par->len; SDL_ConvertAudio(&cvt); SDL_MixAudio(stream, cvt.buf, len, par->volume); } else { par->len = len; SDL_MixAudio(stream, buffer, len, par->volume); } TriggerInt5(); // Audio is at interrupt level 5 }
void CPlayer::AudioCallBack( void *udata,Uint8 *stream,int len ) { //SDL 2.0 SDL_memset(stream, 0, len); if(m_audio_len==0) /* Only play if we have data left */ return; len=(len>m_audio_len?m_audio_len:len); /* Mix as much data as possible */ SDL_MixAudio(stream,m_audio_pos,len,SDL_MIX_MAXVOLUME); m_audio_pos += len; m_audio_len -= len; }
void fill_audio(void *udata, Uint8 *stream, int len) { if (is_no_sound) return; if (audio_remain == 0) { audio_remain = audio_len; audio_at = audio_head; } len = (len > (int)audio_remain ? audio_remain : len); SDL_MixAudio(stream, audio_at, len, volume); audio_at += len; audio_remain -= len; }