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