create_filemagic_flac 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/env bash
  2. ## bash script to generate file magic support for flac.
  3. ## https://github.com/file/file/blob/master/magic/Magdir/audio
  4. ## below "#some common sample rates" (line 471), ie:
  5. ## >>17 belong&0xfffff0 0x2ee000 \b, 192 kHz
  6. LANG=C
  7. target=magic/Magdir/audio
  8. ## construct static list of sample rates based on standard crystal
  9. ## oscillator frequencies.
  10. ## 16.384 MHz Unknown audio application
  11. ## (16384 kHz = 32 kHz * 512 = 32 * 2^9)
  12. ## 22.5792 MHz Redbook/CD
  13. ## (22579.2 kHz = 44.1kHz * 512 = 44.1 * 2^9)
  14. ## also used: 11.2896, 16.9344, 33.8688 and 45.1584
  15. ## 24.576 MHz DAT/Video
  16. ## (24576 kHz = 48 kHz * 512 = 48 * 2^9)
  17. ## also used: 49.1520
  18. ## 33.8688 > 16.9344
  19. ## 36.864 > 18.432000
  20. declare -a a_ground_fs=(16384000 22579200 24576000)
  21. ## multiply ground clock frequencies by 1953 to get usable base
  22. ## frequencies, for instance:
  23. ## DAT/video: 24.576 MHz * 1000000 / 512 = 48000Hz
  24. ## Redbook/CD: 22.5792 MHz * 1000000 / 512 = 44100Hz
  25. ## use base rates for calculating derived rates
  26. declare -a samplerates
  27. ## min divider: fs/n
  28. def_fs_n=512
  29. min_fs_n=4
  30. ## start at base_fs/(def_fs*min_fs)
  31. ## add each derived sample rate to the array
  32. for base_fs in "${a_ground_fs[@]}"; do
  33. min_fs=$( echo "${base_fs} / ( ${def_fs_n} * ${min_fs_n} )" | bc)
  34. ## max multiplier: fs*n*min_fs
  35. max_fs_n=$(( 8 * min_fs_n ))
  36. n=${max_fs_n}
  37. while [[ ${n} -ge 1 ]]; do
  38. sample_rate=$(( min_fs * n ))
  39. samplerates+=(${sample_rate})
  40. n=$(( n / 2 ))
  41. done
  42. done
  43. declare -a stripped_rates
  44. declare -a lines
  45. for samplerate in "${samplerates[@]}"; do
  46. ## use bc with sed to convert and format Hz to kHz
  47. stripped_rate="$(LANG=C bc <<< "scale=5; ${samplerate} / 1000" | \
  48. sed 's#[0\.]*$##g')"
  49. ## only add uniq sample rates (should be necessary
  50. if [[ ! "${stripped_rates[@]}" =~ ${stripped_rate} ]]; then
  51. printf -v line ">>17\tbelong&%#-15x\t%#08x\t%s, %s kHz\n" \
  52. "16777200" \
  53. "$(( samplerate * 16 ))" \
  54. "\b" \
  55. "${stripped_rate}"
  56. stripped_rates+=("${stripped_rate}")
  57. lines+=("${line}")
  58. fi
  59. done
  60. printf "## start cutting >>> \n"
  61. ## print out the formatted lines
  62. printf "%s" "${lines[@]}" | sort -k5 -n
  63. printf "## <<< stop cutting\n"