how to read output of sine_fm1?

Status
Not open for further replies.

alonsorobots

New member
Hi there!

How do you read the output of sine_fm1?

Below is the code that I made that takes a mic input, finds the note freq, creates sine wave of that frequency, which is then modulated and output to the speakers. It's pretty trippy to hear your voice in pure tone =)

I wanted to be able to print out the output of sine_fm1 that is going into i2s2. i.e if the signal was being modulated by a really slow "triangle", I wanted to be able to print the freq and see it go up and down.

Thanks a bunch!

Alonso


Code:
#include <Bounce.h>
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

Bounce button0 = Bounce(0, 15);
// GUItool  begin automatically generated code
AudioInputI2S            i2s1;           //xy=101,237
AudioAnalyzePeak         peak1;
AudioOutputI2S           i2s2;           //xy=310,169
AudioSynthWaveform       waveform1;      //xy=77,30
AudioAnalyzeNoteFrequency notefreq1;      //xy=312,291
AudioSynthWaveformSineModulated sine_fm1;       //xy=145,102
AudioConnection          patchCord1(i2s1, 0, peak1, 0);
AudioConnection          patchCord2(i2s1, 0, notefreq1, 0);
AudioConnection          patchCord3(waveform1, sine_fm1);
AudioConnection          patchCord4(sine_fm1, 0, i2s2, 0);
AudioConnection          patchCord5(sine_fm1, 0, i2s2, 1);
AudioControlSGTL5000     sgtl5000_1;     //xy=319,350
// GUItool  end automatically generated code


void setup() {

  Serial.begin(9600);
  AudioMemory(60);
  sgtl5000_1.enable();
  sgtl5000_1.volume(0.5);
  sgtl5000_1.inputSelect(AUDIO_INPUT_MIC);
  sgtl5000_1.micGain(15);
  //this is for button0 and 1
  pinMode(0, INPUT_PULLUP);
  notefreq1.begin(.40);
  waveform1.begin(WAVEFORM_SINE);
  waveform1.amplitude(0.0);
  waveform1.frequency(60);
  waveform1.pulseWidth(0.15);
  sine_fm1.frequency(0);
  sine_fm1.amplitude(0.75);
  delay(1000);
}
elapsedMillis timeout = 0;
uint16_t micTone = 0;
int lastTime = 0;
int waveform_type = WAVEFORM_SINE;
float volPeak = 0;
void loop() {
  button0.update();
  if (button0.fallingEdge()) {
    Serial.print("Control waveform: ");
    if (waveform_type == WAVEFORM_SAWTOOTH) {
      waveform_type = WAVEFORM_SINE;
      Serial.println("Sine");
    } else if (waveform_type == WAVEFORM_SINE) {
      waveform_type = WAVEFORM_SQUARE;
      Serial.println("Square");
    } else if (waveform_type == WAVEFORM_SQUARE) {
      waveform_type = WAVEFORM_TRIANGLE;
      Serial.println("Triangle");
    } else if (waveform_type == WAVEFORM_TRIANGLE) {
      waveform_type = WAVEFORM_SAWTOOTH;
      Serial.println("SawTooth");
    }
    waveform1.begin(waveform_type);
  }
  if (timeout >= 80) {
    if (peak1.available()) {
      volPeak = peak1.read()*50;
    }
    if (notefreq1.available()) {
      micTone = notefreq1.read();
      //this is useful to remove some noise
      if ((micTone > 2000) or (micTone < 70) or (volPeak < 0.05)) {
        micTone = 0;
      }
      timeout = 0;
    } else {
      micTone = 0;
    }
  }
  // knob1 on A3 controls the amplitude of modifier wave that offsets micTone freq
  float knob1 = (float)analogRead(A3) / 1023.0;
  // knob2 on A2 controls the frequency of modifier wave that offsets micTone freq
  float knob2 = (float)analogRead(A2) / 1023.0;
  //higher pitch sound louder
  sgtl5000_1.volume(mapF(micTone,0,2000,0.5,0.2));
  waveform1.amplitude(knob1);
  waveform1.frequency(360 * knob2 + 0.25);
  sine_fm1.frequency(micTone);
  
  if (micTone != 0 ){
    Serial.print("Freq: ");
    Serial.print(micTone);
    Serial.print(" Amp: ");
    Serial.print(knob1);
    Serial.print(" Freq: ");
    Serial.print(knob2);
    if (waveform_type == 0)Serial.print(" Sine ");
    if (waveform_type == 1)Serial.print(" Sawtooth ");
    if (waveform_type == 2)Serial.print(" Square ");
    if (waveform_type == 3)Serial.print(" Triangle ");
    if (waveform_type == 5)Serial.print(" Pulse ");
    Serial.print(" peak: ");
    Serial.print(volPeak);
    Serial.print("  |");
    for (int cnt=0; cnt<volPeak*100; cnt++) {
      Serial.print(">");
    }
    Serial.println();
  }
}
float mapF(float x, float in_min, float in_max, float out_min, float out_max)
{
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
 
Last edited:
Status
Not open for further replies.
Back
Top