Skip to content

Instantly share code, notes, and snippets.

@katspaugh
Last active February 15, 2022 23:15
Show Gist options
  • Save katspaugh/53b07b0ee71127bc9c6b to your computer and use it in GitHub Desktop.
Save katspaugh/53b07b0ee71127bc9c6b to your computer and use it in GitHub Desktop.
A script to convert wav-files to mp3 + waveform JSONs.
$(function () {
var wavesurfer = Object.create(WaveSurfer);
var container = document.querySelector('#wave');
container.innerHTML = '';
wavesurfer.init({
container: container,
backend: 'MediaElement',
height: 40,
waveColor: '#ADADAD',
progressColor: '#29FE31',
cursorColor: '#FFFFFF',
reflection: true,
normalize: true,
barWidth: 3
});
// Bind controls
var playPause = document.querySelector('#play');
playPause.addEventListener('click', function () {
wavesurfer.playPause();
});
// Toggle play/pause text
wavesurfer.on('play', function () {
// todo
});
wavesurfer.on('pause', function () {
// todo
});
// Play on audio load
wavesurfer.on('ready', function () {
wavesurfer.play();
});
// Load track function
window.loadtrack = function (track) {
var jsonUrl = track.file.replace(/^audio/, 'output/json') + '.json';
var mp3Url = track.file.replace(/^audio/, 'output/mp3') + '.mp3';
wavesurfer.util.ajax({
responseType: 'json',
url: jsonUrl
}).on('success', function (response) {
var data = response.data;
data.unshift(data[1]);
wavesurfer.load(mp3Url, data);
});
document.querySelector('#artist').textContent = track.artist;
document.querySelector('#title').textContent = track.title;
};
});
#!/bin/bash
MP3_DIR='mp3'
JSON_DIR='json'
INPUT_DIR="$1"
OUTPUT_DIR="$2"
if [ -z "$INPUT_DIR" ]; then
echo "Specify the INPUT directory as the first argument";
echo -e "\nUsage:";
echo " waveform.sh audio output";
exit 0;
fi
if [ -z "$OUTPUT_DIR" ]; then
echo "Specify the OUTPUT directory as the second argument";
exit 0;
fi
if [ ! -d "$OUTPUT_DIR" ]; then
mkdir -p "$OUTPUT_DIR";
fi
if [ ! -d "$OUTPUT_DIR/$MP3_DIR" ]; then
mkdir "$OUTPUT_DIR/$MP3_DIR";
fi
if [ ! -d "$OUTPUT_DIR/$JSON_DIR" ]; then
mkdir "$OUTPUT_DIR/$JSON_DIR";
fi
for i in "$INPUT_DIR"/*.wav; do
OUTPUT_FILENAME=`basename "$i" | sed 's/\.wav$//'`;
avconv -i "$i" -codec:a libmp3lame -qscale:a 2 "$OUTPUT_DIR/$MP3_DIR/$OUTPUT_FILENAME.mp3";
audiowaveform -i "$i" --pixels-per-second 10 -b 8 -o "$OUTPUT_DIR/$JSON_DIR/$OUTPUT_FILENAME.json";
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment