Any Linux audio gurus out there?
Any Linux audio gurus out there?
Author
Discussion

TonyRPH

Original Poster:

13,554 posts

199 months

Tuesday 28th August 2018
quotequote all
I have an odd issue.

I have written a script that will generate a sine tone using sox (play), which is played into a sound card.

This tone is then read from the sound card using sox and written to a .wav file.

I then run 'sndfile-info' on trhe .wav file to read back the amplitude.

However...

If I switch between a low(ish) frequency back to a high frequency, the initial level read from the high frequency is the same as the low frequency (but the frequency stored in the .wav is correct!).

Running it a second time result sin the correct amplitude.

Here is my (test!) script: (couldn't use code tags because my script has braces in it)

You'll note that I'm reading the amplitude using two different methods (sox -n stat and sndfile-info) because I thought maybe one of them was wrong....

script is run like this: ./test.sh <duration in secs> <frequency>

e.g. ./test.sh 1 1000 (1 second, 1 kHz)

script said:
#!/bin/bash

outfile=/tmp/output.wav
duration="$1"
toneduration=$(($duration + 1))
freq="$2"
gain="-1"
playback_dev="hw:1,0" # HiFace spdif
record_dev="hw:0,0"
rm -f $datafile $outfile
rm -f ${freq}_fr.wav
AUDIODEV=$playback_dev play --buffer 4194304 -q --norm=-1.1 -b 32 -r 192k -n synth $toneduration sine $freq|sox -q --buffer 4194304 -b 32 -r 192k -c 2 -t alsa $record_dev ${freq}_fr.wav trim 0 $duration
sleep 1

amplitude=$(sox ${freq}_fr.wav -n stat 2>&1 | sed -n 's#^Maximum amplitude:[^0-9]*\([0-9.]*\)$#\1#p')
db=$(echo "scale=3; 20*l($amplitude)/l(10)" | bc -l)
db2=$(sndfile-info ${freq}_fr.wav |grep "Signal Max" |cut -d "(" -f2 | cut -d "d" -f1)
echo $freq, $db, $db2
rm -f $infile $outfile
Sample output:


./test.sh 1 81920
81920, -17.080, -17.07 <<<<< correct

./test.sh 1 8192
8192, -1.172, -1.17 <<<< correct

./test.sh 1 81920
81920, -1.181, -1.18 <<<< incorrect!

./test.sh 1 81920
81920, -17.072, -17.07 <<<<< correct!



Any ideas??

Thanks in advance.

scorp

8,783 posts

260 months

Wednesday 29th August 2018
quotequote all
Is it something to do with the --norm parameter ? That will normalise volume(power), which will result in lower peak-peak amplitude for higher frequencies.

Amplitude has a relationship with frequency when considering volume: http://www.indiana.edu/~emusic/etext/acoustics/cha...

I think the play command uses that iso226 curve when normalising.

Since you are interested in amplitude and not volume just remove the --norm switch. Hopefully you will get what you're expecting.


Edited by scorp on Wednesday 29th August 10:06

TonyRPH

Original Poster:

13,554 posts

199 months

Wednesday 29th August 2018
quotequote all
Thanks, I think I solved it by using 'aply' and 'arecord' instead.

I still use Sox to generate the tone (saved to a file) however.


# generate the source file to playback
sox -q -b 32 -r 192k -c 2 -n $infile synth $duration sine $i gain $gain # where $i is frequency

# play back and record the source file
aplay -q -D $playback_dev $infile|arecord -q -D plughw:0 -f S32_LE -t wav -r 192k -d $duration $outfile



I can seemingly get consistent results now, when switching between frequencies.

I guess that using 'gain -1' instead of '--norm=-1.1' is the solution, however 'play' doesn't seem to support 'gain'

script said:
./calib.sh 1 80000
80000, -15.960, -15.96

./calib.sh 1 80000
80000, -16.038, -15.74

./calib.sh 1 8000
8000, -1.120, -1.13

./calib.sh 1 8000
8000, -1.120, -1.13

./calib.sh 1 80000
80000, -15.960, -15.96

./calib.sh 1 80000
80000, -16.038, -15.89
That seems pretty consistent now - thanks for your help.