38 Replies Latest reply on Aug 24, 2016 3:17 AM by saurabh2890

    Read in from the headset jack, and read out to the amplifier

    chamuel

      Hi,

      I've installed the drivers for the Wolfson Audio Card on my raspian, and it works!

      I can play music with a python progeramm, i take the command line music player xmms2.       https://xmms2.org/wiki/Main_Page

       

      Now, I'm engineering a standalone Musicbox, and i want to read in the music (headset jack), and read out the same music the the integrated amplifier.

      It woud be nice to control it from a python programm.

       

      Is that possible ?

       

      Thanks and Regards,

      Chamuel

        • Re: Read in from the headset jack, and read out to the amplifier
          suicidal_orange

          It might be possible but the headset only has one input channel so you'll either get the same sound out of both speakers through the amp or one will be silent... neither would sound very good!

           

          Also depending what you are putting the input through you may only land up with the left or right channel going in - why not use the line in socket which is stereo?

          • Re: Read in from the headset jack, and read out to the amplifier
            ragnar.jensen

            I have made a couple of scripts that connect inputs to outputs directly on the Wolfson card. To just listen, there is no need for aplay, arecord or alsaloop.

            The sound is also routed to the Pi, so you can record it.

            If you run the scripts with the argument "eq", the sound is routed through equalizers.

            • Listen_to_LineIn_on_Lineout.sh
            • Listen_to_LineIn_on_SPDIF.sh
            • Listen_to_LineIn_on_Speakers.sh
            • Listen_to_SPDIF_on_LineOut.sh
            • Listen_to_SPDIF_on_SPDIF.sh
            • Listen_to_SPDIF_on_Speakers.sh

             

            --

            Ragnar

            1 of 1 people found this helpful
              • Re: Read in from the headset jack, and read out to the amplifier
                chamuel

                oh wow, that is really good!

                it has no delay and is not blocking the terminal.

                 

                The equalizers works too, my question is: I want to filter out some frequency of a Song.

                (If my musicbox plays specific frequency the box starts to vibrate, and i would like to damping them. So i can listen my music louder.)

                  • Re: Read in from the headset jack, and read out to the amplifier
                    laughingbuddha.01

                    Thanks for the script but I'm getting a message after loading it

                    >fi

                    amixer: Cannot find the given element from control hw:0

                     

                    It should see the card ?

                      • Re: Read in from the headset jack, and read out to the amplifier
                        ragnar.jensen

                        That ">fi" looks suspect. I get the feeling the script is corrupted.

                        Listen_to_SPDIF_on_LineOut.sh should look exactly like this:

                        #!/bin/bash -x
                        
                        CARD="hw:0"
                        BEQUIET=" -q "
                        #BEQUIET=""
                        
                        # Route SPDIF input to LineOut jack.
                        # Optionally route sound through equalizers.
                        # WM8804 SPDIF chip is connected to AIF2 on WM5102.
                        
                        # Open SPDIF in
                        amixer ${BEQUIET} -D${CARD} cset name='SPDIF in Switch' off
                        amixer ${BEQUIET} -D${CARD} cset name='SPDIF in Switch' on
                        amixer -Dhw:0 cset name='RX Playback Switch' off
                        amixer -Dhw:0 cset name='RX Playback Switch' on
                        
                        # Switch off SPDIF TX in case already active as WM8804 must run at RX rate if enabled
                        amixer ${BEQUIET} -D${CARD} cset name='TX Playback Switch' off
                        amixer ${BEQUIET} -D${CARD} cset name='RX Playback Switch' on
                        amixer ${BEQUIET} -D${CARD} cset name='AIF Playback Switch' on
                        
                        # Set volume on HPOUT2 (LineOut) to a sane level first, to avoid loud bursts.
                        amixer ${BEQUIET} -D${CARD} cset name='HPOUT2 Digital Volume' 128
                        amixer ${BEQUIET} -D${CARD} cset name='HPOUT2L Input 1 Volume' 20
                        amixer ${BEQUIET} -D${CARD} cset name='HPOUT2R Input 1 Volume' 20
                        if [ "$1" = eq ]
                        then
                         # Route sound through equalizers
                         # Route SPDIF input AIF2RX{1,2} through equalizsers EQ{1,2} to LineOut jack HPOUT2{L,R}
                         # AIF2RX --> EQ --> HPOUT2
                         amixer ${BEQUIET} -D${CARD} cset name='EQ1 Input 1' AIF2RX1
                         amixer ${BEQUIET} -D${CARD} cset name='EQ2 Input 1' AIF2RX2
                         amixer ${BEQUIET} -D${CARD} cset name='HPOUT2L Input 1' EQ1 
                         amixer ${BEQUIET} -D${CARD} cset name='HPOUT2R Input 1' EQ2
                        
                         # Adjust equalizers.
                         # My cheap PC speakers need some bass and a slight treble boost.
                         # Valid values: 0 - 24 (-12dB to +12dB in 1dB steps)
                         # Left channel
                         amixer ${BEQUIET} -D${CARD} cset name='EQ1 B1 Volume' 16 # +4dB
                         amixer ${BEQUIET} -D${CARD} cset name='EQ1 B2 Volume' 13 # +1dB
                         amixer ${BEQUIET} -D${CARD} cset name='EQ1 B3 Volume' 12
                         amixer ${BEQUIET} -D${CARD} cset name='EQ1 B4 Volume' 12
                         amixer ${BEQUIET} -D${CARD} cset name='EQ1 B5 Volume' 14 # +2dB
                         # Right channel
                         amixer ${BEQUIET} -D${CARD} cset name='EQ2 B1 Volume' 16
                         amixer ${BEQUIET} -D${CARD} cset name='EQ2 B2 Volume' 13
                         amixer ${BEQUIET} -D${CARD} cset name='EQ2 B3 Volume' 12
                         amixer ${BEQUIET} -D${CARD} cset name='EQ2 B4 Volume' 12
                         amixer ${BEQUIET} -D${CARD} cset name='EQ2 B5 Volume' 14
                        
                         # If we want to be able to record the unequalized sound, send output from AIF2RX (SPDIFin) to AIF1TX (I2S to RPi)
                         # AIF2 --> AIF1
                         amixer ${BEQUIET} -D${CARD} cset name='AIF1TX1 Input 1' AIF2RX1
                         amixer ${BEQUIET} -D${CARD} cset name='AIF1TX2 Input 1' AIF2RX2
                         # ...or if we want to record the equalized sound, send output from equalizers to AIF1TX (I2S to RPi)...
                         # EQ --> AIF1 
                         # amixer ${BEQUIET} -D${CARD} cset name='AIF1TX1 Input 1' EQ1
                         # amixer ${BEQUIET} -D${CARD} cset name='AIF1TX2 Input 1' EQ2
                        else
                         # No EQ, send sound straight from SPDIFin to LineOut
                         # Route SPDIF input (AudioInterFace2) to LineOut jack (HeadPhone2)
                         amixer ${BEQUIET} -D${CARD} cset name='HPOUT2L Input 1' AIF2RX1
                         amixer ${BEQUIET} -D${CARD} cset name='HPOUT2R Input 1' AIF2RX2
                        
                         # If we want to be able to record what we listen to, send output from AIF2RX (SPDIFin) to AIF1TX (I2S to RPi)
                         # AIF2 --> AIF1
                         amixer ${BEQUIET} -D${CARD} cset name='AIF1TX1 Input 4' AIF2RX1
                         amixer ${BEQUIET} -D${CARD} cset name='AIF1TX2 Input 4' AIF2RX2
                        
                        fi
                          
                        

                         

                         

                        Can you post the entire output of the script when you run it?

                        --

                        Ragnar

                    • Re: Read in from the headset jack, and read out to the amplifier
                      kenaynay

                      Ok Ragnar I'm a newb.  What do i need to do to the Listen to LineIn on Lineout script to boost the volume level? And when you say use the argument eq, are

                      • Re: Read in from the headset jack, and read out to the amplifier
                        tstirlingc

                          Thank you so much for the Listen_scripts.tgz!  That was almost exactly what I'm looking for.  I am hoping to use the Wolfson/Raspberry Pi as a simultaneous SPDIFIn->LineOut and LineIn->SPDIFOut at 96khz for a band I'm working with.  I looked through the listen scripts and it looks like it might be possible to do this.  Can you confirm that this is possible?

                         

                        Thank you!

                         

                        Todd

                        • Re: Read in from the headset jack, and read out to the amplifier
                          realies

                          Could there be any particular reason for this script not to be routing properly on the cirrus logic audio card which is the alternative version of the sound card for the A+ / B+ models? I have changed hw:0 to hw:sndrpiwsp as it is in the examples files, but after running LineIn to LineOut for example I get no output on the LineOut jack. I get no strange messages from the terminal even if executed without the -q argument. Thanks

                           

                          PS: default Record_from_linein.sh just in case

                          #!/bin/bash
                          
                          # $1 added to support 1st line argument. i.e. "./Record_from_linein.sh -q" will stop all the control information being display$
                          
                          #Record from onboard Line Input to AP
                          # +8dB input PGA gain
                          amixer $1 -Dhw:sndrpiwsp cset name='IN3L Volume' 8
                          amixer $1 -Dhw:sndrpiwsp cset name='IN3R Volume' 8
                          
                          # better THD in normal mode vs lower noise floor in high performance
                          amixer $1 -Dhw:sndrpiwsp cset name='IN3 High Performance Switch' on
                          # Configure the input path for 0dB Gain,  HPF with a low cut off for DC removal
                          amixer $1 -Dhw:sndrpiwsp cset name='IN3L Digital Volume' 128
                          amixer $1 -Dhw:sndrpiwsp cset name='IN3R Digital Volume' 128
                          amixer $1 -Dhw:sndrpiwsp cset name='LHPF1 Input 1' IN3L
                          amixer $1 -Dhw:sndrpiwsp cset name='LHPF2 Input 1' IN3R
                          amixer $1 -Dhw:sndrpiwsp cset name='LHPF1 Mode' High-pass
                          amixer $1 -Dhw:sndrpiwsp cset name='LHPF2 Mode' High-pass
                          amixer $1 -Dhw:sndrpiwsp cset name='LHPF1 Coefficients' 240,3
                          amixer $1 -Dhw:sndrpiwsp cset name='LHPF2 Coefficients' 240,3
                          # Configure the Audio Interface and volume 0dB
                          amixer $1 -Dhw:sndrpiwsp cset name='AIF1TX1 Input 1' LHPF1
                          amixer $1 -Dhw:sndrpiwsp cset name='AIF1TX1 Input 1 Volume' 32
                          amixer $1 -Dhw:sndrpiwsp cset name='AIF1TX2 Input 1' LHPF2
                          amixer $1 -Dhw:sndrpiwsp cset name='AIF1TX2 Input 1 Volume' 32
                          
                          
                          # The following command should be used to test
                          # arecord -Dhw:sndrpiwsp -r 44100 -c 2 -f S32_LE 

                           

                           

                          Confirmed, I've executed the script on the Model B with the Wolfson Audio Card and it passes the audio through as expected. Does someone have a clue what might be the change over the Cirrus Logic one?

                          • Re: Read in from the headset jack, and read out to the amplifier
                            rstark

                            Hi Ragnar,

                             

                            I was wondering if you would know how to take the input from line in and then put it through some C code and then back out through line out one sample at a time? I have the C code written already in such a way that if I can just get the samples into it in real time from line in and put the samples back out to line out it will work perfectly. This is for a guitar pedal project (guitar plugged into the cirrus / wolfson audio card, sample run through C code, cirrus card plugged into amp).

                             

                            Thanks for any help,

                             

                            Ryan

                            • Re: Read in from the headset jack, and read out to the amplifier
                              saurabh2890

                              hi Ranger,,,I am using a cirrus audio card in which I am giving live audio through FM tuner in Line IN and is recording internally,,,,I want to listen live through Line out..but when I am running command   ( Listen_to_LineIn_on_Lineout.sh)..it shows the error,,,,(bash Listen_to_LineIn_on_Lineout.sh: command not found),,,,can u help me please...

                            • Re: Read in from the headset jack, and read out to the amplifier
                              kjciszewski

                              I'm not impressed with this whole Wolfson/Cirrus thing. Wolfson can't set up their PI A/B driver to handle all the sample rates supported by ALSA (down to 8K for sure), and now the PI A+/B+ driver doesn't work right.  Can some one explain to them how to get their act together?  I sent an email to Cirrus' CEO on the corporate site complaining about the sample rate problem.  How do we contact someone in tech support to get their attention?

                               

                              I asked (in another post) what sample rates the A+/B+ card supported, and was told to look at the data sheet, which gives a 32K minimum for the SPDIF ports, but says nothing about the driver itself.  Why are things being limited based on SPDIF?  Most of the world doesn't care about SPDIF--the interest is in network audio, SIP/Asterisk communications, and so on.

                              • Re: Read in from the headset jack, and read out to the amplifier
                                kjciszewski

                                Even so , a full implementation of ALSA's features would be appreciated for the Wolfson/Cirrus cards.  How do we get tech support motivated to help?

                                • Re: Read in from the headset jack, and read out to the amplifier
                                  hystrix

                                  I have tried to pass the Microphone input to Line out, per the OP's original query, but it doesn't work.  All I get is a chopping noise.

                                   

                                  I have run the two use scripts - record from headset and playback to line out, and tried this:

                                   

                                  arecord -Dhw:sndrpiwsp -r 44100 -c 2 -f S32_LE -d0 -vvv  | aplay -Dhw:sndrpiwsp -r 44100 -c 2 -f S32_LE

                                   

                                  I even tried recording to a null file:


                                  arecord -Dhw:sndrpiwsp -r 44100 -c 2 -f S32_LE -d0 -vvv /dev/null  | aplay -Dhw:sndrpiwsp -r 44100 -c 2 -f S32_LE


                                  I really want to monitor microphone input at 192kHz, and play back at say 44.1kHz to line-out, but I can't even get it working with the same frequency in and out.


                                  Can anyone help?


                                  Many thanks.

                                    • Re: Read in from the headset jack, and read out to the amplifier
                                      pimpmypixel

                                      Hey,

                                       

                                      did you get this (DMIC -> headphones) to work?

                                      I'm still such a newbie here - what am I missing?

                                       

                                      #!/bin/bash
                                      CARD="hw:0"
                                      BEQUIET=" -q "
                                      #BEQUIET=""

                                      # Gain Control
                                      amixer ${BEQUIET} -D${CARD} cset name='IN2L Digital Volume' 138
                                      amixer ${BEQUIET} -D${CARD} cset name='IN2R Digital Volume' 138

                                      # Input level
                                      amixer ${BEQUIET} -D${CARD} cset name='AIF1TX1 Input 1' IN2R

                                      amixer ${BEQUIET} -D${CARD} cset name='AIF1TX1 Input 1 Volume' 30
                                      amixer ${BEQUIET} -D${CARD} cset name='AIF1TX2 Input 1' IN2L

                                      amixer ${BEQUIET} -D${CARD} cset name='AIF1TX2 Input 1 Volume' 44
                                      amixer ${BEQUIET} -D${CARD} cset name='DMIC Switch' on

                                       

                                      # No EQ, ATTEMPT TO send sound straight from DMIC to headphones
                                      amixer ${BEQUIET} -D${CARD} cset name='HPOUT1 Input 1' IN2L

                                      amixer ${BEQUIET} -D${CARD} cset name='HPOUT1 Input 1' IN2R

                                       

                                      #Playback from AP to Headset
                                      amixer ${BEQUIET} -D${CARD} cset name='HPOUT1 Digital Switch' on


                                      Hope you can help..

                                      Thanks so much!

                                        • Re: Read in from the headset jack, and read out to the amplifier
                                          spambake

                                          I was able to playback the Dmics to a headphone by using the Sox utilities:

                                           

                                          ./Reset_paths.sh

                                          ./Record_from_DMIC.sh

                                          ./Playback_to_Headset.sh

                                          play "|rec -d"

                                           

                                          I got the solution from this site via Hackaday:

                                          http://planet-geek.com/2015/10/29/hacks/using-a-raspberry-pi-as-a-realtime-voice-changer-for-halloween/

                                           

                                          With a RPI2 and Cirrus card the latency is about 1/10 second I would guess.

                                           

                                          This should work well for near real time bat detection pitch shifting.

                                            • Re: Read in from the headset jack, and read out to the amplifier
                                              hystrix

                                              It does indeed work well for bat detection using pitch shifting, or "time-expansion".

                                               

                                              I've been using this method, using sox, this summer.  When you're used to the heterodyne clicks of bats, to hear them pitch-shifted is a revelation.  They sound like birds .

                                               

                                              Here's a time-expansion bat recording I made using my Pi2 and Cirrus Logic card: https://youtu.be/6evyf_9uZjs

                                                • Re: Read in from the headset jack, and read out to the amplifier
                                                  spambake

                                                  Actually just using "sox -d -d" works fine for sending default in to default out.  You can also add sox effects in to the command.

                                                   

                                                  Hystrix - I have been following your superb bat detection project (noted on the youtube detail) for a while.  Who would have thought we could have a high quality hackable bat detector for 100USD or so.  Mine is set to automatically record bat calls all night long and upload the spectrograms to Google drive in the morning.

                                                   

                                                  And now you have a multiuse touch display on yours.  Nice work! 

                                                    • Re: Read in from the headset jack, and read out to the amplifier
                                                      hystrix

                                                      I'm glad you like my bat project.  It sounds like what you're doing with spectrograms is interesting, too.  I would be interested to know how you are extracting the periods of recorded data that contains bat calls in order to create the spectrograms.

                                                       

                                                      Also, I find sox commands a complete mystery, and not well documented.  It took me ages to work out how to send the mic input out to speakers or line out.  My solution is a lot more complicated than just "sox -d -d"!  Is there a good resource anywhere?

                                                        • Re: Read in from the headset jack, and read out to the amplifier
                                                          kjciszewski

                                                          There's a book entitled "Raspberry Pi for Secret Agents" by Stefan Sjogelid  that uses Sox and explains how some of the commands work. Unfortunately, it was written before the Wolfson Audio Card came into being, so it uses a USB audio interface.  Even so, it might be helpful.

                                                          • Re: Read in from the headset jack, and read out to the amplifier
                                                            wan1

                                                            Looks great! What software did you use to create the spectrogram? Was it executed on RPi, or on your desktop comp?

                                                             

                                                            I'm asking because I looking for the optimal way to create a real-time spectrograms on RPi during recording. (Actually, the idea is to make a compact autonomous field recorder based on RPi+CLAC with lcd, wich shows r-t spectrogram to monitor killer whale sounds, as a part of my research work.)

                                                            • Re: Read in from the headset jack, and read out to the amplifier
                                                              spambake

                                                              @hystrix

                                                              I don’t know of any really good resources for sox.  I mostly use their online man page and, like you, gradually tease out what they mean. Good Google-fu can be helpful.  This site can be useful:

                                                              http://sox.10957.n7.nabble.com/

                                                              Beware: popups!!

                                                               

                                                              The automated system works pretty well except rain and some insects set it off, too.  It is still very much a work in progress and I am not an expert. Some things I use:

                                                              Homebuilt preamp that rolls off lower frequencies and seems less noisy.

                                                              Use the Cirrus card high pass filter coefficients to roll off lower frequencies.  The lines in the use case scripts look like this:

                                                              amixer $1 -Dhw:sndrpiwsp cset name='LHPF1 Coefficients' 248,31
                                                              
                                                              

                                                              Roll off below 10K =  244,139

                                                              Below 20K = 248,31

                                                              Below 30k=251,41

                                                               

                                                              To automate the recording I use the sox silence function to record sound above a certain volume level and split each detection into separate files.  Fortunately bats are loud.  Here is the command I currently use:

                                                              rec -V3 -b 32 -r 192000 /home/pi/bats/test.wav remix 2 silence 1 0 1% 1 2.0 1% : newfile : restart
                                                              
                                                              

                                                              Adjust the percents to get above the noise level on your setup

                                                               

                                                              Morning processing involves deleting files smaller than 1 meg and larger than 10 meg.  A meg equates to about one second.  This gets rid of most of the worst clutter.  File names are changed to reflect the times they were first created and then sox is used to bulk create the spectrograms.  I use this to upload the spectrograms to Google drive:

                                                              https://github.com/prasmussen/gdrive

                                                               

                                                              As you know all of this required a lot of research, experimentation and troubleshooting and there is much more to do.

                                                               

                                                              Feel free to ask more about this.

                                                               

                                                              I have not yet found a way to have two simultaneous outputs in real time.  For instance record and listen at the same time, or record at full spectrum while listening to a pitch shifted version or produce a spectrogram while recording.

                                                  • Re: Read in from the headset jack, and read out to the amplifier
                                                    saurabh2890

                                                    hi, Ragnar ,,

                                                                       I AM USING cirrus logic ,,,in which I feed input through Line and able to make a file....I want to listen live through Line out....through the command u  given...

                                                    • Listen_to_LineIn_on_Lineout.sh ,,,,,,,,,,but this shows the error : command not found ,,will u please help me..