Go Down

Topic: Step-based volume adjustment (Read 373 times) previous topic - next topic

sixsmith

Nov 14, 2017, 08:33 am Last Edit: Nov 14, 2017, 08:41 am by sixsmith
Hi there, I'm pretty new to Arduino, with a basic Arduino Uno set. I have a project that essentially requires the following things:

  • Plays two different set of sounds into right and left earphone channels separately - let's call them sound A in the right, and sound B in the left
  • Two buttons/triggers to vary only sound A. If I press one button, sound A increases in volume, if I press the other button, it decreases in volume in steps, eg. by 5 decibels. This process repeats over several tracks.
  • A way to record how many times the tracks were played and the respective decibels/decibel changes, if any.

I'm having a hard time understanding what parts I might need to go about achieving this. A friend recommended the MP3 shield, but will I need earphone jacks? An external battery to run this separate from the computer? Any help would be really appreciated!

Grumpy_Mike

#1
Nov 14, 2017, 09:46 am Last Edit: Nov 14, 2017, 09:52 am by Grumpy_Mike
Be aware that this project can not be done with just an Arduino Uno. Also it is a complex processor intensive project, it might sound simple to you but that is because you have no idea yet about what is needed.

I would advise you to look at the Teensy 3.6 board and the I2S sound output board they sell. They have developed a graphics interface for complex projects like this.
http://openaudio.blogspot.co.uk/2016/10/benchmarking-teensy-36-is-fast.html
https://www.pjrc.com/store/teensy36.html

This page tells you about it in the long video
https://www.pjrc.com/store/audio_tutorial_kit.html

sixsmith

I did some looking around and was further recommended to use Robertsonic's WAV Trigger, which seems to be able to do what I need for now.

What I'm trying to do now is actually have it activate the sequence with a pushbutton. (Later, I'll redefine it to increase/decrease volume accordingly.) I've taken bits from the original WAV Trigger code, as well as a basic two pushbutton trigger code.

Code: [Select]
/* Libraries */
#include <Metro.h>
#include <AltSoftSerial.h>
#include <wavTrigger.h>

/* Define buttons */
int buttonBpin = 8;
#define LED 13                // our LED
wavTrigger wTrig;             // Our WAV Trigger object
Metro gLedMetro(500);         // LED blink interval timer
Metro gSeqMetro(6000);        // Sequencer state machine interval timer
byte gLedState = 0;           // LED State
int  gSeqState = 0;           // Main program sequencer state
int  gRateOffset = 0;         // WAV Trigger sample-rate offset

void setup() {
  Serial.begin(9600);
  pinMode(buttonBpin, INPUT_PULLUP);
  pinMode(LED,OUTPUT);
  digitalWrite(LED,gLedState);

    // If the Arduino is powering the WAV Trigger, we should wait for the WAV
  //  Trigger to finish reset before trying to send commands.
  delay(1000);

  // WAV Trigger startup at 57600
  wTrig.start();
  delay(10);
 
  // Send a stop-all command and reset the sample-rate offset, in case we have
  //  reset while the WAV Trigger was already playing.
  wTrig.stopAllTracks();
  wTrig.samplerateOffset(0);
}

void loop() {

  if (digitalRead(buttonBpin) == HIGH)
  {
    int i;
          switch (gSeqState) {
 
          // State 0: Demonstrates how to fade in a music track
          case 0:
                Serial.println("hello0");
              wTrig.samplerateOffset(0);            // Reset our sample rate offset
              wTrig.masterGain(0);                  // Reset the master gain to 0dB
             
              wTrig.trackGain(2, -40);              // Preset Track 2 gain to -40dB
              wTrig.trackPlayPoly(2);               // Start Track 2
              wTrig.trackFade(2, 0, 2000, 0);       // Fade Track 2 up to 0dB over 2 secs
          break;

          // State 1: Demonstrates how to cross-fade music tracks
          case 1:
                          Serial.println("hello1");

              wTrig.trackGain(1, -40);              // Preset Track 1 gain to -40dB
              wTrig.trackPlayPoly(1);               // Start Track 1
              wTrig.trackFade(1, 0, 3000, false);   // Fade Track 1 up to 0db over 3 secs
              wTrig.update();
              delay(2000);                          // Wait 2 secs
              wTrig.trackFade(2, -40, 3000, true);  // Fade Track 2 down to -40dB over 3 secs and stop
          break;
                                 
          // State 2: Fade down music and start looping dialog
          case 2:
                          Serial.println("hello2");

              wTrig.trackFade(1, -6, 500, 0);
              wTrig.trackPlayPoly(4);               // Start Track 4 poly
              wTrig.trackLoop(4, 1);                // Enable Track 4 looping
          break;

          // State 3: Honk the horn 2 times
          case 3:
                          Serial.println("hello3");

              wTrig.trackPlayPoly(5);               // Start Track 5 poly
              delay(500);
              wTrig.trackStop(5);                   // Stop Track 5
              delay(250);
              wTrig.trackPlayPoly(5);               // Start Track 5 poly
              delay(500);
              wTrig.trackStop(5);                   // Stop Track 5
          break;
         
          // State 4: Fade out and stop dialog
          case 4:
                          Serial.println("hello4");

              wTrig.trackLoop(4, 0);                // Disable Track 4 looping
              wTrig.trackFade(4, -50, 5000, 1);     // Fade Track 4 to -50dB and stop
          break;
         
          // State 5: This demonstrates playing musical instrument samples, with decay on
          //  release
          case 5:
              wTrig.masterGain(-8);                 // Lower main volume
              wTrig.trackPlayPoly(6);               // Play first note
              delay(1000);
              wTrig.trackPlayPoly(7);               // Play second note
              delay(1000);
              wTrig.trackPlayPoly(8);               // Play third note
              delay(1000);
              wTrig.trackFade(6, -50, 5000, 1);     // Fade Track 6 to -50dB and stop
              wTrig.trackFade(7, -50, 5000, 1);     // Fade Track 7 to -50dB and stop
              wTrig.trackFade(8, -50, 5000, 1);     // Fade Track 8 to -50dB and stop
          break;
   
          // State 6: Demonstrates preloading tracks and starting them in sample-sync, and
          //  real-time samplerate control (pitch bending);
          case 6:
              wTrig.trackLoad(6);                   // Load and pause Track 6
              wTrig.trackLoad(7);                   // Load and pause Track 7
              wTrig.trackLoad(8);                   // Load and pause Track 8
              wTrig.resumeAllInSync();              // Start all in sample sync
              for (i = 0; i < 100; i++) {
                gRateOffset -= 200;
                wTrig.samplerateOffset(gRateOffset);
                delay(10);
              }
              for (i = 0; i < 100; i++) {
                gRateOffset += 200;
                wTrig.samplerateOffset(gRateOffset);
                delay(10);
              }
              delay(500);
              wTrig.stopAllTracks();                // Stop all
         break;
         
           
      } // switch
 
      // Increment our state
      gSeqState++;
      if (gSeqState > 6)
          gSeqState = 0;
         
  } // if (digitalRead(buttonBpin)

  // If time to do so, toggle the LED
  if (gLedMetro.check() == 1) {
      if (gLedState == 0) gLedState = 1;
      else gLedState = 0;
      digitalWrite(LED, gLedState);
  } // if (gLedMetro.check() == 1)

  // Delay 30 msecs
  delay(30);
}


However, I've been getting the following error message: (Using Windows 10, Arduino 1.8.5, an Arduino Uno and WAV Trigger board)
Quote
C:\Users\sixsmith\AppData\Local\Temp\ccA3y1EC.ltrans0.ltrans.o:(.rodata+0x6): undefined reference to `Print::write(unsigned char const*, unsigned int)'

c:/program files/windowsapps/arduinollc.arduinoide_1.8.10.0_x86__mdqgnx93n4wtt/hardware/tools/avr/bin/../lib/gcc/avr/4.9.2/../../../../avr/lib/avr5/crtatmega328p.o:(.init9+0x0): undefined reference to `main'

collect2.exe: error: ld returned 1 exit status

exit status 1
Error compiling for board Arduino/Genuino Uno.
Wondering if anyone is able to help with this?

Grumpy_Mike

It looks like you have not installed all the libraries correctly
Metro.h
AltSoftSerial.h
wavTrigger.h

When you post code with obscure libraries you should also include links to the libiary.

sixsmith

#4
Nov 18, 2017, 04:09 pm Last Edit: Nov 18, 2017, 05:45 pm by sixsmith
Apologies! Here are the individual libraries.

WAV Trigger
Metro
AltSoftSerial

I deleted them and reinstalled them, but I seem to be getting more error messages:

Quote
Arduino: 1.8.5 (Windows Store 1.8.10.0) (Windows 10), Board: "Arduino/Genuino Uno"

C:\Users\sixsmith\Documents\Arduino\libraries\Metro\Metro.cpp:42:16: warning: extra tokens at end of #ifdef directive

  #ifdef NOCATCH-UP

                ^

c:\program files\windowsapps\arduinollc.arduinoide_1.8.10.0_x86__mdqgnx93n4wtt\hardware\tools\avr\bin\../lib/gcc/avr/4.9.2/../../../../avr/bin/ar.exe: unable to rename 'core\core.a'; reason: File exists

exit status 1
Error compiling for board Arduino/Genuino Uno.
I should probably point out that I could get the WAV Trigger's example file to work fine previously, although now it doesn't seem to be running either (same error message appears).

Grumpy_Mike

#5
Nov 18, 2017, 08:48 pm Last Edit: Nov 18, 2017, 08:49 pm by Grumpy_Mike
When I tried it I had to extract the Metro folder from the Metro-Arduino-Wiring folder and just put it in the libraries. Did you remove the "-Master" bit from the other folders before you put them in the libiary folder?

It then compiles on my system.

Restart the IDE when you have made these changes.

MarkT

Master is just a github thing, its the branch in the repo, remove it
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Go Up