<commit_before>/*
   SDWriting_02_RemoteControlled
   
   Created: <NAME>, OpenAudio, May 2019
   Purpose: Write audio to SD based on serial commands
      via USB serial or via BT serial

   MIT License.  use at your own risk.
*/


// Include all the of the needed libraries
#include <Tympan_Library.h>

//define state...the "myState" instance is actually created in SerialManager.h
#define NO_STATE (-1)
const int INPUT_PCBMICS = 0, INPUT_MICJACK = 1, INPUT_LINEIN_SE = 2, INPUT_LINEIN_JACK = 3, INPUT_PDM_MICS = 4;
class State_t {
  public:
    int input_source = NO_STATE;
};

//local files
#include "SerialManager.h"

//set the sample rate and block size
const float sample_rate_Hz = 44117.0f ; //24000 or 44117 or 96000 (or other frequencies in the table in AudioOutputI2S_F32)
const int audio_block_samples = 128;     //do not make bigger than AUDIO_BLOCK_SAMPLES from AudioStream.h (which is 128)  Must be 128 for SD recording.
AudioSettings_F32 audio_settings(sample_rate_Hz, audio_block_samples);

// Define the overall setup
String overall_name = String("Tympan: SDWriting_02_RemoteControlled");
float default_input_gain_dB = 5.0f; //gain on the microphone
float input_gain_dB = default_input_gain_dB;
#define MAX_AUDIO_MEM 60

// /////////// Define audio objects...they are configured later

//create audio library objects for handling the audio
Tympan                        myTympan(TympanRev::D);
AudioInputI2S_F32             i2s_in(audio_settings);   //Digital audio input from the ADC
AudioSDWriter_F32             audioSDWriter(audio_settings); //this is stereo by default
AudioOutputI2S_F32            i2s_out(audio_settings);  //Digital audio output to the DAC.  Should always be last.

//Connect to outputs
AudioConnection_F32           patchcord1(i2s_in, 0, i2s_out, 0);    //Left input to left output
AudioConnection_F32           patchcord2(i2s_in, 1, i2s_out, 1);    //Right input to right output

//Connect to SD logging
AudioConnection_F32           patchcord3(i2s_in, 0, audioSDWriter, 0);   //connect Raw audio to left channel of SD writer
AudioConnection_F32           patchcord4(i2s_in, 1, audioSDWriter, 1);   //connect Raw audio to right channel of SD writer


//control display and serial interaction
bool enable_printCPUandMemory = false;
void togglePrintMemoryAndCPU(void) {  enable_printCPUandMemory = !enable_printCPUandMemory; }; 
void setPrintMemoryAndCPU(bool state) { enable_printCPUandMemory = state;};
SerialManager serialManager;

//keep track of state
State_t myState;

void setConfiguration(int config) {
  myState.input_source = config;

  switch (config) {
    case INPUT_PCBMICS:
      //Select Input and set gain
      audioHardware.enableDigitalMicInputs(false);
      myTympan.inputSelect(TYMPAN_INPUT_ON_BOARD_MIC); // use the on-board microphones
      input_gain_dB = 15;
      myTympan.setInputGain_dB(input_gain_dB);
      break;

    case INPUT_MICJACK:
      //Select Input and set gain
      audioHardware.enableDigitalMicInputs(false);
      myTympan.inputSelect(TYMPAN_INPUT_JACK_AS_MIC); // use the mic jack
      myTympan.setEnableStereoExtMicBias(true);  //put the mic bias on both channels
      input_gain_dB = default_input_gain_dB;
      myTympan.setInputGain_dB(input_gain_dB);
      break;
      
    case INPUT_LINEIN_JACK:
      //Select Input and set gain
      audioHardware.enableDigitalMicInputs(false);
      myTympan.inputSelect(TYMPAN_INPUT_JACK_AS_LINEIN); // use the line-input through holes
      input_gain_dB = 0;
      myTympan.setInputGain_dB(input_gain_dB);
      break;
      
    case INPUT_LINEIN_SE:
      //Select Input and set gain
      audioHardware.enableDigitalMicInputs(false);
      myTympan.inputSelect(TYMPAN_INPUT_LINE_IN); // use the line-input through holes
      input_gain_dB = default_input_gain_dB;
      myTympan.setInputGain_dB(input_gain_dB);
      break;
        
     case INPUT_PDM_MICS:
      audioHardware.enableDigitalMicInputs(true);
      input_gain_dB = 0;
      myTympan.setInputGain_dB(input_gain_dB); //doesn't affect the digital PDM mic inputs?
      break;  
  }
 }

// ///////////////// Main setup() and loop() as required for all Arduino programs

// define the setup() function, the function that is called once when the device is booting
void setup() {
  delay(100);
  myTympan.beginBothSerial(); delay(1000);
  myTympan.print(overall_name); myTympan.println(": setup():...");
  myTympan.print("Sample Rate (Hz): "); myTympan.println(audio_settings.sample_rate_Hz);
  myTympan.print("Audio Block Size (samples): "); myTympan.println(audio_settings.audio_block_samples);

  //allocate the dynamically re-allocatable audio memory
  AudioMemory_F32(MAX_AUDIO_MEM, audio_settings); 

  //activate the Tympan audio hardware
  myTympan.enable();        // activate AIC
  myTympan.volume_dB(0.0);  // output amp: -63.6 to +24 dB in 0.5dB steps.  uses signed 8-bit

  //Configure for Tympan PCB mics
  myTympan.println("Setup: Using Mic Jack with Mic Bias.");
  setConfiguration(INPUT_MICJACK); //this will also unmute the system

  //prepare the SD writer for the format that we want and any error statements
  audioSDWriter.setSerial(&myTympan);         //the library will print any error info to this serial stream (note that myTympan is also a serial stream)<commit_msg>SDwriting_02_RemoteControlled: fix audioHardware to myTympan<commit_after>/*
   SDWriting_02_RemoteControlled
   
   Created: <NAME>, OpenAudio, May 2019
   Purpose: Write audio to SD based on serial commands
      via USB serial or via BT serial

   MIT License.  use at your own risk.
*/


// Include all the of the needed libraries
#include <Tympan_Library.h>

//define state...the "myState" instance is actually created in SerialManager.h
#define NO_STATE (-1)
const int INPUT_PCBMICS = 0, INPUT_MICJACK = 1, INPUT_LINEIN_SE = 2, INPUT_LINEIN_JACK = 3, INPUT_PDM_MICS = 4;
class State_t {
  public:
    int input_source = NO_STATE;
};

//local files
#include "SerialManager.h"

//set the sample rate and block size
const float sample_rate_Hz = 44117.0f ; //24000 or 44117 or 96000 (or other frequencies in the table in AudioOutputI2S_F32)
const int audio_block_samples = 128;     //do not make bigger than AUDIO_BLOCK_SAMPLES from AudioStream.h (which is 128)  Must be 128 for SD recording.
AudioSettings_F32 audio_settings(sample_rate_Hz, audio_block_samples);

// Define the overall setup
String overall_name = String("Tympan: SDWriting_02_RemoteControlled");
float default_input_gain_dB = 5.0f; //gain on the microphone
float input_gain_dB = default_input_gain_dB;
#define MAX_AUDIO_MEM 60

// /////////// Define audio objects...they are configured later

//create audio library objects for handling the audio
Tympan                        myTympan(TympanRev::D);
AudioInputI2S_F32             i2s_in(audio_settings);   //Digital audio input from the ADC
AudioSDWriter_F32             audioSDWriter(audio_settings); //this is stereo by default
AudioOutputI2S_F32            i2s_out(audio_settings);  //Digital audio output to the DAC.  Should always be last.

//Connect to outputs
AudioConnection_F32           patchcord1(i2s_in, 0, i2s_out, 0);    //Left input to left output
AudioConnection_F32           patchcord2(i2s_in, 1, i2s_out, 1);    //Right input to right output

//Connect to SD logging
AudioConnection_F32           patchcord3(i2s_in, 0, audioSDWriter, 0);   //connect Raw audio to left channel of SD writer
AudioConnection_F32           patchcord4(i2s_in, 1, audioSDWriter, 1);   //connect Raw audio to right channel of SD writer


//control display and serial interaction
bool enable_printCPUandMemory = false;
void togglePrintMemoryAndCPU(void) {  enable_printCPUandMemory = !enable_printCPUandMemory; }; 
void setPrintMemoryAndCPU(bool state) { enable_printCPUandMemory = state;};
SerialManager serialManager;

//keep track of state
State_t myState;

void setConfiguration(int config) {
  myState.input_source = config;

  switch (config) {
    case INPUT_PCBMICS:
      //Select Input and set gain
      myTympan.enableDigitalMicInputs(false);
      myTympan.inputSelect(TYMPAN_INPUT_ON_BOARD_MIC); // use the on-board microphones
      input_gain_dB = 15;
      myTympan.setInputGain_dB(input_gain_dB);
      break;

    case INPUT_MICJACK:
      //Select Input and set gain
      myTympan.enableDigitalMicInputs(false);
      myTympan.inputSelect(TYMPAN_INPUT_JACK_AS_MIC); // use the mic jack
      myTympan.setEnableStereoExtMicBias(true);  //put the mic bias on both channels
      input_gain_dB = default_input_gain_dB;
      myTympan.setInputGain_dB(input_gain_dB);
      break;
      
    case INPUT_LINEIN_JACK:
      //Select Input and set gain
      myTympan.enableDigitalMicInputs(false);
      myTympan.inputSelect(TYMPAN_INPUT_JACK_AS_LINEIN); // use the line-input through holes
      input_gain_dB = 0;
      myTympan.setInputGain_dB(input_gain_dB);
      break;
      
    case INPUT_LINEIN_SE:
      //Select Input and set gain
      myTympan.enableDigitalMicInputs(false);
      myTympan.inputSelect(TYMPAN_INPUT_LINE_IN); // use the line-input through holes
      input_gain_dB = default_input_gain_dB;
      myTympan.setInputGain_dB(input_gain_dB);
      break;
        
     case INPUT_PDM_MICS:
      myTympan.enableDigitalMicInputs(true);
      input_gain_dB = 0;
      myTympan.setInputGain_dB(input_gain_dB); //doesn't affect the digital PDM mic inputs?
      break;  
  }
 }

// ///////////////// Main setup() and loop() as required for all Arduino programs

// define the setup() function, the function that is called once when the device is booting
void setup() {
  delay(100);
  myTympan.beginBothSerial(); delay(1000);
  myTympan.print(overall_name); myTympan.println(": setup():...");
  myTympan.print("Sample Rate (Hz): "); myTympan.println(audio_settings.sample_rate_Hz);
  myTympan.print("Audio Block Size (samples): "); myTympan.println(audio_settings.audio_block_samples);

  //allocate the dynamically re-allocatable audio memory
  AudioMemory_F32(MAX_AUDIO_MEM, audio_settings); 

  //activate the Tympan audio hardware
  myTympan.enable();        // activate AIC
  myTympan.volume_dB(0.0);  // output amp: -63.6 to +24 dB in 0.5dB steps.  uses signed 8-bit

  //Configure for Tympan PCB mics
  myTympan.println("Setup: Using Mic Jack with Mic Bias.");
  setConfiguration(INPUT_MICJACK); //this will also unmute the system

  //prepare the SD writer for the format that we want and any error statements
  audioSDWriter.setSerial(&myTympan);         //the library will print any error info to this serial stream (note that myTympan is also a serial stream)
{'fraction_non_alphanumeric': 0.08466754567715201, 'fraction_numerical': 0.021284611037860238, 'mean_word_length': 3.5910073497622137, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '422857', 'n_tokens_mistral': 3600, 'n_tokens_neox': 3349, 'n_words': 979}