Advertisement
Guest User

ABX.sh

a guest
Apr 8th, 2020
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.93 KB | None | 0 0
  1. #!/usr/bin/bash
  2.  
  3. PLAYER="play -q"
  4. # `play` is from sox package; `man soxformat` for supported formats
  5.  
  6. if [[ $# <3 ]]; then
  7.     echo 'ABX test for audio files.'
  8.     echo
  9.     echo "Usage: $0  <n> <wave_A> <wave_B>"
  10.     echo '  n          - number of tests performed (n >= 10 is recommended)'
  11.     echo '  wave_{A,B} - audio files supported by PLAYER'
  12.     echo '  Make your waves shorter.'
  13.     echo
  14.     echo 'Correct results required for a 95% confidence level:'
  15.     echo 'n  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25'
  16.     echo 'CR  9   9  10  10  11  12  12  13  13  14  15  15  16  16  17  18'
  17.     echo 'In general, CR must be greater than n/2+sqrt(n).'
  18.     echo
  19.     echo "PLAYER = \"${PLAYER}\""
  20.     echo
  21.     exit 1
  22. fi
  23.  
  24. n=$1
  25. wave_A="$2"
  26. wave_B="$3"
  27. waves_AB=("$wave_A" "$wave_B")
  28. AB=("A" "B")
  29.  
  30. echo "In each test, \"${wave_A}\" then \"${wave_B}\" will be played."
  31. echo "Then you'll hear randomly selected one — \"X\"."
  32. echo "After the third sample you'll be asked for what file been played."
  33. echo 'Answer only "A" or "B" and then press Enter.'
  34.  
  35. correct_answers=0
  36.  
  37. for i in $(seq $n); do
  38.     echo
  39.     echo "Test #${i} of ${n}:"
  40.    
  41.     echo -ne "\tA | Press Enter when ready. "; read
  42.     $PLAYER "$wave_A"
  43.  
  44.     echo -ne "\tB | Press Enter when ready. "; read
  45.     $PLAYER "$wave_B"
  46.  
  47.     random=$(( $RANDOM % 2 ))
  48.     wave_X=${waves_AB[random]}
  49.     X=${AB[random]}
  50.  
  51.     echo -ne "\tX | Press Enter when ready. "; read
  52.     $PLAYER "$wave_X"
  53.  
  54.     while true; do
  55.         echo -ne '\tWas that A or B? : '
  56.         read answer
  57.         if [[ $answer =~ ^[aAbB] ]]; then
  58.             break
  59.         fi
  60.     done
  61.    
  62.     if [[ ($answer =~ ^[aA] && $X == 'A') || \
  63.           ($answer =~ ^[bB] && $X == 'B') ]]; then
  64.         correct_answers=$(( $correct_answers + 1 ))
  65.     fi
  66. done
  67.  
  68. echo
  69. echo "Congratulations! You've done all the ${n} tests!"
  70. echo "Your result is ${correct_answers}/${n}."
  71. echo "Bye."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement