1#!/bin/bash
2
3#
4# MOC - music on console
5# Copyright (C) 2004-2005 Damian Pietras <daper@daper.net>
6#
7# maketests.sh Copyright (C) 2012 John Fitzgerald
8#
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2 of the License, or
12# (at your option) any later version.
13#
14
15#
16# TODO: - Add other supported formats.
17#
18
19AUDIO=false
20VIDEO=false
21FFMPEG="$(which avconv 2>/dev/null || which ffmpeg 2>/dev/null)"
22SOX="$(which sox 2>/dev/null)"
23SYNTH="synth 10 sine 440 vol 0.5"
24
25# Clean error termination.
26function die {
27  echo $@ > /dev/stderr
28  exit 1
29}
30
31[[ -x "$SOX" ]] || die This script requires the SoX package.
32
33# Provide usage information.
34function usage () {
35  echo "Usage: ${0##*/} [-a] [--audio] [-h] [--video] [FORMAT ...]"
36}
37
38# Provide help information.
39function help () {
40  echo
41  echo "MOC test file generation tool"
42  echo
43  usage
44  echo
45  echo "  -a|--all        Generate all formats"
46  echo "     --audio      Generate all audio formats"
47  echo "  -h|--help       This help information"
48  echo "     --video      Generate all video formats"
49  echo
50  echo "Supported audio formats: flac mp3 vorbis wav"
51  echo "Supported video formats: vob"
52  echo
53}
54
55# Generate FLAC audio test files.
56function flac {
57  echo "Generating FLAC audio test files"
58  for r in 8000 16000 24000 32000 48000 96000 192000 \
59           11025 22050 44100 88200 176400
60  do
61    for b in 16 32
62    do
63      $SOX -b$b -c1 -r$r -e signed -n -L sinewave-s${b}le-1-$r.flac $SYNTH
64      $SOX -b$b -c2 -r$r -e signed -n -L sinewave-s${b}le-2-$r.flac $SYNTH
65    done
66  done
67}
68
69# Generate MP3 audio test files.
70function mp3 {
71  echo "Generating MP3 audio test files"
72  for r in 8000 16000 24000 32000 48000 11025 22050 44100
73  do
74    for c in 1 2
75    do
76      $SOX -b8 -c$c -r$r -n sinewave-u8-$c-$r.mp3 $SYNTH
77      for b in 16 24 32
78      do
79        $SOX -b$b -c$c -r$r -n -L sinewave-s${b}le-$c-$r.mp3 $SYNTH
80        $SOX -b$b -c$c -r$r -n -B sinewave-s${b}be-$c-$r.mp3 $SYNTH
81      done
82    done
83  done
84}
85
86# Generate VOB video test files.
87function vob {
88  [[ -x "$FFMPEG" ]] || return
89  echo "Generating VOB video test files"
90  for r in 16000 22050 24000 32000 44100 48000
91  do
92    for c in 1 2
93    do
94      $FFMPEG -f rawvideo -pix_fmt yuv420p -s 320x240 -r 30000/1001 \
95              -i /dev/zero \
96              -f s16le -c pcm_s16le -ac 2 -ar 48000 \
97              -i <($SOX -q -b16 -c2 -r 48000 -e signed -n -L -t s16 - $SYNTH) \
98              -vcodec mpeg2video -acodec mp2 -shortest -ac $c -ar $r \
99              -y sinewave-s16le-$c-$r.vob > /dev/null  2>&1
100    done
101  done
102}
103
104# Generate Ogg/Vorbis audio test files.
105function vorbis {
106  echo "Generating Ogg/Vorbis audio test files"
107  for r in 8000 16000 24000 32000 48000 96000 192000 \
108           11025 22050 44100 88200 176400
109  do
110    $SOX -b16 -c1 -r$r -e signed -n -L sinewave-s16le-1-$r.ogg $SYNTH
111    $SOX -b16 -c2 -r$r -e signed -n -L sinewave-s16le-2-$r.ogg $SYNTH
112  done
113}
114
115# Generate WAV audio test files.
116function wav {
117  echo "Generating WAV audio test files"
118  for r in 8000 16000 24000 32000 48000 96000 192000 \
119           11025 22050 44100 88200 176400
120  do
121    for c in 1 2
122    do
123      $SOX -b8 -c$c -r$r -n sinewave-u8-$c-$r.wav $SYNTH
124      $SOX -b16 -c$c -r$r -n -B sinewave-s16be-$c-$r.wav $SYNTH
125      for b in 16 24 32
126      do
127        $SOX -b$b -c$c -r$r -n -L sinewave-s${b}le-$c-$r.wav $SYNTH
128      done
129    done
130  done
131}
132
133# Directory safety check.
134ls sinewave-* > /dev/null 2>&1 && {
135  echo
136  echo "This script generates many filenames starting with 'sinewave-' in the"
137  echo "current directory which already contains similarly named files."
138  echo "Running it in this directory is a really, really bad idea.  (In fact,"
139  echo "it's probably not wise to run this script in a non-empty directory at"
140  echo "all.)  So we're aborting in the interests of safety."
141  echo
142  exit 1
143} > /dev/stderr
144
145# Process command line options.
146for OPTS
147do
148  case $1 in
149   -a|--all) AUDIO=true
150             VIDEO=true
151             ;;
152    --audio) AUDIO=true
153             ;;
154  -h|--help) help
155             exit 0
156             ;;
157    --video) VIDEO=true
158             ;;
159         -*) echo Unrecognised option: $1
160             usage > /dev/stderr
161             exit 1
162             ;;
163          *) break
164             ;;
165  esac
166  shift
167done
168
169# Generate all audio formats.
170$AUDIO && {
171  flac
172  mp3
173  vorbis
174  wav
175}
176
177# Generate all video formats.
178$VIDEO && {
179  vob
180}
181
182# Generate specified formats.
183for ARGS
184do
185  case $1 in
186  flac|mp3|vorbis|wav)
187      $1
188      ;;
189  vob)
190      $1
191      ;;
192  *)  echo "*** Unsupported format: $1"
193      echo
194      ;;
195  esac
196  shift
197done
198
199exit 0
200