You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
2.8 KiB
102 lines
2.8 KiB
3 years ago
|
#ifndef output_test_h_
|
||
|
#define output_test_h_
|
||
|
|
||
|
#include <Arduino.h>
|
||
|
#include <AudioStream.h>
|
||
|
#include <thread>
|
||
|
#include <iostream>
|
||
|
#include <fstream>
|
||
|
#include <filesystem>
|
||
|
#include <unistd.h>
|
||
|
|
||
|
#ifdef __APPLE__
|
||
|
#define __filesystem std::__fs::filesystem
|
||
|
#else
|
||
|
#define __filesystem std::filesystem
|
||
|
#endif
|
||
|
class TestAudioOutput : public AudioStream
|
||
|
{
|
||
|
public:
|
||
|
TestAudioOutput(void) : AudioStream(2, inputQueueArray) { begin(); }
|
||
|
virtual void update(void);
|
||
|
void begin(void);
|
||
|
static void isr(void);
|
||
2 years ago
|
bool saveOutputFile(const char * path, const char* filename){
|
||
|
if (num_inputs == 0) return false;
|
||
3 years ago
|
char cwd[500];
|
||
|
if (getcwd(cwd, sizeof(cwd)) != NULL) {
|
||
|
printf("Current working dir: %s\n", cwd);
|
||
|
} else {
|
||
|
perror("getcwd() error");
|
||
|
}
|
||
2 years ago
|
std::string outputPath = std::string(cwd) + "/" + std::string(path);
|
||
3 years ago
|
__filesystem::path p(outputPath);
|
||
|
if (! __filesystem::exists(p) )
|
||
|
__filesystem::create_directories(outputPath);
|
||
|
|
||
2 years ago
|
_filePath = outputPath + std::string(filename);
|
||
2 years ago
|
std::cout << "saving output audio .wav file to " << _filePath << std::endl;
|
||
2 years ago
|
_outputFile.open(_filePath, std::ios_base::trunc | std::ios_base::out);
|
||
3 years ago
|
if (!_outputFile.is_open()) {
|
||
2 years ago
|
Serial.printf("couldn't open file for recording...%s\n", _filePath.c_str());
|
||
|
return false;
|
||
3 years ago
|
} else {
|
||
|
_filename = filename;
|
||
|
_outputFile.write((char*)test_output_wav_header, 44);
|
||
2 years ago
|
_saveToFile = true;
|
||
|
return true;
|
||
3 years ago
|
}
|
||
|
}
|
||
|
void closeOutputfile(uint16_t numChannels) {
|
||
|
if (!_saveToFile) return;
|
||
|
if (_outputFile.is_open()) {
|
||
|
_saveToFile = false;
|
||
|
char buf[4];
|
||
|
buf[1] = numChannels >> 8;
|
||
|
buf[0] = numChannels;
|
||
2 years ago
|
_outputFile.seekp(22, std::ios_base::beg);
|
||
3 years ago
|
_outputFile.write(buf, 2);
|
||
|
|
||
|
long bytespersecond = numChannels * 2 * 44100;
|
||
|
buf[3] = bytespersecond >> 24;
|
||
|
buf[2] = bytespersecond >> 16;
|
||
|
buf[1] = bytespersecond >> 8;
|
||
|
buf[0] = bytespersecond;
|
||
2 years ago
|
_outputFile.seekp(28, std::ios_base::beg);
|
||
3 years ago
|
_outputFile.write(buf, 4);
|
||
|
|
||
|
short bytespersampleframe = numChannels * 2;
|
||
|
buf[1] = bytespersampleframe >> 8;
|
||
|
buf[0] = bytespersampleframe;
|
||
2 years ago
|
_outputFile.seekp(32, std::ios_base::beg);
|
||
3 years ago
|
_outputFile.write(buf, 2);
|
||
|
|
||
|
buf[3] = _dataSize >> 24;
|
||
|
buf[2] = _dataSize >> 16;
|
||
|
buf[1] = _dataSize >> 8;
|
||
|
buf[0] = _dataSize;
|
||
2 years ago
|
_outputFile.seekp(40, std::ios_base::beg);
|
||
3 years ago
|
_outputFile.write(buf, 4);
|
||
|
_outputFile.close();
|
||
|
_filename = nullptr;
|
||
|
}
|
||
|
}
|
||
|
protected:
|
||
|
std::ofstream _outputFile;
|
||
2 years ago
|
std::string _filePath;
|
||
3 years ago
|
static audio_block_t *block_left_1st;
|
||
|
static audio_block_t *block_right_1st;
|
||
|
static bool update_responsibility;
|
||
|
bool _saveToFile = false;
|
||
|
unsigned int _dataSize = 0;
|
||
|
const char *_filename;
|
||
|
static void memcpy_tdm_tx(int16_t *dest, int16_t *src1, int16_t *src2);
|
||
|
|
||
|
private:
|
||
|
audio_block_t *inputQueueArray[2];
|
||
|
static unsigned char test_output_wav_header[];
|
||
|
};
|
||
|
|
||
|
|
||
|
#endif
|