ft2_audio.h (4514B)
1 #pragma once 2 3 #include <stdint.h> 4 #include <stdbool.h> 5 #include <SDL2/SDL.h> 6 #include "ft2_replayer.h" 7 8 enum 9 { 10 FREQ_TABLE_LINEAR = 0, 11 FREQ_TABLE_AMIGA = 1, 12 }; 13 14 #define DEFAULT_AUDIO_FREQ 48000 15 16 #define MIN_AUDIO_FREQ 44100 17 #define MAX_AUDIO_FREQ 96000 18 19 #define MAX_AUDIO_DEVICES 99 20 21 // more bits makes little sense here 22 23 #define BPM_FRAC_BITS 52 24 #define BPM_FRAC_SCALE (1ULL << BPM_FRAC_BITS) 25 #define BPM_FRAC_MASK (BPM_FRAC_SCALE-1) 26 27 #define TICK_TIME_FRAC_BITS 52 28 #define TICK_TIME_FRAC_SCALE (1ULL << TICK_TIME_FRAC_BITS) 29 #define TICK_TIME_FRAC_MASK (TICK_TIME_FRAC_SCALE-1) 30 31 // for audio/video sync queue. (2^n-1 - don't change this! Queue buffer is already BIG in size) 32 #define SYNC_QUEUE_LEN 4095 33 34 typedef struct audio_t 35 { 36 char *currInputDevice, *currOutputDevice, *lastWorkingAudioDeviceName; 37 char *inputDeviceNames[MAX_AUDIO_DEVICES], *outputDeviceNames[MAX_AUDIO_DEVICES]; 38 volatile bool locked, resetSyncTickTimeFlag, volumeRampingFlag; 39 bool linearPeriodsFlag, rescanAudioDevicesSupported, sincInterpolation; 40 volatile uint8_t interpolationType; 41 int32_t inputDeviceNum, outputDeviceNum, lastWorkingAudioFreq, lastWorkingAudioBits; 42 uint32_t quickVolRampSamples, freq; 43 44 uint32_t tickSampleCounter, samplesPerTickInt, samplesPerTickIntTab[(MAX_BPM-MIN_BPM)+1]; 45 uint64_t tickSampleCounterFrac, samplesPerTickFrac, samplesPerTickFracTab[(MAX_BPM-MIN_BPM)+1]; 46 47 uint32_t audLatencyPerfValInt, tickTimeIntTab[(MAX_BPM-MIN_BPM)+1]; 48 uint64_t audLatencyPerfValFrac, tickTimeFracTab[(MAX_BPM-MIN_BPM)+1]; 49 50 uint64_t tickTime64, tickTime64Frac; 51 52 float *fMixBufferL, *fMixBufferR, fQuickVolRampSamplesMul, fSamplesPerTickIntMul; 53 double dHz2MixDeltaMul; 54 55 SDL_AudioDeviceID dev; 56 uint32_t wantFreq, haveFreq, wantSamples, haveSamples; 57 } audio_t; 58 59 typedef struct 60 { 61 const int8_t *base8, *revBase8; 62 const int16_t *base16, *revBase16; 63 bool active, samplingBackwards, isFadeOutVoice, hasLooped; 64 uint8_t scopeVolume, mixFuncOffset, panning, loopType; 65 int32_t position, sampleEnd, loopStart, loopLength; 66 uint32_t volumeRampLength; 67 uint64_t positionFrac, delta, scopeDelta; 68 69 // if (loopEnabled && hasLooped && samplingPos <= loopStart+MAX_LEFT_TAPS) readFixedTapsFromThisPointer(); 70 const int8_t *leftEdgeTaps8; 71 const int16_t *leftEdgeTaps16; 72 73 const float *fSincLUT; 74 float fVolume, fCurrVolumeL, fCurrVolumeR, fVolumeLDelta, fVolumeRDelta, fTargetVolumeL, fTargetVolumeR; 75 } voice_t; 76 77 #ifdef _MSC_VER 78 #pragma pack(push) 79 #pragma pack(1) 80 #endif 81 typedef struct pattSyncData_t // used for audio/video sync queue (pack to save RAM) 82 { 83 uint8_t pattNum, globalVolume, songPos, tick, speed, row, BPM; 84 uint64_t timestamp; 85 } 86 #ifdef __GNUC__ 87 __attribute__ ((packed)) 88 #endif 89 pattSyncData_t; 90 #ifdef _MSC_VER 91 #pragma pack(pop) 92 #endif 93 94 typedef struct pattSync_t 95 { 96 volatile int32_t readPos, writePos; 97 pattSyncData_t data[SYNC_QUEUE_LEN+1]; 98 } pattSync_t; 99 100 typedef struct chSyncData_t 101 { 102 syncedChannel_t channels[MAX_CHANNELS]; 103 uint64_t timestamp; 104 } chSyncData_t; 105 106 typedef struct chSync_t 107 { 108 volatile int32_t readPos, writePos; 109 chSyncData_t data[SYNC_QUEUE_LEN+1]; 110 } chSync_t; 111 112 int32_t pattQueueReadSize(void); 113 int32_t pattQueueWriteSize(void); 114 bool pattQueuePush(pattSyncData_t t); 115 bool pattQueuePop(void); 116 bool pattQueuePop(void); 117 pattSyncData_t *pattQueuePeek(void); 118 uint64_t getPattQueueTimestamp(void); 119 int32_t chQueueReadSize(void); 120 int32_t chQueueWriteSize(void); 121 bool chQueuePush(chSyncData_t t); 122 bool chQueuePop(void); 123 chSyncData_t *chQueuePeek(void); 124 uint64_t getChQueueTimestamp(void); 125 void resetSyncQueues(void); 126 127 void decreaseMasterVol(void); 128 void increaseMasterVol(void); 129 130 void calcPanningTable(void); 131 void setAudioAmp(int16_t amp, int16_t masterVol, bool bitDepth32Flag); 132 void setNewAudioFreq(uint32_t freq); 133 void setBackOldAudioFreq(void); 134 void setMixerBPM(int32_t bpm); 135 void audioSetVolRamp(bool volRamp); 136 void audioSetInterpolationType(uint8_t interpolationType); 137 void stopVoice(int32_t i); 138 bool setupAudio(bool showErrorMsg); 139 void closeAudio(void); 140 void pauseAudio(void); 141 void resumeAudio(void); 142 bool setNewAudioSettings(void); 143 void lockAudio(void); 144 void unlockAudio(void); 145 void lockMixerCallback(void); 146 void unlockMixerCallback(void); 147 void resetRampVolumes(void); 148 void updateVoices(void); 149 void mixReplayerTickToBuffer(uint32_t samplesToMix, void *stream, uint8_t bitDepth); 150 151 // in ft2_audio.c 152 extern audio_t audio; 153 extern pattSyncData_t *pattSyncEntry; 154 extern chSyncData_t *chSyncEntry; 155 extern chSync_t chSync; 156 extern pattSync_t pattSync; 157 158 extern volatile bool pattQueueClearing, chQueueClearing;