[ksh93-integration-discuss] Small toy/demo: "audiotest.ksh"
Roland Mainz
roland.mainz at nrubsig.org
Tue May 22 20:54:14 PDT 2007
Hi!
----
Attached is a small sample demo/toy script ("audiotest.ksh.txt") which
should demonstrate the usage of binary variables in ksh93 (>= ksh93s+)
via creating an audio sample and sending it to the audio device...
... can anyone with a SunRay server test whether the attached demo
script works on a SunRay terminal (speakers required), please ?
----
Bye,
Roland
--
__ . . __
(o.\ \/ /.o) roland.mainz at nrubsig.org
\__\/\/__/ MPEG specialist, C&&JAVA&&Sun&&Unix programmer
/O /==\ O\ TEL +49 641 7950090
(;O/ \/ \O;)
-------------- next part --------------
# ast-ksh.2007-04-18 has a bug which screws-up 'printf "%B"' for non-C locales
export LC_ALL=C
typeset -b binvar
typeset -r base64chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
float -A -r notes=(
["0"]="8.176" ["C-2"]="8.176" ["C-1"]="8.176" ["C,,,"]="8.176" ["C,,,"]="8.176"
["1"]="8.662" ["C#-2"]="8.662" ["C#-1"]="8.662" ["Cis,,,"]="8.662" ["Des,,,"]="8.662"
["2"]="9.177" ["D-2"]="9.177" ["D-1"]="9.177" ["D,,,"]="9.177" ["D,,,"]="9.177"
["3"]="9.723" ["D#-2"]="9.723" ["D#-1"]="9.723" ["Dis,,,"]="9.723" ["Es,,,"]="9.723"
["4"]="10.301" ["E-2"]="10.301" ["E-1"]="10.301" ["E,,,"]="10.301" ["E,,,"]="10.301"
["5"]="10.913" ["F-2"]="10.913" ["F-1"]="10.913" ["F,,,"]="10.913" ["F,,,"]="10.913"
["6"]="11.562" ["F#-2"]="11.562" ["F#-1"]="11.562" ["Fis,,,"]="11.562" ["Ges,,,"]="11.562"
["7"]="12.250" ["G-2"]="12.250" ["G-1"]="12.250" ["G,,,"]="12.250" ["G,,,"]="12.250"
["8"]="12.978" ["G#-2"]="12.978" ["G#-1"]="12.978" ["Gis,,,"]="12.978" ["As,,,"]="12.978"
["9"]="13.750" ["A-2"]="13.750" ["A-1"]="13.750" ["A,,,"]="13.750" ["A,,,"]="13.750"
["10"]="14.568" ["A#-2"]="14.568" ["A#-1"]="14.568" ["Ais,,,"]="14.568" ["B,,,"]="14.568"
["11"]="15.434" ["B-2"]="15.434" ["B-1"]="15.434" ["H,,,"]="15.434" ["H,,,"]="15.434"
["12"]="16.352" ["C-1"]="16.352" ["C0"]="16.352" ["C,,"]="16.352" ["C,,"]="16.352"
["13"]="17.324" ["C#-1"]="17.324" ["C#0"]="17.324" ["Cis,,"]="17.324" ["Des,,"]="17.324"
["14"]="18.354" ["D-1"]="18.354" ["D0"]="18.354" ["D,,"]="18.354" ["D,,"]="18.354"
["15"]="19.445" ["D#-1"]="19.445" ["D#0"]="19.445" ["Dis,,"]="19.445" ["Es,,"]="19.445"
["16"]="20.601" ["E-1"]="20.601" ["E0"]="20.601" ["E,,"]="20.601" ["E,,"]="20.601"
["17"]="21.826" ["F-1"]="21.826" ["F0"]="21.826" ["F,,"]="21.826" ["F,,"]="21.826"
["18"]="23.124" ["F#-1"]="23.124" ["F#0"]="23.124" ["Fis,,"]="23.124" ["Ges,,"]="23.124"
["19"]="24.499" ["G-1"]="24.499" ["G0"]="24.499" ["G,,"]="24.499" ["G,,"]="24.499"
["20"]="25.956" ["G#-1"]="25.956" ["G#0"]="25.956" ["Gis,,"]="25.956" ["As,,"]="25.956"
["21"]="27.500" ["A-1"]="27.500" ["A0"]="27.500" ["A,,"]="27.500" ["A,,"]="27.500"
["22"]="29.135" ["A#-1"]="29.135" ["A#0"]="29.135" ["Ais,,"]="29.135" ["B,,"]="29.135"
["23"]="30.867" ["B-1"]="30.867" ["B0"]="30.867" ["H,,"]="30.867" ["H,,"]="30.867"
["24"]="32.703" ["C0"]="32.703" ["C1"]="32.703" ["C,"]="32.703" ["C,"]="32.703"
["25"]="34.648" ["C#0"]="34.648" ["C#1"]="34.648" ["Cis,"]="34.648" ["Des,"]="34.648"
["26"]="36.708" ["D0"]="36.708" ["D1"]="36.708" ["D,"]="36.708" ["D,"]="36.708"
["27"]="38.890" ["D#0"]="38.890" ["D#1"]="38.890" ["Dis,"]="38.890" ["Es,"]="38.890"
["28"]="41.203" ["E0"]="41.203" ["E1"]="41.203" ["E"]="41.203" ["E"]="41.203"
["29"]="43.653" ["F0"]="43.653" ["F1"]="43.653" ["F,"]="43.653" ["F,"]="43.653"
["30"]="46.249" ["F#0"]="46.249" ["F#1"]="46.249" ["Fis,"]="46.249" ["Ges,"]="46.249"
["31"]="48.999" ["G0"]="48.999" ["G1"]="48.999" ["G,"]="48.999" ["G,"]="48.999"
["32"]="51.913" ["G#0"]="51.913" ["G#1"]="51.913" ["Gis,"]="51.913" ["As,"]="51.913"
["33"]="55.000" ["A0"]="55.000" ["A1"]="55.000" ["A,"]="55.000" ["A,"]="55.000"
["34"]="58.270" ["A#0"]="58.270" ["A#1"]="58.270" ["Ais,"]="58.270" ["B,"]="58.270"
["35"]="61.735" ["B0"]="61.735" ["B1"]="61.735" ["H,"]="61.735" ["H,"]="61.735"
["36"]="65.406" ["C1"]="65.406" ["C2"]="65.406" ["C"]="65.406" ["C"]="65.406"
["37"]="69.295" ["C#1"]="69.295" ["C#2"]="69.295" ["Cis"]="69.295" ["Des"]="69.295"
["38"]="73.416" ["D1"]="73.416" ["D2"]="73.416" ["D"]="73.416" ["D"]="73.416"
["39"]="77.781" ["D#1"]="77.781" ["D#2"]="77.781" ["Dis"]="77.781" ["Es"]="77.781"
["40"]="82.406" ["E1"]="82.406" ["E2"]="82.406" ["E"]="82.406" ["E"]="82.406"
["41"]="87.307" ["F1"]="87.307" ["F2"]="87.307" ["F"]="87.307" ["F"]="87.307"
["42"]="92.499" ["F#1"]="92.499" ["F#2"]="92.499" ["Fis"]="92.499" ["Ges"]="92.499"
["43"]="97.998" ["G1"]="97.998" ["G2"]="97.998" ["G"]="97.998" ["G"]="97.998"
["44"]="103.82" ["G#1"]="103.82" ["G#2"]="103.82" ["Gis"]="103.82" ["As"]="103.82"
["45"]="110.00" ["A1"]="110.00" ["A2"]="110.00" ["A"]="110.00" ["A"]="110.00"
["46"]="116.56" ["A#1"]="116.56" ["A#2"]="116.56" ["Ais"]="116.56" ["B"]="116.56"
["47"]="123.47" ["B1"]="123.47" ["B2"]="123.47" ["H"]="123.47" ["H"]="123.47"
["48"]="130.81" ["C2"]="130.81" ["C3"]="130.81" ["c"]="130.81" ["c"]="130.81"
["49"]="138.59" ["C#2"]="138.59" ["C#3"]="138.59" ["cis"]="138.59" ["des"]="138.59"
["50"]="146.83" ["D2"]="146.83" ["D3"]="146.83" ["d"]="146.83" ["d"]="146.83"
["51"]="155.56" ["D#2"]="155.56" ["D#3"]="155.56" ["dis"]="155.56" ["es"]="155.56"
["52"]="164.81" ["E2"]="164.81" ["E3"]="164.81" ["e"]="164.81" ["e"]="164.81"
["53"]="174.61" ["F2"]="174.61" ["F3"]="174.61" ["f"]="174.61" ["f"]="174.61"
["54"]="184.99" ["F#2"]="184.99" ["F#3"]="184.99" ["fis"]="184.99" ["ges"]="184.99"
["55"]="195.99" ["G2"]="195.99" ["G3"]="195.99" ["g"]="195.99" ["g"]="195.99"
["56"]="207.65" ["G#2"]="207.65" ["G#3"]="207.65" ["gis"]="207.65" ["as"]="207.65"
["57"]="220.00" ["A2"]="220.00" ["A3"]="220.00" ["a"]="220.00" ["a"]="220.00"
["58"]="233.08" ["A#2"]="233.08" ["A#3"]="233.08" ["ais"]="233.08" ["b"]="233.08"
["59"]="246.94" ["B2"]="246.94" ["B3"]="246.94" ["h"]="246.94" ["h"]="246.94"
["60"]="261.63" ["C3"]="261.63" ["C4"]="261.63" ["c'"]="261.63" ["c'"]="261.63"
["61"]="277.18" ["C#3"]="277.18" ["C#4"]="277.18" ["cis'"]="277.18" ["des'"]="277.18"
["62"]="293.66" ["D3"]="293.66" ["D4"]="293.66" ["d'"]="293.66" ["d'"]="293.66"
["63"]="311.13" ["D#3"]="311.13" ["D#4"]="311.13" ["dis'"]="311.13" ["es'"]="311.13"
["64"]="329.63" ["E3"]="329.63" ["E4"]="329.63" ["e'"]="329.63" ["e'"]="329.63"
["65"]="349.23" ["F3"]="349.23" ["F4"]="349.23" ["f'"]="349.23" ["f'"]="349.23"
["66"]="369.99" ["F#3"]="369.99" ["F#4"]="369.99" ["fis'"]="369.99" ["ges'"]="369.99"
["67"]="391.99" ["G3"]="391.99" ["G4"]="391.99" ["g'"]="391.99" ["g'"]="391.99"
["68"]="415.31" ["G#3"]="415.31" ["G#4"]="415.31" ["gis'"]="415.31" ["as'"]="415.31"
["69"]="440.00" ["A3"]="440.00" ["A4"]="440.00" ["a'"]="440.00" ["a'"]="440.00"
["70"]="466.16" ["A#3"]="466.16" ["A#4"]="466.16" ["ais'"]="466.16" ["b'"]="466.16"
["71"]="493.88" ["B3"]="493.88" ["B4"]="493.88" ["h'"]="493.88" ["h'"]="493.88"
["72"]="523.25" ["C4"]="523.25" ["C5"]="523.25" ["c''"]="523.25" ["c''"]="523.25"
["73"]="554.37" ["C#4"]="554.37" ["C#5"]="554.37" ["cis''"]="554.37" ["des''"]="554.37"
["74"]="587.33" ["D4"]="587.33" ["D5"]="587.33" ["d''"]="587.33" ["d''"]="587.33"
["75"]="622.25" ["D#4"]="622.25" ["D#5"]="622.25" ["dis''"]="622.25" ["es''"]="622.25"
["76"]="659.26" ["E4"]="659.26" ["E5"]="659.26" ["e''"]="659.26" ["e''"]="659.26"
["77"]="698.46" ["F4"]="698.46" ["F5"]="698.46" ["f''"]="698.46" ["f''"]="698.46"
["78"]="739.99" ["F#4"]="739.99" ["F#5"]="739.99" ["fis''"]="739.99" ["ges''"]="739.99"
["79"]="783.99" ["G4"]="783.99" ["G5"]="783.99" ["g''"]="783.99" ["g''"]="783.99"
["80"]="830.61" ["G#4"]="830.61" ["G#5"]="830.61" ["gis''"]="830.61" ["as''"]="830.61"
["81"]="880.00" ["A4"]="880.00" ["A5"]="880.00" ["a''"]="880.00" ["a''"]="880.00"
["82"]="932.32" ["A#4"]="932.32" ["A#5"]="932.32" ["ais''"]="932.32" ["b''"]="932.32"
["83"]="987.77" ["B4"]="987.77" ["B5"]="987.77" ["h''"]="987.77" ["h''"]="987.77"
["84"]="1046.5" ["C5"]="1046.5" ["C6"]="1046.5" ["c'''"]="1046.5" ["c'''"]="1046.5"
["85"]="1108.7" ["C#5"]="1108.7" ["C#6"]="1108.7" ["cis'''"]="1108.7" ["des'''"]="1108.7"
["86"]="1174.7" ["D5"]="1174.7" ["D6"]="1174.7" ["d'''"]="1174.7" ["d'''"]="1174.7"
["87"]="1244.5" ["D#5"]="1244.5" ["D#6"]="1244.5" ["dis'''"]="1244.5" ["es'''"]="1244.5"
["88"]="1318.5" ["E5"]="1318.5" ["E6"]="1318.5" ["e'''"]="1318.5" ["e'''"]="1318.5"
["89"]="1396.9" ["F5"]="1396.9" ["F6"]="1396.9" ["f'''"]="1396.9" ["f'''"]="1396.9"
["90"]="1480.0" ["F#5"]="1480.0" ["F#6"]="1480.0" ["fis'''"]="1480.0" ["ges'''"]="1480.0"
["91"]="1568.0" ["G5"]="1568.0" ["G6"]="1568.0" ["g'''"]="1568.0" ["g'''"]="1568.0"
["92"]="1661.2" ["G#5"]="1661.2" ["G#6"]="1661.2" ["gis'''"]="1661.2" ["as'''"]="1661.2"
["93"]="1760.0" ["A5"]="1760.0" ["A6"]="1760.0" ["a'''"]="1760.0" ["a'''"]="1760.0"
["94"]="1864.7" ["A#5"]="1864.7" ["A#6"]="1864.7" ["ais'''"]="1864.7" ["b'''"]="1864.7"
["95"]="1975.5" ["B5"]="1975.5" ["B6"]="1975.5" ["h'''"]="1975.5" ["h'''"]="1975.5"
["96"]="2093.0" ["C6"]="2093.0" ["C7"]="2093.0" ["c''''"]="2093.0" ["c''''"]="2093.0"
["97"]="2217.5" ["C#6"]="2217.5" ["C#7"]="2217.5" ["cis''''"]="2217.5" ["des''''"]="2217.5"
["98"]="2349.3" ["D6"]="2349.3" ["D7"]="2349.3" ["d''''"]="2349.3" ["d''''"]="2349.3"
["99"]="2489.0" ["D#6"]="2489.0" ["D#7"]="2489.0" ["dis''''"]="2489.0" ["es''''"]="2489.0"
["100"]="2637.0" ["E6"]="2637.0" ["E7"]="2637.0" ["e''''"]="2637.0" ["e''''"]="2637.0"
["101"]="2793.8" ["F6"]="2793.8" ["F7"]="2793.8" ["f''''"]="2793.8" ["f''''"]="2793.8"
["102"]="2960.0" ["F#6"]="2960.0" ["F#7"]="2960.0" ["fis''''"]="2960.0" ["ges''''"]="2960.0"
["103"]="3136.0" ["G6"]="3136.0" ["G7"]="3136.0" ["g''''"]="3136.0" ["g''''"]="3136.0"
["104"]="3322.4" ["G#6"]="3322.4" ["G#7"]="3322.4" ["gis''''"]="3322.4" ["as''''"]="3322.4"
["105"]="3520.0" ["A6"]="3520.0" ["A7"]="3520.0" ["a''''"]="3520.0" ["a''''"]="3520.0"
["106"]="3729.3" ["A#6"]="3729.3" ["A#7"]="3729.3" ["ais''''"]="3729.3" ["b''''"]="3729.3"
["107"]="3951.1" ["B6"]="3951.1" ["B7"]="3951.1" ["h''''"]="3951.1" ["h''''"]="3951.1"
["108"]="4186.0" ["C7"]="4186.0" ["C8"]="4186.0" ["c'''''"]="4186.0" ["c'''''"]="4186.0"
["109"]="4434.9" ["C#7"]="4434.9" ["C#8"]="4434.9" ["cis'''''"]="4434.9" ["des'''''"]="4434.9"
["110"]="4698.6" ["D7"]="4698.6" ["D8"]="4698.6" ["d'''''"]="4698.6" ["d'''''"]="4698.6"
["111"]="4978.0" ["D#7"]="4978.0" ["D#8"]="4978.0" ["dis'''''"]="4978.0" ["es'''''"]="4978.0"
["112"]="5274.0" ["E7"]="5274.0" ["E8"]="5274.0" ["e'''''"]="5274.0" ["e'''''"]="5274.0"
["113"]="5587.7" ["F7"]="5587.7" ["F8"]="5587.7" ["f'''''"]="5587.7" ["f'''''"]="5587.7"
["114"]="5919.9" ["F#7"]="5919.9" ["F#8"]="5919.9" ["fis'''''"]="5919.9" ["ges'''''"]="5919.9"
["115"]="6271.9" ["G7"]="6271.9" ["G8"]="6271.9" ["g'''''"]="6271.9" ["g'''''"]="6271.9"
["116"]="6644.9" ["G#7"]="6644.9" ["G#8"]="6644.9" ["gis'''''"]="6644.9" ["as'''''"]="6644.9"
["117"]="7040.0" ["A7"]="7040.0" ["A8"]="7040.0" ["a'''''"]="7040.0" ["a'''''"]="7040.0"
["118"]="7458.6" ["A#7"]="7458.6" ["A#8"]="7458.6" ["ais'''''"]="7458.6" ["b'''''"]="7458.6"
["119"]="7902.1" ["B7"]="7902.1" ["B8"]="7902.1" ["h'''''"]="7902.1" ["h'''''"]="7902.1"
["120"]="8372.0" ["C8"]="8372.0" ["C9"]="8372.0" ["c''''''"]="8372.0" ["c''''''"]="8372.0"
["121"]="8869.8" ["C#8"]="8869.8" ["C#9"]="8869.8" ["cis''''''"]="8869.8" ["des''''''"]="8869.8"
["122"]="9397.3" ["D8"]="9397.3" ["D9"]="9397.3" ["d''''''"]="9397.3" ["d''''''"]="9397.3"
["123"]="9956.1" ["D#8"]="9956.1" ["D#9"]="9956.1" ["dis''''''"]="9956.1" ["es''''''"]="9956.1"
["124"]="10548.1" ["E8"]="10548.1" ["E9"]="10548.1" ["e''''''"]="10548.1" ["e''''''"]="10548.1"
["125"]="11175.3" ["F8"]="11175.3" ["F9"]="11175.3" ["f''''''"]="11175.3" ["f''''''"]="11175.3"
["126"]="11839.8" ["F#8"]="11839.8" ["F#9"]="11839.8" ["fis''''''"]="11839.8" ["ges''''''"]="11839.8"
["127"]="12543.9" ["G8"]="12543.9" ["G9"]="12543.9" ["g''''''"]="12543.9" ["g''''''"]="12543.9"
)
function filenametobytearray
{
nameref bytearray=$2
typeset filename="$1"
[ -z "$filename" ] && { print -n2 "missing filename" ; exit 1 ; }
[ ! -f "$filename" ] && { print -n2 "file not found" ; exit 1 ; }
[ ! -r "$filename" ] && { print -n2 "file not readable" ; exit 1 ; }
cat "$filename" |
od -t x1 |
while read a b ; do
print "$b" ;
done |
(LC_ALL=C /usr/bin/tr '[:blank:]' '\n') |
while read i ; do
bytearray+=( "0x0${i}" )
done
}
function bytearraytobase64
{
nameref bytearray=$1
typeset out=""
integer len
integer i i0 i1 i2
(( len=${#bytearray[*]}-1 ))
for (( i=0 ; i < len ; i+=3 )) ; do
(( i0=bytearray[i+0] , i1=bytearray[i+1] , i2=bytearray[i+2] ))
out+="${base64chars:$(( i0 >> 2 )):1}"
out+="${base64chars:$(( ((i0 & 0x03) << 4) | ((i1 & 0xf0) >> 4) )):1}"
(( (i+1) < len )) && { out+="${base64chars:$(( ((i1 & 0x0f) << 2) | ((i2 & 0xc0) >> 6) )):1}" ; } || out+="="
(( (i+2) < len )) && { out+="${base64chars:$(( i2 & 0x3f )):1}" ; } || out+="="
done
printf "%s" "${out}"
}
function testbytearraytobase64
{
typeset -a -i ba
filenametobytearray "$1" ba
binvar="$(bytearraytobase64 ba)"
x1=$(cat "$1" | openssl md5)
x2=$(printf "%B" binvar | openssl md5)
if [ "$x1" != "$x2" ] ; then
print "difference: $x1 vs. $x2"
fi
}
# stack layout:
# stack.currpos = current position
# stack.data = nameref to integer array
function stack_init
{
nameref stk=$1
stk.currpos=0
}
function stack_put_byte
{
nameref stk=$1
stk.data[stk.currpos++]=$(( $2 & 0xFF ))
}
function stack_put_uint16
{
stack_put_byte $1 $(( ($2 >> 8) & 0xFF ))
stack_put_byte $1 $(( $2 & 0xFF ))
}
typeset audio=( typeset -i currpos=0 ; typeset -a -i data=( [0]=0 ) )
stack_init audio
# au_magic: magic number
stack_put_byte audio $(('.'))
stack_put_byte audio $(('s'))
stack_put_byte audio $(('n'))
stack_put_byte audio $(('d'))
# au_offset
stack_put_byte audio 0x00
stack_put_byte audio 0x00
stack_put_byte audio 0x00
stack_put_byte audio 0x1C
# au_data_size (0xFFFFFFFF = AUDIO_AU_UNKNOWN_SIZE ((unsigned)(~0)))
stack_put_byte audio 0xFF
stack_put_byte audio 0xFF
stack_put_byte audio 0xFF
stack_put_byte audio 0xFF
# au_encoding
stack_put_byte audio 0x00
stack_put_byte audio 0x00
stack_put_byte audio 0x00
stack_put_byte audio 1
# au_sample_rate
stack_put_byte audio 0x00
stack_put_byte audio 0x00
stack_put_byte audio 0x1f
stack_put_byte audio 0x40
# au_channels
stack_put_byte audio 0x00
stack_put_byte audio 0x00
stack_put_byte audio 0x00
stack_put_byte audio 0x01
# dummy
stack_put_byte audio 0
stack_put_byte audio 0
stack_put_byte audio 0
stack_put_byte audio 0
integer i j
float sample_rate
float freq
float duration=0.10
integer p
sample_rate=8000
# warning: the math used here is so wrong that your head my explode
# when you continue reading this
for note in C3 D3 E3 F3 G3 G3 A3 A3 A3 A3 G3 p F3 F3 F3 F3 E3 E3 p D3 D3 D3 D3 C3 ; do
if [ $note == "p" ] ; then
(( freq=1*(1./duration) ))
else
freq=${notes["${note}"]}
fi
(( p=(sample_rate/freq)-1 ))
printf "note=%s sample_rate=%f, freq=%f, p=%d\n" "$note" $sample_rate $freq $p
for ((i=0 ; i < ((sample_rate*duration)/p) ; i++)) ; do
stack_put_byte audio 255
for (( j=0 ; j < p ; j++ )) ; do
stack_put_byte audio 0
done
done
for ((i=0 ; i < ((sample_rate*duration)/2.) ; i++)) ; do
stack_put_byte audio 0
done
done
print "Converting..."
binvar=$(bytearraytobase64 audio.data)
if [ "${AUDIODEV}" = "" ] ; then
AUDIODEV="/dev/audio"
fi
print "Playing sound to device ${AUDIODEV}"
printf "%B" binvar >"${AUDIODEV}"
#EOF.
More information about the ksh93-integration-discuss
mailing list