element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Raspberry Pi
  • Products
  • More
Raspberry Pi
Raspberry Pi Forum Is it possible to send audio from "SPDIF In" to the Lineout using the mixer?
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi to participate - click to join for free!
Featured Articles
Announcing Pi
Technical Specifications
Raspberry Pi FAQs
Win a Pi
Raspberry Pi Wishlist
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Verified Answer
  • Replies 21 replies
  • Answers 3 answers
  • Subscribers 666 subscribers
  • Views 2846 views
  • Users 0 members are here
  • raspberry_pi
  • raspeberry_pi_accessories
Related

Is it possible to send audio from "SPDIF In" to the Lineout using the mixer?

eveready1010
eveready1010 over 11 years ago

I have been trying to figure this out for a while now...

 

I can record audio coming in from the "SPDIF In" to a file, and then I can play back the same file to the Lineout, but is it possible to "mix" the incoming audio from the SPDIF In to the Lineout only using the mixer?

 

I know it should be possible since my windows pc does allow for just this scenario (Except it used the Line In instead of SPDIF In).

  • Sign in to reply
  • Cancel
  • Former Member
    0 Former Member over 11 years ago

    I managed to do this by editing the Playback_to_Lineout.sh usage script so that the value after 'HPOUT2L Input 1' is AIF2RX1 and 'HPOUT2R Input 1' is AIF2RX2. I then ran that modified script and the SPDIF_record.sh usage script.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • Former Member
    0 Former Member over 11 years ago in reply to Former Member

    I did like youtomph, but put it all in one script. And for good measure, I added the possibility to use the equalizers on the Wolfson chip.

    The attached script takes an optional argument "eq". Run it without that argument and the sound goes straight from SPDIFin to LineOut.

     

    --

    Ragnar

    Attachments:
    https://community.element14.com/cfs-file/__key/communityserver-discussions-components-files/91/Listen_5F00_to_5F00_SPDIF.sh
    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • eveready1010
    0 eveready1010 over 11 years ago in reply to Former Member

    Thanks for the reply.  Yes, both your solution and tomph's solution worked!  I guess I just did not know what input on the mixer was the SPDIF In.

     

    Maybe this is for another topic, but is there a way to increase the sample rate of the SPDIF In?  The features say that the audio chip is capable of 192kHz at 24 bits.

     

    Is this done through the driver, mixer, etc.?  Maybe the Pi cannot handle that high of a resolution?

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 11 years ago in reply to eveready1010

    As I have seen it, it's automatic. If the recording application requests 192k, the drivers switch the input accordingly.

    E.g: arecord -Dhw:0 -r 192000 -c 2 -f S32_LE myfile.wav

    I have done recordings at 192k with a couple of different recording programs. I select the rate I want in the program and it just works image

    --

    Ragnar

     



    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • eveready1010
    0 eveready1010 over 11 years ago in reply to Former Member

    Thanks for the response.  Actually I am not trying to record anything on the Pi, just pass through audio from my PC's SPDIF Out to the Pi's SPDIF In and then out to the Line Out of the Pi.  I checked my PC and the SPDIF Out was set to 16bits, 44100 Hz.  I tried changing the format on my PC to higher settings but the sound actually got worse as I increased it.

     

    Here are the settings that are available on the PC:

     

    16bit, 44100 Hz     Sounded fine

    16bit, 48000 Hz     Sounded the same but with a little bit of static

    16bit, 96000 Hz     Nothing but pops and static

    16bit, 192000 Hz     Nothing but static

    24bit, 44100 Hz     Sounded fine

    24bit, 48000 Hz     Sounded the same but with a little bit of static

    24bit, 96000 Hz     Nothing but pops and static

    24bit, 192000 Hz     Nothing but static

     

    Since I am not recording anything and just passing audio for SPDIF In to Line Out, is it possible to change the Pi SPDIF In Sample Rate to match the PC settings?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 11 years ago in reply to eveready1010

    I just record a second at the desired rate and bit depth to the null device, to force the SPDIF input to change.

    E.g. to switch it to 96k/24bit:

    arecord -Dhw:0 -r 96000 -c 2 -d 1 -f S24_LE /dev/null

     

    Here's a small script to set rate and bit depth:

    #!/bin/bash
    if [ "$1" = 44 ] || [ "$1" = 48 ] || [ "$1" = 96 ] || [ "$1" = 192 ]
    then
      recrate=$1"000"
      [ $1 = 44 ] && recrate=44100
    else
      echo "Invalid rate $1"
      echo "Valid values 44, 48, 96 and 192"
      exit
    fi
    if [ "$2" = 16 ] || [ "$2" = 24 ] || [ "$2" = 32 ]
    then
      recformat=S${2}_LE
    else
      echo "Invalid bit depth $2"
      echo "Valid values 16, 24 and 32"
      exit
    fi
    arecord -q -Dhw:0 -r $recrate -c2 -d 1 -f $recformat /dev/null

     

    Save as set_SPDIF_rate.sh, make it executable with chmod +x set_SPDIF_rate.sh
    and then run it: ./set_SPDIF_rate.sh 96 24

     

    --

    Ragnar

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • eveready1010
    0 eveready1010 over 11 years ago in reply to Former Member

    Wow!  That did it!  Thanks for your help and the scripts...  They were helpful.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • eveready1010
    0 eveready1010 over 11 years ago in reply to eveready1010

    Thanks again for all of your help.  The tips have worked great so far!

     

    Ok, sorry to keep asking about this but I have another question...

     

    When I reboot the Pi, the record settings default back to 44100kHz 16bit and my speakers emit garbled sound.  Now I did try to put the script into an upstart script and also tried putting the arecord command directly into the upstart script, but unfortunately doing so basically disabled my Pi!  I cannot SSH into it, nor login to it on the X Session.  (I did find a way to revert the settings back so I could get into it once again, by taking out the SD card and starting into Single User Mode, removing the upstart script, and then booting up the pi again.)  The strange thing is that even though I cannot use the Pi for anything else, the static happens for about 5-10 seconds during boot, and then the arecord settings kick in and the sound goes back to normal!  But the side effect of using the Upstart script is that I just cannot use the Pi for anything else.

     

    Is there some other way of having the Wolfson default to 192000Hz and 24bit besides using Upstart?  Maybe in the cmdline.txt file or the config.txt file?

     

    Or, maybe I am not writing the script correctly?  Below is the contents of my upstart script "setsound.conf".

     

    It would be nice if the settings could be applied at drive load time to prevent the 5-10 second burst of garbled sound.

     

     

    start on runlevel [2345] stop on runlevel [016] script   arecord -q -Dhw:0 -r 192000 -c2 -d 1 -f S24_LE /dev/null end script

     

    Also tried:

     

    start on runlevel [2345] stop on runlevel [016] script   /home/pi/Set_SPDIF_Rate.sh 192 24 end script
    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 11 years ago in reply to eveready1010

    Your scripts look OK. But...

    I think I know what happens. When the script runs at boot-up, it runs with root privileges.

    I don't know why, but when root writes to /dev/null early in the boot sequence, the permissions on it change to -rw-r--r--

    Suddenly only root can write to /dev/null! Not good. That's probably why the login (and much else) fails.

    You should be able to get a text console login prompt by pressing Ctrl-Alt-F2, though.

     

    I have observed that arecord -q -Dhw:0 -r 192000 -c2 -d 1 -f S24_LE always fails if it is the first command to the sound system after boot.

    For some reason it likes to be tickled with its default values first, so this sequence (run in a terminal) works:

    arecord -q -Dhw:0 -r 44100 -c2 -d 1 -f S16_LE  /dev/null

    arecord -q -Dhw:0 -r 192000 -c2 -d 1 -f S24_LE /dev/null

     

    I have never used Upstart myself, I'm old and set in my ways image

    So, I have started on an old-fashioned init script.

    I have quick-hacked in some code that might make it just about usable for you.

    On my RPi it works OK.

     

    #!/bin/sh
    #
    # wolfson sound card initscript
    #
    # This is still very much just the start of a work in progress.
    # For now, it only mutes/unmutes SPDIF In and Line Out
    # and sets the SPDIF rate and bit depth, using hardcoded defaults.
    #
    # TODO: Lots...
    #      A configuration file in /etc/default would be nice.
    #
    #
    ### BEGIN INIT INFO
    # Provides:          wolfson-snd
    # Required-Start:    $local_fs $remote_fs
    # Required-Stop:    $remote_fs
    # Default-Start:    S
    # Default-Stop:      0 1 6
    # Short-Description: Set Wolfson Audio Card SPDIF sampling rate and bit depth.
    # Description:       This script unmutes SPDIF In and Line Out and sets SPDIF sample rate on bootup.
    #                    It mutes SPDIF In and Line-Out on shutdown and reboot.
    ### END INIT INFO
    DEFAULT_SPDIF_RATE=96000
    DEFAULT_SPDIF_BITS=24
    
    # Don't use set -e; check exit status instead
    
    # Exit silently if ALSA package is no longer installed
    [ -x /usr/sbin/alsactl ] || exit 0
    
    PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    MYNAME=/etc/init.d/wolfson-snd
    
    . /lib/lsb/init-functions
    . /usr/share/alsa/utils.sh
    
    # $1 EXITSTATUS
    # [$2 MESSAGE]
    log_action_end_msg_and_exit()
    {
            log_action_end_msg "$1" ${2:+"$2"}
            exit $1
    }
    
    # $1 PROGRAM
    executable()
    {
            # If which is not available then we must be running before
            # /usr is mounted on a system that has which in /usr/bin/.
            # Conclude that $1 is not executable.
            [ -x /bin/which ] || [ -x /usr/bin/which ] || return 1
            which "$1" >/dev/null 2>&1
    }
    
    executable amixer  || { echo "${MYNAME}: Error: No amixer program available." >&2 ; exit 1 ; }
    executable arecord || { echo "${MYNAME}: Error: No arecord program available." >&2 ; exit 1 ; }
    
    TEMPFILE=/tmp/wolfson-snd.wav # Don't use /dev/null, it gets write-protected by root running arecord(!)
    # Get card number
    WOLFSON_CARD=`arecord -l | grep sndrpiwsp | awk '{print $2}' | sed 's/://g'`
    [ -z "$WOLFSON_CARD" ] && { echo "${MYNAME}: Error: Wolfson Sound Card not found!"  >&2 ; exit 1 ; }
    
    # Set defaults
    TARGET_CARD="hw:"${WOLFSON_CARD}
    EXITSTATUS=0
    RATE="$2"
    BITS="$3"
    [ ! -z "$4" ] && TARGET_CARD="$4"
    [ -z "$RATE" ] && RATE=${DEFAULT_SPDIF_RATE}
    [ -z "$BITS" ] && BITS=${DEFAULT_SPDIF_BITS}
    # [ -z "$TARGET_CARD" ] &&  TARGET_CARD="hw:0"
    
    case "$1" in
      start)
            log_action_begin_msg "Setting up Wolfson Audio Card ${TARGET_CARD} $RATE $BITS"
            amixer -q -D${TARGET_CARD} cset name='SPDIF in Switch'  on
    # Card needs to do something (anything?) at 44.1k for some reason before we can switch to other rates.
            arecord -q -D${TARGET_CARD} -r 44100 -c2 -d 1 -f S16_LE $TEMPFILE
            arecord -q -D${TARGET_CARD} -r ${RATE} -c2 -d 1 -f S${BITS}_LE $TEMPFILE
            amixer  -q -D${TARGET_CARD} cset name='HPOUT2 Digital Switch' on
            EXITSTATUS=$?
            rm -f $TEMPFILE
            log_action_end_msg_and_exit "$EXITSTATUS"
            ;;
      stop)
            # Mute SPDIF-in and Line-Out...
            amixer -q -D${TARGET_CARD} cset name='HPOUT2 Digital Switch' off
            amixer -q -D${TARGET_CARD} cset name='SPDIF in Switch' off
            # ... and save the muted state to keep silent during bootup.
            if MSG="$(alsactl store sndrpiwsp 2>&1)" ; then
                    EXITSTATUS=$?
                    sleep 1
            else
                    log_action_cont_msg "warning: 'alsactl store${TARGET_CARD:+ $TARGET_CARD}' failed with error message '$MSG'"
            fi
            EXITSTATUS=$?
            ;;
      restart|force-reload)
            EXITSTATUS=0
            $0 stop || EXITSTATUS=1
            $0 start || EXITSTATUS=1
            exit $EXITSTATUS
            ;;
      *)
            echo "Usage: $MYNAME {start | stop} [RATE] [BITS] [CARD]"  >&2
            exit 3
            ;;
    esac

     

    Save this script as /etc/init.d/wolfson-snd

    Only root can write in /etc/init.d, so sudo is your friend.

    Change the values in lines 23 and 24 to what you need and then run

    sudo update-rc.d  wolfson-snd start 17 S . stop 17 0 1 6 .

    The two dots are important!

     

    Then, try a reboot and cross your fingers image

    Oh, and don't forget to remove your upstart script first.

    --

    Ragnar

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • eveready1010
    0 eveready1010 over 11 years ago in reply to Former Member

    Yes!  That also worked like a charm!

     

    Thanks for the help!

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube