Path: blob/master/RSDKv5/RSDK/Audio/Audio.hpp
1168 views
#ifndef AUDIO_H1#define AUDIO_H23namespace RSDK4{56#define SFX_COUNT (0x100)7#define CHANNEL_COUNT (0x10)89#define MIX_BUFFER_SIZE (0x800)10#define SAMPLE_FORMAT float1112#define AUDIO_FREQUENCY (44100)13#define AUDIO_CHANNELS (2)1415struct SFXInfo {16RETRO_HASH_MD5(hash);17float *buffer;18size_t length;19int32 playCount;20uint8 maxConcurrentPlays;21uint8 scope;22};2324struct ChannelInfo {25float *samplePtr;26float pan;27float volume;28int32 speed;29size_t sampleLength;30int32 bufferPos;31int32 playIndex;32uint32 loop;33int16 soundID;34uint8 priority;35uint8 state;36};3738enum ChannelStates { CHANNEL_IDLE, CHANNEL_SFX, CHANNEL_STREAM, CHANNEL_LOADING_STREAM, CHANNEL_PAUSED = 0x40 };3940extern SFXInfo sfxList[SFX_COUNT];41extern ChannelInfo channels[CHANNEL_COUNT];4243class AudioDeviceBase44{45public:46static bool32 Init();47static void Release();4849static void ProcessAudioMixing(void *stream, int32 length);5051static void FrameInit();5253static void HandleStreamLoad(ChannelInfo *channel, bool32 async);5455static uint8 initializedAudioChannels;56static uint8 audioState;57static uint8 audioFocus;5859protected:60static void InitAudioChannels();61};6263void UpdateStreamBuffer(ChannelInfo *channel);64void LoadStream(ChannelInfo *channel);65int32 PlayStream(const char *filename, uint32 slot, uint32 startPos, uint32 loopPoint, bool32 loadASync);6667void LoadSfxToSlot(char *filename, uint8 slot, uint8 plays, uint8 scope);68void LoadSfx(char *filePath, uint8 plays, uint8 scope);6970} // namespace RSDK7172#if RETRO_AUDIODEVICE_XAUDIO73#include "XAudio/XAudioDevice.hpp"74#elif RETRO_AUDIODEVICE_PORT75#include "PortAudio/PortAudioDevice.hpp"76#elif RETRO_AUDIODEVICE_MINI77#include "MiniAudio/MiniAudioDevice.hpp"78#elif RETRO_AUDIODEVICE_SDL279#include "SDL2/SDL2AudioDevice.hpp"80#elif RETRO_AUDIODEVICE_OBOE81#include "Oboe/OboeAudioDevice.hpp"82#endif8384namespace RSDK85{8687inline uint16 GetSfx(const char *sfxName)88{89RETRO_HASH_MD5(hash);90GEN_HASH_MD5(sfxName, hash);9192for (int32 s = 0; s < SFX_COUNT; ++s) {93if (HASH_MATCH_MD5(sfxList[s].hash, hash))94return s;95}9697return -1;98}99int32 PlaySfx(uint16 sfx, uint32 loopPoint, uint32 priority);100inline void StopSfx(uint16 sfx)101{102#if !RETRO_USE_ORIGINAL_CODE103LockAudioDevice();104#endif105106for (int32 i = 0; i < CHANNEL_COUNT; ++i) {107if (channels[i].soundID == sfx) {108MEM_ZERO(channels[i]);109channels[i].soundID = -1;110channels[i].state = CHANNEL_IDLE;111}112}113114#if !RETRO_USE_ORIGINAL_CODE115UnlockAudioDevice();116#endif117}118119#if RETRO_REV0U120inline void StopAllSfx()121{122#if !RETRO_USE_ORIGINAL_CODE123LockAudioDevice();124#endif125126for (int32 i = 0; i < CHANNEL_COUNT; ++i) {127if (channels[i].state == CHANNEL_SFX) {128MEM_ZERO(channels[i]);129channels[i].soundID = -1;130channels[i].state = CHANNEL_IDLE;131}132}133134#if !RETRO_USE_ORIGINAL_CODE135UnlockAudioDevice();136#endif137}138#endif139140void SetChannelAttributes(uint8 channel, float volume, float panning, float speed);141142inline void StopChannel(uint32 channel)143{144if (channel < CHANNEL_COUNT) {145if (channels[channel].state != CHANNEL_LOADING_STREAM)146channels[channel].state = CHANNEL_IDLE;147}148}149150inline void PauseChannel(uint32 channel)151{152if (channel < CHANNEL_COUNT) {153if (channels[channel].state != CHANNEL_LOADING_STREAM)154channels[channel].state |= CHANNEL_PAUSED;155}156}157158inline void ResumeChannel(uint32 channel)159{160if (channel < CHANNEL_COUNT) {161if (channels[channel].state != CHANNEL_LOADING_STREAM)162channels[channel].state &= ~CHANNEL_PAUSED;163}164}165166inline void PauseSound()167{168for (int32 c = 0; c < CHANNEL_COUNT; ++c) PauseChannel(c);169}170171inline void ResumeSound()172{173for (int32 c = 0; c < CHANNEL_COUNT; ++c) ResumeChannel(c);174}175176inline bool32 SfxPlaying(uint16 sfx)177{178for (int32 c = 0; c < CHANNEL_COUNT; ++c) {179if (channels[c].state == CHANNEL_SFX && channels[c].soundID == sfx)180return true;181}182return false;183}184185inline bool32 ChannelActive(uint32 channel)186{187if (channel >= CHANNEL_COUNT)188return false;189else190return (channels[channel].state & 0x3F) != CHANNEL_IDLE;191}192193uint32 GetChannelPos(uint32 channel);194double GetVideoStreamPos();195196void ClearStageSfx();197#if RETRO_USE_MOD_LOADER198void ClearGlobalSfx();199#endif200201#if RETRO_REV0U202#include "Legacy/AudioLegacy.hpp"203#endif204205} // namespace RSDK206207#endif208209210