-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from lnd3/version-0.1.4
Upcoming changes for version 0.1.4
- Loading branch information
Showing
101 changed files
with
7,286 additions
and
1,449 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule ldeps
updated
8 files
+3 −0 | .gitmodules | |
+2 −1 | CMakeLists.txt | |
+5 −2 | src/CMakeLists.txt | |
+93,928 −0 | src/various/include/various/miniaudio.h | |
+34 −2 | thirdparty/CMakeLists.txt | |
+1 −1 | thirdparty/bs | |
+1 −0 | thirdparty/portaudio/portaudio | |
+0 −2 | thirdparty_mods/CMakeLists.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
project (audio) | ||
|
||
set(deps | ||
logging | ||
testing | ||
memory | ||
math | ||
) | ||
|
||
bs_generate_package(audio "tier1" "${deps}" "PortAudio") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#pragma once | ||
|
||
#include <functional> | ||
|
||
namespace l::audio { | ||
float GetFrequencyFromNote(float note); | ||
double GetPhaseModifier(double note, double modifier); | ||
float BatchUpdate(float updateSamples, float samplesLeft, int32_t start, int32_t end, std::function<void()> update, std::function<void(int32_t, int32_t, bool)> process); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
#pragma once | ||
|
||
#include <mutex> | ||
#include <atomic> | ||
#include <condition_variable> | ||
#include <semaphore> | ||
#include <vector> | ||
#include <unordered_map> | ||
|
||
#include "../../include/portaudio.h" | ||
|
||
namespace l::audio { | ||
|
||
struct AudioStreamData { | ||
double mDacOutputTimeAtLastCallback = 0.0; | ||
int32_t mSampleRate = 0; | ||
int32_t mNumBufferParts = 0; | ||
int32_t mTotalNumFrames = 0; | ||
int32_t mTotalBufferSize = 0; | ||
int32_t mFramePosition = 0; | ||
int32_t mDacFramesPerBufferPart = 0; | ||
int32_t mNumChannels = 0; | ||
float* mBuffer = nullptr; | ||
std::binary_semaphore mDacWriteReady{ 0 }; | ||
|
||
float* GetCurrentBufferPosition() { | ||
auto framePos = mFramePosition; | ||
return mBuffer + framePos * mNumChannels; | ||
} | ||
|
||
void NextPart() { | ||
// go to the part just before the one being written | ||
auto framesAhead = mDacFramesPerBufferPart * (mNumBufferParts - 1); | ||
mFramePosition = (mFramePosition + framesAhead) % mTotalNumFrames; | ||
} | ||
|
||
void NextPartCanBeWritten() { | ||
mDacWriteReady.release(); | ||
} | ||
|
||
bool CanWrite() { | ||
return mDacWriteReady.try_acquire(); | ||
} | ||
|
||
int32_t GetPartTotalSize() { | ||
return mDacFramesPerBufferPart * mNumChannels; | ||
} | ||
|
||
int32_t GetNumFramesPerPart() { | ||
return mDacFramesPerBufferPart; | ||
} | ||
|
||
int32_t GetSampleRate() { | ||
return mSampleRate; | ||
} | ||
}; | ||
|
||
typedef void PaStream; | ||
|
||
enum class ChannelMode { | ||
MONO, | ||
STEREO | ||
}; | ||
|
||
enum class BufferingMode { | ||
SINGLE_BUFFERING = 0, | ||
DOUBLE_BUFFERING, | ||
TRIPLE_BUFFERING | ||
}; | ||
|
||
class AudioStream { | ||
public: | ||
AudioStream() = default; | ||
~AudioStream() = default; | ||
|
||
bool OpenStream(int32_t dacBufferFrames, float latency = 0.0f, BufferingMode mode = BufferingMode::DOUBLE_BUFFERING, ChannelMode channel = ChannelMode::STEREO); | ||
bool StartStream(); | ||
std::vector<float>& GetWriteBuffer(); | ||
bool CanWrite(); | ||
void Write(); | ||
bool StopStream(); | ||
int32_t GetPartTotalSize(); | ||
int32_t GetNumFramesPerPart(); | ||
int32_t GetSampleRate(); | ||
protected: | ||
PaStreamParameters mOutputParameters; | ||
PaStream* mPaStream; | ||
|
||
AudioStreamData mAudioStreamData; | ||
std::vector<float> mOutputBufferInterleaved; | ||
std::vector<float> mWriteBuffer; | ||
}; | ||
|
||
class AudioManager { | ||
public: | ||
AudioManager() = default; | ||
~AudioManager(); | ||
|
||
bool Init(); | ||
AudioStream* GetStream(std::string_view name); | ||
void CloseStream(std::string_view name); | ||
protected: | ||
std::unordered_map<size_t, std::unique_ptr<AudioStream>> mStreams; | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include "audio/AudioUtils.h" | ||
|
||
#include "logging/LoggingAll.h" | ||
#include "math/MathAll.h" | ||
|
||
#include "math/MathFunc.h" | ||
|
||
#include <math.h> | ||
|
||
namespace l::audio { | ||
|
||
float GetFrequencyFromNote(float note) { | ||
return 440.0f * l::math::functions::pow(2.0f, (note - 49.0f) / 12.0f); | ||
} | ||
|
||
double GetPhaseModifier(double note, double modifier) { | ||
double limit = 1.0 / l::math::functions::max(note / 25.0, 1.0); | ||
return 800.0 * modifier * modifier * limit; | ||
} | ||
|
||
float BatchUpdate(float updateSamples, float samplesLeft, int32_t start, int32_t end, std::function<void()> update, std::function<void(int32_t, int32_t, bool)> process) { | ||
float startNum = static_cast<float>(start); | ||
while (startNum < static_cast<float>(end)) { | ||
bool updated = false; | ||
if (samplesLeft < 1.0f) { | ||
samplesLeft += updateSamples; | ||
if (update != nullptr) { | ||
update(); | ||
} | ||
updated = true; | ||
} | ||
|
||
// Update stuff | ||
// process samples until | ||
// * batch max has been reached | ||
// * end has been reached | ||
|
||
float samples = l::math::functions::min(static_cast<float>(end) - startNum, samplesLeft); | ||
if (process != nullptr) { | ||
process(static_cast<int32_t>(startNum), static_cast<int32_t>(startNum + samples), updated); | ||
} | ||
startNum += samples; | ||
samplesLeft -= samples; | ||
} | ||
return samplesLeft; | ||
} | ||
|
||
} |
Oops, something went wrong.