Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 8 of 8

Thread: Problem changing AudioMixer4 gain in library

  1. #1
    Junior Member
    Join Date
    Aug 2017
    Posts
    6

    Problem changing AudioMixer4 gain in library

    Hi,
    I am attempting to make a device that plays back multiple wav files and runs them through effects.

    I also want to use a seven segment display as an interface and to have a few encoders control multiple parameters. To handle all these states I am using the automaton library by tinkerspy: https://github.com/tinkerspy/Automaton

    I made some proof of concept sketches to familiarize myself with the audio library, and I was able to control AudioMixer4 objects using pots with no problem.

    In my current code, I am trying to split different parts of the program into different state machine libraries. One of the machines I am trying to build is a volume controller that will control either master volume (sgtl5000_1.volume()) or different instance of the wav player's volume by changing the appropriate mixer gain.

    When I split things up, controlling the sgtl5000 works, but controlling mixer gain does not.
    I have tried setting up and including the mixers every way that I can think of, but even when it compiles and runs, none of the commands to the mixers in Atm_master_vol.cpp have any effect.

    I did something similar to the Atm_master_vol.setVolume in an earlier sketch this way, and it worked:

    void SetMasterVolume(int level) {
    // read the knob position for master volume(analog input A2)
    float vol = (float)level / 1280.0;
    sgtl5000_1.volume(vol);
    //Serial.print("volume = ");
    //Serial.println(vol);
    }

    void SetFeedbackLevel(int level) {
    // uncomment for A3 knob to control the feedback level
    float feedback = (float)level / 1050.0;
    mixer2.gain(1, feedback);
    }

    however when i try to do it in the library, only the sgtl5000 volume control is working. I have sgtl5000 and the AudioMixer4 objects declared the same way in audio_system_mix.h.

    //audio_system_mix.h
    #pragma once
    //#include "audio_system.h"
    //#include <Audio.h>
    //audio library
    static AudioMixer4 mixer2; //xy=568,393
    static AudioMixer4 mixer1; //xy=575,263
    static AudioMixer4 mixer4; //xy=760,403
    static AudioMixer4 mixer3; //xy=765,263
    static AudioControlSGTL5000 sgtl5000_1; //xy=744,575

    Can you please help me understand why i can change the sftl5000 volume and not mixer3 or mixer4 .gain?



    Thank you!

    Full source can be found here: https://github.com/radiorpl/Atm_bira_teensy
    Last edited by radiorpl; 08-07-2017 at 03:26 AM.

  2. #2
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    4,438
    I'd insert a println(level) in SetFeedbackLevel to see if it ever gets called and with which values.

  3. #3
    Junior Member
    Join Date
    Aug 2017
    Posts
    6
    Thanks, I'll try that. I should mention that in the machine::begin of Atm_master_vol.cpp, changing values of the mixer gains also has no effect. I can comment them out or leave them and it doesn't change anything. Meanwhile, in the begin,
    sgtl5000_1.volume(0.5);
    works, and you can hear the gain change from this, so it seems that no calls to the mixer gain are working in the cpp file, but sgtl5000_1 calls are working.

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    17,482
    Quote Originally Posted by radiorpl View Post
    Can you please help me understand why i can change the sftl5000 volume and not mixer3 or mixer4 .gain?
    The problem is probably due to your use of "static".

    Code:
    static AudioMixer4              mixer4;         //xy=760,403
    static AudioMixer4              mixer3;         //xy=765,263
    This will create separate copies of the mixer objects for each file where you use them. The ones you're controlling in another file aren't the same mixers which are actually processing your audio.

  5. #5
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    17,482
    The proper usage is to create one instance in one .cpp file. Then use "extern" for the header file declaration, so all other files are actually referencing the instance created elsewhere.

    These pages might help:

    http://www.cprogramming.com/declare_vs_define.html

    https://stackoverflow.com/questions/...nd-extern-in-c

    Using "static" creates a unique copy which is local to only that .cpp file. That's obviously not what you wanted.

  6. #6
    Junior Member
    Join Date
    Aug 2017
    Posts
    6
    Thanks Paul! Declaring in Atm_master_vol.cpp and changing static to extern in the audio_system_mix.h did the trick!

    Also, thank you for the teensy and audio library, I'm finding lots of awesome stuff to do with them.

  7. #7
    Junior Member
    Join Date
    Aug 2017
    Posts
    6
    Hi I was incorrect before when I said sgtl5000_1.volume was working completely. It does work to change the volume in the begin section of Atm_master_vol.cpp, but in the function setVolume, it does not work.

    The mixer gains I was originally asking about do work in this function, just not the sgtl5000_1.volume().
    Can you help me under stand why I can set the sgtl5000 volume in one part of the file and not in another?

    sgtl5000_1.volume(0.5) works here:
    Machine::begin( state_table, ELSE );
    vol_control = volCon;
    AudioMemory(160);
    sgtl5000_1.enable();
    sgtl5000_1.volume(0.5);

    but does not here:
    Atm_master_vol& Atm_master_vol::setVolume( float volume ) {
    if ( vol_control == 0 ){ //master
    if(volume > 0.8){
    volume = 0.8; //louder will clip
    }
    sgtl5000_1.volume(volume);
    Serial.println("master vol");
    Serial.println(volume);
    }
    else if ( vol_control == 1 ){ //wav1
    mixer6.gain(0, volume);
    Serial.println("bira 1 volume");
    Serial.println(volume);
    }
    else if ( vol_control == 2 ){ //wav2
    mixer6.gain(1, volume);
    Serial.println("bira 2 volume");
    Serial.println(volume);
    }
    return *this;
    }

    full source at:
    https://github.com/radiorpl/Atm_rpl_repo

    Thank you!

  8. #8
    Junior Member
    Join Date
    Aug 2017
    Posts
    6
    update: fixed by declaring
    sgtl5000_1.enable();
    sgtl5000_1.volume(0.5);
    in the sketch instead of a library.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •