Still WIP, but first working version of audio. I2S works good, analogue not so much.

This commit is contained in:
Thomas Göttgens
2022-12-02 00:30:31 +01:00
parent fb89828990
commit 4b63730efb
3 changed files with 154 additions and 167 deletions

View File

@@ -7,41 +7,36 @@
#include "NodeDB.h"
#include <Arduino.h>
#include <driver/i2s.h>
#include <driver/adc.h>
// #include <driver/adc.h>
#include <functional>
#include <codec2.h>
#include <ButterworthFilter.h>
#include <FastAudioFIFO.h>
#define ADC_BUFFER_SIZE 320 // 40ms of voice in 8KHz sampling frequency
#define ADC_BUFFER_SIZE_MAX 320
class Codec2Thread : public concurrency::NotifiedWorkerThread
{
struct CODEC2* codec2_state = NULL;
int16_t output_buffer[ADC_BUFFER_SIZE] = {};
public:
Codec2Thread();
int get_encode_frame_size() { return encode_frame_size; };
protected:
int tx_encode_frame_index = 0;
int encode_codec_size = 0;
int encode_frame_num = 0;
int encode_frame_size = 0;
virtual void onNotify(uint32_t notification) override;
};
enum RadioState { standby, rx, tx };
class AudioModule : public SinglePortModule, private concurrency::OSThread
{
bool firstTime = true;
hw_timer_t* adcTimer = NULL;
uint16_t adc_buffer_index = 0;
public:
unsigned char rx_encode_frame[Constants_DATA_PAYLOAD_LEN] = {};
unsigned char tx_encode_frame[Constants_DATA_PAYLOAD_LEN] = {};
int16_t speech[ADC_BUFFER_SIZE_MAX] = {};
int16_t output_buffer[ADC_BUFFER_SIZE_MAX] = {};
uint16_t adc_buffer[ADC_BUFFER_SIZE_MAX] = {};
int adc_buffer_size = 0;
uint16_t adc_buffer_index = 0;
int tx_encode_frame_index = 0;
int rx_encode_frame_index = 0;
int encode_codec_size = 0;
int encode_frame_size = 0;
volatile RadioState radio_state = RadioState::rx;
FastAudioFIFO fifo;
struct CODEC2* codec2 = NULL;
int16_t sample;
adc1_channel_t mic_chan = (adc1_channel_t)0;
uint8_t rx_raw_audio_value = 127;
AudioModule();
@@ -51,9 +46,11 @@ class AudioModule : public SinglePortModule, private concurrency::OSThread
void sendPayload(NodeNum dest = NODENUM_BROADCAST, bool wantReplies = false);
protected:
virtual int32_t runOnce() override;
int encode_frame_num = 0;
bool firstTime = true;
// void run_codec2();
virtual int32_t runOnce() override;
virtual MeshPacket *allocReply() override;
@@ -64,15 +61,5 @@ class AudioModule : public SinglePortModule, private concurrency::OSThread
};
extern AudioModule *audioModule;
extern Codec2Thread *codec2Thread;
extern uint16_t adc_buffer[ADC_BUFFER_SIZE];
extern uint16_t adc_buffer_index;
extern portMUX_TYPE timerMux;
extern int16_t speech[ADC_BUFFER_SIZE];
enum RadioState { standby, rx, tx };
extern volatile RadioState radio_state;
extern adc1_channel_t mic_chan;
IRAM_ATTR void am_onTimer();
#endif