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

Thread: Audio library, USB audio interface, volume setting

  1. #1
    Senior Member PaulS's Avatar
    Join Date
    Apr 2015
    Location
    Netherlands
    Posts
    106

    Audio library, USB audio interface, volume setting

    Hooked up a PCM5102 board to a Teensy 3.1 to form a USB audio interface.

    Click image for larger version. 

Name:	PCM5102 DAC board.jpg 
Views:	9 
Size:	111.3 KB 
ID:	16337

    With the code below, my windows PC sees a USB Teensy Audio device and outputs audio via the PCM5102 board as expected.
    Code:
    // PCM5102 bd   Teensy
    // VCC          5V
    // GND          GND
    // LRCK         23
    // DATA         22
    // BCK          9
    
    // set Tools > USB Type to Audio (+ Serial if needed)
    
    #include <Audio.h>
    
    AudioInputUSB            usb1;
    AudioAmplifier           amp2;
    AudioAmplifier           amp1;
    AudioOutputI2S           i2s1;
    AudioConnection          patchCord1(usb1, 0, amp1, 0);
    AudioConnection          patchCord2(usb1, 1, amp2, 0);
    AudioConnection          patchCord3(amp2, 0, i2s1, 1);
    AudioConnection          patchCord4(amp1, 0, i2s1, 0);
    
    void setup()
    {
      Serial.begin(9600);
      AudioMemory(12);
      pinMode(LED_BUILTIN, OUTPUT);
      digitalWrite(LED_BUILTIN, HIGH);
    }
    
    void loop()
    {
      float vol = usb1.volume();  // read PC volume setting
      amp1.gain(vol);             // set gain according to PC volume
      amp2.gain(vol);
      delay(100);
      Serial.println(vol);
    }
    To control the volume of the audio I use the windows volume slider:

    Click image for larger version. 

Name:	Windows audio volume 50%.png 
Views:	8 
Size:	8.5 KB 
ID:	16338

    Now the interesting part is that when the slider is at 50%, the value of variable "vol" = 0.63. I expected 0.50.
    When I checked other slider settings, I see the following values for slider percentages:

    Click image for larger version. 

Name:	Slider vs volume value.PNG 
Views:	10 
Size:	16.5 KB 
ID:	16339

    My questions now are: where [and how] is this conversion done? And is there a reason it isn't linear?
    I have been digging through the audio library sources but could not find it.

    Any help and/or explanation is appreciated!

    Thanks,
    Paul

  2. #2
    Senior Member PaulS's Avatar
    Join Date
    Apr 2015
    Location
    Netherlands
    Posts
    106
    After further digging I found a number of references to USB volume in C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy3\us b_audio.h & usb_audio.c.
    But I'm still in the dark where the non-linear conversion takes place.

    Is it perhaps in C:\Program Files (x86)\Arduino\hardware\teensy\avr\libraries\Audio\ mixer.h and mixer.cpp? If I understand these files correctly, I see a conversion from float to int32_t. Could that be the issue?

    Regards,
    Paul

  3. #3
    Senior Member PaulS's Avatar
    Join Date
    Apr 2015
    Location
    Netherlands
    Posts
    106

    Questions answered

    While printing member "volume" of structure "usb_audio_features_struct" [in C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy3\us b_audio.h] to the serial monitor, I noticed the same non-linearity between Windows slider percentage and the value of "volume".

    Since member "volume" is directly obtained from the USB audio interface Volume feature descriptor, I can only conclude that Windows does the non-linear conversion from slider percentage to USB volume. Apparently Windows 7 tries to make a kind of logarithmic scaled volume.

    Regards,
    Paul

Posting Permissions

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