CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/UI/BackgroundAudio.h
Views: 1401
#pragma once12#include <atomic>3#include <mutex>4#include <string>5#include <vector>67#include "Common/File/Path.h"8#include "Common/UI/Root.h"910class AT3PlusReader;1112struct Sample {13// data must be new[]-ed.14Sample(int16_t *data, int channels, int length, int rateInHz) : channels_(channels), data_(data), length_(length), rateInHz_(rateInHz) {}15~Sample() {16delete[] data_;17}18int16_t *data_;19int length_; // stereo or mono samples.20int rateInHz_; // sampleRate21int channels_;2223static Sample *Load(const std::string &path);24};2526// Mixer for things played on top of everything.27class SoundEffectMixer {28public:29void LoadSamples();3031void Mix(int16_t *buffer, int sz, int sampleRateHz);32void Play(UI::UISound sfx, float volume);3334void UpdateSample(UI::UISound sound, Sample *sample);35void LoadDefaultSample(UI::UISound sound);3637std::vector<std::unique_ptr<Sample>> samples_;3839struct PlayInstance {40UI::UISound sound;41int64_t offset; // 32.32 fixed point42int volume; // 0..25543bool done;44};4546private:47std::mutex mutex_;48std::vector<PlayInstance> queue_;49std::vector<PlayInstance> plays_;50};5152class BackgroundAudio {53public:54BackgroundAudio();55~BackgroundAudio();5657void SetGame(const Path &path);58void Update();59bool Play();6061SoundEffectMixer &SFX() {62return sfxMixer_;63}6465private:66void Clear(bool hard);6768enum {69// 0.5 ms buffer at 44.1 khz should be enough.70BUFSIZE = 22050,71};7273std::mutex mutex_;74Path bgGamePath_;75std::atomic<bool> sndLoadPending_;76int playbackOffset_ = 0;77AT3PlusReader *at3Reader_ = nullptr;78double gameLastChanged_ = 0.0;79double lastPlaybackTime_ = 0.0;80int *buffer = nullptr;81bool fadingOut_ = true;82float volume_ = 0.0f;83float delta_ = -0.0001f;84SoundEffectMixer sfxMixer_;85};8687extern BackgroundAudio g_BackgroundAudio;888990