Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "../include/kit_sdl2/kit_kmixer.h"
- #include "../_private/include/_kit_privmacro.h"
- #include "../_private/include/_kit_kmixerAllPrivate.h"
- void _kit_kmixerAsyncRemoveCallback(void* userdata){ if(userdata != NULL) SDL_free(userdata); }
- void _kit_kmixerAsyncTrackCallback(void* userdata, void* _stream, int size, SDL_bool hasInput){
- _kit_kmixerAsyncTrack* track = userdata;
- kit_acodecPCM* pcm = track->pcm;
- //if pcm is NULL, the clip has finished playing, and will be deactivated by the main voice
- if(pcm == NULL){ kit_coreMemset(_stream,0,size); return; }
- kit_kmixerVoiceSpec vspec = track->voice->spec; //make copy of voice spec
- if(track->timeStamp){
- //loops will count down to 0, whereupon the clip will finish (-1 for endless loop)
- track->loops = pcm->loopCount;
- //initialize previous sample to NAN, to indicate that it should be set later
- union { Uint32 u32; float f32; } not_a_number = { .u32 = 0x7FC00000 };
- track->prevSample.l = not_a_number.f32;
- //calculate position based on difference between device and clip timestamps
- Uint64 trackTimeStamp = track->timeStamp;
- Uint64 deviceTimeStamp = *track->deviceTimeStamp; // = device->_timeStampEnd
- //device->_timeStampEnd is used instead of Start, so the clip's time stamp
- //can be compared to the last time the device callback exited
- double difference = (double)(trackTimeStamp-deviceTimeStamp)/1000;
- difference *= vspec.freq; //convert seconds to samples
- track->position = -(difference*track->speed); //starts playing when position reaches 0
- track->timeStamp = 0; //to ensure that this only occurs once per clip queued
- }
- float pan = track->pan;
- double position = track->position;
- float speed = track->speed;
- Uint16 loops = track->loops;
- Uint64 loopStart = pcm->loopStart;
- Uint64 loopEnd = pcm->loopEnd;
- //Uint64 src_len = pcm->numSamples; (unnecessary, as loopEnd is used instead)
- Uint16 dst_len = size / (sizeof(float)<<vspec.stereo);
- if(vspec.stereo){
- kit_acodecPCM_F32S* dst = _stream, *src = pcm->f32s;
- kit_acodecPCM_F32S prevSample = track->prevSample;
- //(set prevSample to src[0] if track's prevSample is NaN)
- if(track->prevSample.l != track->prevSample.l) prevSample = src[0];
- if(track->linear){ //linear resampling
- for(Uint32 i=0; i<dst_len; ++i) _stereo_iteration(_stereo_linear)
- } else { //nearest-neighbor resampling
- for(Uint32 i=0; i<dst_len; ++i) _stereo_iteration(_stereo_nearest)
- }
- track->prevSample = prevSample;
- } else { //mono
- float* dst = _stream, *src = pcm->f32;
- float prevSample = track->prevSample.l;
- //(set prevSample to src[0] if track's prevSample is NaN)
- if(track->prevSample.l != track->prevSample.l) prevSample = src[0];
- if(track->linear){ //linear resampling
- for(Uint32 i=0; i<dst_len; ++i) _mono_iteration(_mono_linear)
- } else { //nearest-neighbor resampling
- for(Uint32 i=0; i<dst_len; ++i) _mono_iteration(_mono_nearest)
- }
- track->prevSample.l = prevSample;
- }
- //update some values
- track->pcm = pcm; //will either set track->pcm to itself, or NULL
- track->position = position;
- track->speed = speed;
- track->loops = loops;
- //if the track's stopOnMute condition is met, mark track as finished,
- //so it'll be deactivated until a new clip is queued
- if(!vspec.stereo) track->volume.r = track->volume.l;
- if((track->volume.l<=0 && track->volume.r<=0) || track->speed<=0){
- if(track->stopOnMute) track->pcm = NULL;
- }
- }
- void _kit_kmixerAsyncVoiceCallback(void* _userdata, void* _stream, int size, SDL_bool hasInput){
- _kit_kmixerAsyncUserdata* userdata = _userdata;
- float pan = userdata->pan;
- SDL_bool stereo = userdata->voice->spec.stereo;
- Uint16 dst_len = size / (sizeof(float)<<stereo);
- if(stereo){
- kit_acodecPCM_F32S* dst = _stream;
- for(Uint32 i=0; i<dst_len; ++i){
- kit_acodecPCM_F32S sample = _stereo_volume(dst[i], (void*)userdata);
- dst[i] = _apply_pan(_stereo_clamp(sample), pan);
- _stereo_deltaVol((void*)userdata);
- }
- } else { //mono
- float* dst = _stream;
- for(Uint32 i=0; i<dst_len; ++i){
- dst[i] = _mono_clamp(_mono_volume(dst[i],(void*)userdata));
- _mono_deltaVol((void*)userdata);
- }
- }
- Uint32 numTracks = userdata->numTracks;
- _kit_kmixerAsyncTrack* tracks = userdata->tracks;
- //deactivate all active tracks if main voice stopOnMute condition is met
- if(!stereo) userdata->volume.r = userdata->volume.l;
- if(userdata->volume.l<=0 && userdata->volume.r<=0 && userdata->stopOnMute){
- for(Uint32 ti=0; ti<numTracks; ++ti){
- _kit_kmixerAsyncTrack* track = &tracks[ti];
- _deactivate_track_if(track->pcm!=NULL, track);
- }
- //otherwise, just deactivate the tracks that have finished playing its queued clip
- } else {
- for(Uint32 ti=0; ti<numTracks; ++ti){
- _kit_kmixerAsyncTrack* track = &tracks[ti];
- _kit_kmixerVoice* voice = track->voice;
- if(voice == NULL) return;
- if(voice->lock == NULL) return;
- _deactivate_track_if(track->pcm==NULL && voice->active, track);
- }
- }
- }
- Uint32 kit_kmixerAsyncAdd(kit_kmixerDevice* device,
- SDL_bool linearInterpolation, SDL_bool stereo,
- Uint32 outputVoiceID, Uint32 numTracks)
- {
- Uint32 mainVoiceID = 0; //0 for error by default
- _DEVICE_VALIDITY_CHECK(0)
- _IF_SDLERR(outputVoiceID>=device->_raw->x,;,"outputVoiceID out of bounds")
- _IF_SDLERR(numTracks==0,;,"!numTracks")
- kit_coreVector** raw_p = &device->_raw;
- size_t userdataSize = sizeof(_kit_kmixerAsyncUserdata);
- userdataSize += sizeof(_kit_kmixerAsyncTrack)*numTracks;
- //create main voice
- kit_kmixerVoiceSpec vspec = VECTOR_INDEX_C(_kit_kmixerVoice, *raw_p, 0).spec;
- vspec.remove = _kit_kmixerAsyncRemoveCallback;
- vspec.callback = _kit_kmixerAsyncVoiceCallback;
- _IF_GOTO_ERROR(kit_coreRealloc(&vspec.userdata,0,userdataSize),;)
- vspec.stereo = stereo&1;
- mainVoiceID = kit_kmixerVoiceAdd(device,&vspec,outputVoiceID);
- _IF_GOTO_ERROR(!mainVoiceID, SDL_free(vspec.userdata) )
- VECTOR_INDEX_C(_kit_kmixerVoice, *raw_p, mainVoiceID).active = SDL_FALSE;
- //fill in userdata
- _kit_kmixerAsyncUserdata* userdata = vspec.userdata;
- userdata->type = type_UDAT; //U[SER]DAT[A]
- userdata->volume.l = 1.0f;
- userdata->volume.r = 1.0f;
- userdata->numTracks = numTracks;
- userdata->voice = &VECTOR_INDEX_C(_kit_kmixerVoice, *raw_p, mainVoiceID);
- userdata->tracks = (void*)userdata+sizeof(_kit_kmixerAsyncUserdata);
- //create tracks
- vspec.remove = NULL; //only the main voice should be doing the free()'ing
- vspec.callback = _kit_kmixerAsyncTrackCallback;
- _kit_kmixerAsyncTrack* tracks = userdata->tracks;
- for(Uint32 ti=0; ti<numTracks; ++ti){
- _kit_kmixerAsyncTrack* track = vspec.userdata = &tracks[ti];
- track->type = type_TRCK; //TR[A]CK
- track->volume.l = 1.0f;
- track->volume.r = 1.0f;
- track->linear = linearInterpolation&1;
- track->deviceTimeStamp = &device->_timeStampEnd;
- track->rawIndex = kit_kmixerVoiceAdd(device,&vspec,mainVoiceID);
- if(!track->rawIndex){ kit_kmixerVoiceRemove(device, mainVoiceID); return 0; }
- track->voice = &VECTOR_INDEX_C(_kit_kmixerVoice, *raw_p, track->rawIndex);
- kit_kmixerVoiceSetActive(device, track->rawIndex, SDL_FALSE);
- }
- _noerr_:
- _error_:
- return mainVoiceID;
- }
- Uint32 kit_kmixerAsyncPlayPVS(kit_kmixerDevice* device, Uint32 voiceID, kit_acodecPCM* pcm,
- float pan, float volumeL, float volumeR, float speedMultiplier)
- {
- Uint64 trackTimeStamp = SDL_GetTicks64();
- Uint32 trackNum = -1; //set to -1 for error by default
- _DEVICE_VALIDITY_CHECK(0)
- _IF_SDLERR(voiceID>=device->_raw->x,;,"voiceID out of bounds")
- _IF_SDLERR(pcm==NULL,;,"!pcm")
- _IF_SDLERR(!NORMALIZED(pan),;,"pan must be -1.0f -> 1.0f")
- _IF_SDLERR(volumeL<0,;,"volumeL < 0")
- _IF_SDLERR(volumeR<0,;,"volumeR < 0")
- _IF_SDLERR(speedMultiplier<=0,;,"speedMultiplier <= 0")
- kit_coreVector** raw_p = &device->_raw;
- _kit_kmixerVoice* voice = &VECTOR_INDEX_C(_kit_kmixerVoice, *raw_p, voiceID);
- _IF_SDLERR(voice->lock==NULL,;,"voice does not exist")
- kit_kmixerVoiceSpec vspec = voice->spec;
- _kit_kmixerAsyncUserdata* userdata = vspec.userdata;
- _IF_SDLERR(userdata==NULL,;,"invalid async voice")
- _IF_SDLERR(userdata->type!=type_UDAT,;,"invalid async voice")
- _IF_SDLERR(pcm->magic!=KPCM_MAGIC,;,"invalid pcm struct")
- _IF_SDLERR(pcm->format!=AUDIO_F32,;,"pcm->format != AUDIO_F32")
- _IF_SDLERR(pcm->loopStart>=pcm->loopEnd,;,"pcm->loopStart >= pcm->loopEnd")
- _IF_SDLERR(pcm->loopEnd>pcm->numSamples,;,"pcm->loopEnd > pcm->numSamples")
- _IF_SDLERR(pcm->numSamples<2,;,"pcm->numSamples < 2")
- _IF_SDLERR(pcm->sampleRate!=vspec.freq,;,"pcm->sampleRate != device freq")
- _IF_SDLERR((pcm->channels-1)!=vspec.stereo,;,"pcm->channels is invalid")
- //look for empty track to queue pcm clip with
- Uint32 numTracks = userdata->numTracks;
- _kit_kmixerAsyncTrack* tracks = userdata->tracks;
- for(Uint32 i=0; i<numTracks; ++i){
- if(tracks[i].pcm == NULL){ trackNum = i; break; }
- }
- _IF_GOTO(trackNum==-1,_noerr_,;) //exit early if no free track was found
- _kit_kmixerAsyncTrack* track = &tracks[trackNum];
- track->pan = pan;
- track->volume.l = volumeL;
- track->volume.r = volumeR;
- track->delta.l = 0.0f;
- track->delta.r = 0.0f;
- track->stopOnMute = SDL_TRUE;
- track->pcm = pcm;
- track->timeStamp = trackTimeStamp;
- track->speed = speedMultiplier;
- track->deltaS = 0.0f;
- track->voice->active = SDL_TRUE;
- kit_kmixerVoiceSetActive(device, track->rawIndex, SDL_TRUE);
- if(!kit_kmixerVoiceGetActive(device, voiceID)) kit_kmixerVoiceSetActive(device, voiceID, SDL_TRUE);
- _noerr_: if(trackNum == -1) trackNum = -2; //-2 for 'no available track found; no error'
- _error_: return trackNum;
- }
- Uint32 kit_kmixerAsyncGetActiveTracks(kit_kmixerDevice* device, Uint32 voiceID){
- Uint32 activeTracks = -1; //-1 for error by default
- _DEVICE_VALIDITY_CHECK(0)
- _IF_SDLERR(voiceID>=device->_raw->x,;,"voiceID out of bounds")
- kit_coreVector** raw_p = &device->_raw;
- _kit_kmixerVoice* voice = &VECTOR_INDEX_C(_kit_kmixerVoice, *raw_p, voiceID);
- _IF_SDLERR(voice->lock==NULL,;,"voice does not exist")
- kit_kmixerVoiceSpec vspec = voice->spec;
- _kit_kmixerAsyncUserdata* userdata = vspec.userdata;
- _IF_SDLERR(userdata==NULL,;,"invalid async voice")
- _IF_SDLERR(userdata->type!=type_UDAT,;,"invalid async voice")
- activeTracks = 0; //there are no errors past this, so setting to 0 is safe here
- Uint32 numTracks = userdata->numTracks;
- _kit_kmixerAsyncTrack* tracks = userdata->tracks;
- for(Uint32 i=0; i<numTracks; ++i){
- if(tracks[i].pcm != NULL) ++activeTracks;
- }
- _noerr_:
- _error_: return activeTracks;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement