tab->space file normalization

pull/38/head
dimitri 7 years ago
parent dafe5969ae
commit 49ab009115

@ -37,12 +37,12 @@ static int rt_callback(void * output_buffer, void * input_buffer, uint32_t num_b
{ {
if (status) std::cerr << "[rtaudio] buffer over or underflow" << std::endl; if (status) std::cerr << "[rtaudio] buffer over or underflow" << std::endl;
// Playback // Playback
if (buffer.getAvailableRead()) buffer.read((float*) output_buffer, BUFFER_LENGTH); if (buffer.getAvailableRead()) buffer.read((float*) output_buffer, BUFFER_LENGTH);
else memset(output_buffer, 0, BUFFER_LENGTH * sizeof(float)); else memset(output_buffer, 0, BUFFER_LENGTH * sizeof(float));
// Recording // Recording
if (record_buffer.getAvailableWrite()) record_buffer.write((float*) input_buffer, BUFFER_LENGTH / 2); if (record_buffer.getAvailableWrite()) record_buffer.write((float*) input_buffer, BUFFER_LENGTH / 2);
return 0; return 0;
} }
@ -62,9 +62,9 @@ AudioDevice::~AudioDevice()
{ {
rtaudio->stopStream(); rtaudio->stopStream();
if (rtaudio->isStreamOpen()) if (rtaudio->isStreamOpen())
{ {
rtaudio->closeStream(); rtaudio->closeStream();
} }
} }
} }
@ -77,7 +77,7 @@ bool AudioDevice::Open(const int deviceId)
outputParams.nChannels = info.numChannels; outputParams.nChannels = info.numChannels;
outputParams.firstChannel = 0; outputParams.firstChannel = 0;
RtAudio::StreamParameters inputParams; RtAudio::StreamParameters inputParams;
inputParams.deviceId = rtaudio->getDefaultInputDevice(); inputParams.deviceId = rtaudio->getDefaultInputDevice();
inputParams.nChannels = 1; inputParams.nChannels = 1;
inputParams.firstChannel = 0; inputParams.firstChannel = 0;
@ -129,21 +129,21 @@ bool AudioDevice::Play(const std::vector<float> & data)
bool AudioDevice::Record(const uint32_t lengthInSamples, std::vector<float> & recordingBuffer) bool AudioDevice::Record(const uint32_t lengthInSamples, std::vector<float> & recordingBuffer)
{ {
uint64_t recordedSamples = 0; uint64_t recordedSamples = 0;
// Allocate memory upfront (revisit this later...) // Allocate memory upfront (revisit this later...)
recordingBuffer.resize(lengthInSamples + (BUFFER_LENGTH)); // + a little padding recordingBuffer.resize(lengthInSamples + (BUFFER_LENGTH)); // + a little padding
while (recordedSamples < lengthInSamples) while (recordedSamples < lengthInSamples)
{ {
if (record_buffer.getAvailableRead()) if (record_buffer.getAvailableRead())
{ {
if (record_buffer.read(recordingBuffer.data() + recordedSamples, BUFFER_LENGTH / 2)) if (record_buffer.read(recordingBuffer.data() + recordedSamples, BUFFER_LENGTH / 2))
{ {
recordedSamples += (BUFFER_LENGTH / 2); recordedSamples += (BUFFER_LENGTH / 2);
} }
} }
} }
return true; return true;
} }

@ -39,27 +39,27 @@ static const int32_t BUFFER_LENGTH = FRAME_SIZE * CHANNELS;
struct AudioDeviceInfo struct AudioDeviceInfo
{ {
uint32_t id; uint32_t id;
uint32_t numChannels; uint32_t numChannels;
uint32_t sampleRate; uint32_t sampleRate;
uint32_t frameSize; uint32_t frameSize;
bool isPlaying = false; bool isPlaying = false;
}; };
class AudioDevice class AudioDevice
{ {
std::unique_ptr<RtAudio> rtaudio; std::unique_ptr<RtAudio> rtaudio;
protected: protected:
AudioDevice(const AudioDevice& r) = delete; AudioDevice(const AudioDevice& r) = delete;
AudioDevice & operator = (const AudioDevice& r) = delete; AudioDevice & operator = (const AudioDevice& r) = delete;
public: public:
AudioDeviceInfo info; AudioDeviceInfo info;
AudioDevice(int numChannels, int sampleRate, int deviceId = -1); AudioDevice(int numChannels, int sampleRate, int deviceId = -1);
~AudioDevice(); ~AudioDevice();
static void ListAudioDevices(); static void ListAudioDevices();
bool Open(const int deviceId); bool Open(const int deviceId);
bool Play(const std::vector<float> & data); bool Play(const std::vector<float> & data);
bool Record(const uint32_t lengthInSamples, std::vector<float> & recordingBuffer); bool Record(const uint32_t lengthInSamples, std::vector<float> & recordingBuffer);
}; };
#endif #endif

@ -17,77 +17,77 @@ using namespace nqr;
int main(int argc, const char **argv) try int main(int argc, const char **argv) try
{ {
AudioDevice::ListAudioDevices(); AudioDevice::ListAudioDevices();
const int desiredSampleRate = 44100; const int desiredSampleRate = 44100;
const int desiredChannelCount = 2; const int desiredChannelCount = 2;
AudioDevice myDevice(desiredChannelCount, desiredSampleRate); AudioDevice myDevice(desiredChannelCount, desiredSampleRate);
myDevice.Open(myDevice.info.id); myDevice.Open(myDevice.info.id);
std::shared_ptr<AudioData> fileData = std::make_shared<AudioData>(); std::shared_ptr<AudioData> fileData = std::make_shared<AudioData>();
NyquistIO loader; NyquistIO loader;
if (argc > 1) if (argc > 1)
{ {
std::string cli_arg = std::string(argv[1]); std::string cli_arg = std::string(argv[1]);
loader.Load(fileData.get(), cli_arg); loader.Load(fileData.get(), cli_arg);
} }
else else
{ {
// Circular libnyquist testing // Circular libnyquist testing
//loader.Load(fileData.get(), "libnyquist_example_output.opus"); //loader.Load(fileData.get(), "libnyquist_example_output.opus");
// 1-channel wave // 1-channel wave
//loader.Load(fileData.get(), "test_data/1ch/44100/8/test.wav"); //loader.Load(fileData.get(), "test_data/1ch/44100/8/test.wav");
//loader.Load(fileData.get(), "test_data/1ch/44100/16/test.wav"); //loader.Load(fileData.get(), "test_data/1ch/44100/16/test.wav");
//loader.Load(fileData.get(), "test_data/1ch/44100/24/test.wav"); //loader.Load(fileData.get(), "test_data/1ch/44100/24/test.wav");
//loader.Load(fileData.get(), "test_data/1ch/44100/32/test.wav"); //loader.Load(fileData.get(), "test_data/1ch/44100/32/test.wav");
//loader.Load(fileData.get(), "test_data/1ch/44100/64/test.wav"); //loader.Load(fileData.get(), "test_data/1ch/44100/64/test.wav");
// 2-channel wave // 2-channel wave
//loader.Load(fileData.get(), "test_data/2ch/44100/8/test.wav"); //loader.Load(fileData.get(), "test_data/2ch/44100/8/test.wav");
//loader.Load(fileData.get(), "test_data/2ch/44100/16/test.wav"); //loader.Load(fileData.get(), "test_data/2ch/44100/16/test.wav");
//loader.Load(fileData.get(), "test_data/2ch/44100/24/test.wav"); //loader.Load(fileData.get(), "test_data/2ch/44100/24/test.wav");
//loader.Load(fileData.get(), "test_data/2ch/44100/32/test.wav"); //loader.Load(fileData.get(), "test_data/2ch/44100/32/test.wav");
//loader.Load(fileData.get(), "test_data/2ch/44100/64/test.wav"); //loader.Load(fileData.get(), "test_data/2ch/44100/64/test.wav");
//loader.Load(fileData.get(), "test_data/ad_hoc/TestBeat_44_16_mono-ima4-reaper.wav"); //loader.Load(fileData.get(), "test_data/ad_hoc/TestBeat_44_16_mono-ima4-reaper.wav");
//loader.Load(fileData.get(), "test_data/ad_hoc/TestBeat_44_16_stereo-ima4-reaper.wav"); //loader.Load(fileData.get(), "test_data/ad_hoc/TestBeat_44_16_stereo-ima4-reaper.wav");
// Multi-channel wave // Multi-channel wave
//loader.Load(fileData.get(), "test_data/ad_hoc/6_channel_44k_16b.wav"); //loader.Load(fileData.get(), "test_data/ad_hoc/6_channel_44k_16b.wav");
// 1 + 2 channel ogg // 1 + 2 channel ogg
//loader.Load(fileData.get(), "test_data/ad_hoc/LR_Stereo.ogg"); //loader.Load(fileData.get(), "test_data/ad_hoc/LR_Stereo.ogg");
//loader.Load(fileData.get(), "test_data/ad_hoc/TestLaugh_44k.ogg"); //loader.Load(fileData.get(), "test_data/ad_hoc/TestLaugh_44k.ogg");
//loader.Load(fileData.get(), "test_data/ad_hoc/TestBeat.ogg"); //loader.Load(fileData.get(), "test_data/ad_hoc/TestBeat.ogg");
//loader.Load(fileData.get(), "test_data/ad_hoc/TestBeatMono.ogg"); //loader.Load(fileData.get(), "test_data/ad_hoc/TestBeatMono.ogg");
//loader.Load(fileData.get(), "test_data/ad_hoc/BlockWoosh_Stereo.ogg"); //loader.Load(fileData.get(), "test_data/ad_hoc/BlockWoosh_Stereo.ogg");
// 1 + 2 channel flac // 1 + 2 channel flac
//loader.Load(fileData.get(), "test_data/ad_hoc/KittyPurr8_Stereo_Dithered.flac"); //loader.Load(fileData.get(), "test_data/ad_hoc/KittyPurr8_Stereo_Dithered.flac");
//loader.Load(fileData.get(), "test_data/ad_hoc/KittyPurr16_Stereo.flac"); //loader.Load(fileData.get(), "test_data/ad_hoc/KittyPurr16_Stereo.flac");
//loader.Load(fileData.get(), "test_data/ad_hoc/KittyPurr16_Mono.flac"); //loader.Load(fileData.get(), "test_data/ad_hoc/KittyPurr16_Mono.flac");
//loader.Load(fileData.get(), "test_data/ad_hoc/KittyPurr24_Stereo.flac"); //loader.Load(fileData.get(), "test_data/ad_hoc/KittyPurr24_Stereo.flac");
//auto memory = ReadFile("test_data/ad_hoc/KittyPurr24_Stereo.flac"); // broken //auto memory = ReadFile("test_data/ad_hoc/KittyPurr24_Stereo.flac"); // broken
//loader.Load(fileData.get(), "flac", memory.buffer); // broken //loader.Load(fileData.get(), "flac", memory.buffer); // broken
// Single-channel opus // Single-channel opus
//loader.Load(fileData.get(), "test_data/ad_hoc/detodos.opus"); // "Firefox: From All, To All" //loader.Load(fileData.get(), "test_data/ad_hoc/detodos.opus"); // "Firefox: From All, To All"
// 1 + 2 channel wavpack // 1 + 2 channel wavpack
//loader.Load(fileData.get(), "test_data/ad_hoc/TestBeat_Float32.wv"); //loader.Load(fileData.get(), "test_data/ad_hoc/TestBeat_Float32.wv");
//loader.Load(fileData.get(), "test_data/ad_hoc/TestBeat_Float32_Mono.wv"); //loader.Load(fileData.get(), "test_data/ad_hoc/TestBeat_Float32_Mono.wv");
//loader.Load(fileData.get(), "test_data/ad_hoc/TestBeat_Int16.wv"); //loader.Load(fileData.get(), "test_data/ad_hoc/TestBeat_Int16.wv");
//loader.Load(fileData.get(), "test_data/ad_hoc/TestBeat_Int24.wv"); //loader.Load(fileData.get(), "test_data/ad_hoc/TestBeat_Int24.wv");
//loader.Load(fileData.get(), "test_data/ad_hoc/TestBeat_Int32.wv"); //loader.Load(fileData.get(), "test_data/ad_hoc/TestBeat_Int32.wv");
//loader.Load(fileData.get(), "test_data/ad_hoc/TestBeat_Int24_Mono.wv"); //loader.Load(fileData.get(), "test_data/ad_hoc/TestBeat_Int24_Mono.wv");
// 1 + 2 channel musepack // 1 + 2 channel musepack
//loader.Load(fileData.get(), "test_data/ad_hoc/44_16_stereo.mpc"); //loader.Load(fileData.get(), "test_data/ad_hoc/44_16_stereo.mpc");
//loader.Load(fileData.get(), "test_data/ad_hoc/44_16_mono.mpc"); //loader.Load(fileData.get(), "test_data/ad_hoc/44_16_mono.mpc");
// In-memory ogg // In-memory ogg
//auto memory = ReadFile("test_data/ad_hoc/BlockWoosh_Stereo.ogg"); //auto memory = ReadFile("test_data/ad_hoc/BlockWoosh_Stereo.ogg");
@ -96,38 +96,38 @@ int main(int argc, const char **argv) try
// In-memory Mp3 // In-memory Mp3
auto memory = ReadFile("test_data/ad_hoc/acetylene.mp3"); auto memory = ReadFile("test_data/ad_hoc/acetylene.mp3");
loader.Load(fileData.get(), "mp3", memory.buffer); loader.Load(fileData.get(), "mp3", memory.buffer);
} }
/* Test Recording Capabilities of AudioDevice /* Test Recording Capabilities of AudioDevice
fileData->samples.reserve(44100 * 5); fileData->samples.reserve(44100 * 5);
fileData->channelCount = 1; fileData->channelCount = 1;
fileData->frameSize = 32; fileData->frameSize = 32;
fileData->lengthSeconds = 5.0; fileData->lengthSeconds = 5.0;
fileData->sampleRate = 44100; fileData->sampleRate = 44100;
std::cout << "Starting recording ..." << std::endl; std::cout << "Starting recording ..." << std::endl;
myDevice.Record(fileData->sampleRate * fileData->lengthSeconds, fileData->samples); myDevice.Record(fileData->sampleRate * fileData->lengthSeconds, fileData->samples);
*/ */
if (fileData->sampleRate != desiredSampleRate) if (fileData->sampleRate != desiredSampleRate)
{ {
std::cout << "[Warning - Sample Rate Mismatch] - file is sampled at " << fileData->sampleRate << " and output is " << desiredSampleRate << std::endl; std::cout << "[Warning - Sample Rate Mismatch] - file is sampled at " << fileData->sampleRate << " and output is " << desiredSampleRate << std::endl;
} }
std::cout << "Input Samples: " << fileData->samples.size() << std::endl; std::cout << "Input Samples: " << fileData->samples.size() << std::endl;
// Convert mono to stereo for testing playback // Convert mono to stereo for testing playback
if (fileData->channelCount == 1) if (fileData->channelCount == 1)
{ {
std::cout << "Playing MONO for: " << fileData->lengthSeconds << " seconds..." << std::endl; std::cout << "Playing MONO for: " << fileData->lengthSeconds << " seconds..." << std::endl;
std::vector<float> stereoCopy(fileData->samples.size() * 2); std::vector<float> stereoCopy(fileData->samples.size() * 2);
MonoToStereo(fileData->samples.data(), stereoCopy.data(), fileData->samples.size()); MonoToStereo(fileData->samples.data(), stereoCopy.data(), fileData->samples.size());
myDevice.Play(stereoCopy); myDevice.Play(stereoCopy);
} }
else else
{ {
std::cout << "Playing STEREO for: " << fileData->lengthSeconds << " seconds..." << std::endl; std::cout << "Playing STEREO for: " << fileData->lengthSeconds << " seconds..." << std::endl;
myDevice.Play(fileData->samples); myDevice.Play(fileData->samples);
} }
// Test Opus Encoding // Test Opus Encoding
{ {
@ -143,21 +143,21 @@ int main(int argc, const char **argv) try
std::cout << "Encoder Status: " << encoderStatus << std::endl; std::cout << "Encoder Status: " << encoderStatus << std::endl;
} }
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
catch (const UnsupportedExtensionEx & e) catch (const UnsupportedExtensionEx & e)
{ {
std::cerr << "Caught: " << e.what() << std::endl; std::cerr << "Caught: " << e.what() << std::endl;
} }
catch (const LoadPathNotImplEx & e) catch (const LoadPathNotImplEx & e)
{ {
std::cerr << "Caught: " << e.what() << std::endl; std::cerr << "Caught: " << e.what() << std::endl;
} }
catch (const LoadBufferNotImplEx & e) catch (const LoadBufferNotImplEx & e)
{ {
std::cerr << "Caught: " << e.what() << std::endl; std::cerr << "Caught: " << e.what() << std::endl;
} }
catch (const std::exception & e) catch (const std::exception & e)
{ {
std::cerr << "Caught: " << e.what() << std::endl; std::cerr << "Caught: " << e.what() << std::endl;
} }

@ -45,7 +45,7 @@ class RingBufferT
public: public:
// Constructs a RingBufferT with size = 0 // Constructs a RingBufferT with size = 0
RingBufferT() : mData(nullptr), mAllocatedSize(0), mWriteIndex(0), mReadIndex(0) {} RingBufferT() : mData(nullptr), mAllocatedSize(0), mWriteIndex(0), mReadIndex(0) {}
// Constructs a RingBufferT with \a count maximum elements. // Constructs a RingBufferT with \a count maximum elements.
RingBufferT(size_t count) : mAllocatedSize(0) { resize(count); } RingBufferT(size_t count) : mAllocatedSize(0) { resize(count); }

@ -1,44 +1,44 @@
/* /*
Copyright (c) 2019, Dimitri Diakopoulos All rights reserved. copyright (c) 2019, dimitri diakopoulos all rights reserved.
Redistribution and use in source and binary forms, with or without redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met: modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this * redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer. list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, * redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution. and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" this software is provided by the copyright holders and contributors "as is"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE and any express or implied warranties, including, but not limited to, the
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE implied warranties of merchantability and fitness for a particular purpose are
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE disclaimed. in no event shall the copyright holder or contributors be liable
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL for any direct, indirect, incidental, special, exemplary, or consequential
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR damages (including, but not limited to, procurement of substitute goods or
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER services; loss of use, data, or profits; or business interruption) however
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, caused and on any theory of liability, whether in contract, strict liability,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE or tort (including negligence or otherwise) arising in any way out of the use
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. of this software, even if advised of the possibility of such damage.
*/ */
#ifndef NYQUIST_ENCODERS_H #ifndef nyquist_encoders_h
#define NYQUIST_ENCODERS_H #define nyquist_encoders_h
#include "Common.h" #include "common.h"
namespace nqr namespace nqr
{ {
// A simplistic encoder that takes a buffer of audio, conforms it to the user's // a simplistic encoder that takes a buffer of audio, conforms it to the user's
// EncoderParams preference, and writes to disk. Be warned, does not support resampling! // encoderparams preference, and writes to disk. be warned, does not support resampling!
// @todo support dithering, samplerate conversion, etc. // @todo support dithering, samplerate conversion, etc.
int encode_wav_to_disk(const EncoderParams p, const AudioData * d, const std::string & path); int encode_wav_to_disk(const encoderparams p, const audiodata * d, const std::string & path);
// Assume data adheres to EncoderParams, except for bit depth and fmt which are re-formatted // assume data adheres to encoderparams, except for bit depth and fmt which are re-formatted
// to satisfy the Ogg/Opus spec. // to satisfy the ogg/opus spec.
int encode_opus_to_disk(const EncoderParams p, const AudioData * d, const std::string & path); int encode_opus_to_disk(const encoderparams p, const audiodata * d, const std::string & path);
} // end namespace nqr } // end namespace nqr
#endif // end NYQUIST_ENCODERS_H #endif // end nyquist_encoders_h

Loading…
Cancel
Save