My Music (Listen with quality headphones)

Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

Sunday, 17 February 2013

Linux bash script code to convert wav file into equal wav loops based on in-parameters

Let's say you've recorded a track in LMMS that is composed of 10 measures *of equal length*, at 140 BPM (beats per minute) and you want to convert each measure to its own wav file loop, i.e. 10 individual loop files.


The script below does exactly this.

To use this script in linux:
0) make sure you have installed sox:
sudo apt-get install sox

1) paste the bash script code below into a new file called makeloops.sh and save it somewhere, e.g. ~/Downloads/ (the Downloads folder in your home directory, i.e. /home/me/Downloads)

2) make the bash script file executable:
chmod +x ~/Downloads/makeloops.sh

3) cd to the folder where your wav file (e.g. input.wav) is, e.g.:
cd ~/lmms/projects/your-lmms-project/

4) run makeloops.sh, e.g.:
. ~/Downloads/makeloops.sh input.wav 140 10 1

###### makeloops.sh (START COPYING BELOW THIS LINE) ####

#!/bin/bash

# INPUT PARAMETERS: $1==inputfile name (e.g. input.wav), $2==bpm (e.g. 140), $3==total measures of input file (e.g. 10), $4==loop length measures (e.g. 1), $5==loop folder name (e.g. loops)
#
# e.g. say you have a wav file, input.wav, that contains 10 measures *of equal length* of 140 BPM audio and you want to convert each measure to its own wav file loop:
#
#    SEE "HELP" SECTION FOR EXAMPLE
#

EXPECTED_ARG_COUNT=5

# HELP
# IF NUMBER OF PARAMS < 5, OUTPUT HELP TEXT
if [ $# -ne $EXPECTED_ARG_COUNT ]
then
    echo "";
    echo "USAGE: makeloops.sh input-file-name BPM total-number-of-measures-in-input-file number-of-measures-for-loop-length loop-folder-name";
    echo "";
    echo "EXAMPLE: nickleus@mylaptop:/path/to/input/wav/file/$ . /path/to/makeloops.sh input.wav 140 10 1 loops";
    echo "";

else

LOOPSTART=0;
BPS=$(echo $2/60|bc -l);
BEATS=$(echo 4*$4|bc -l);
LOOPLENGTH=$(echo $BEATS/$BPS | bc -l | awk '{printf("%.7f", $1);}');    # max 7 digits to the right of the decimal place for float numbers, no leading zeros on left side of decimal place

# INPUT ($2) 140    ($3) 10    ($4) 1
#echo $BPS    # 2.33333333333333333333
#echo $BEATS    # 4
#echo $LOOPLENGTH    # 1.7142857

LOOPSFOLDER=$5;
mkdir $LOOPSFOLDER;

for (( w = 0 ; w < $3 ; w++ ));
do
sox -V4 "$1" "./$LOOPSFOLDER/$w.wav" trim $LOOPSTART $LOOPLENGTH;
LOOPSTART=$(echo $LOOPSTART+$LOOPLENGTH|bc -l);
done;

fi


###### (STOP COPYING ABOVE THIS LINE) ####


Note: If you don't want sox to run in full verbose mode, then just remove -V4; but it can be interesting to see what sox is actually doing during conversion.

Wednesday, 13 February 2013

Useful linux bash commands for audio file editing and manipulation

Here are some useful linux bash commands I use to convert between different audio file types when making my samples.


AUDIO COMMANDS


Extract audio from a flash video file (flv) and save it as a wav:
mplayer video.flv -vc null -vo null -ao pcm:fast:waveheader:file=outfile.wav


Capture a live audio stream from the system output and save it as a flac file:
jack_capture -f flac


Convert a flac file to a high quality mp3 file with a 128kbps bitrate:
flac -cd sample.flac |lame -h – sample.mp3


Convert all flac files in the current directory to high quality mp3 files with a 128kbps bitrate:
for f in *.flac;do flac -cd "$f"|lame -b 192 – "${f%.flac}.mp3";done


Convert a flac file to a wav file:
flac -d sample.flac


Convert all flac files in the current directory to wav files:
flac -d *


Convert a flac file to an ogg vorbis audio file:
oggenc sample.flac -o sample.ogg

Convert all wav files in the current directory to ogg:
for w in *.wav;do oggenc "$w" -o "${w%.wav}.ogg";done


Convert a wav file to an mp3 with 192 bitrate:
lame -b 192 sample.wav


Reversing the audio playback of a file and saving it as a new file:
sox input.flac output.flac reverse


Trim an audio file, starting e.g. at 0 and stopping at 6.85714 seconds:
sox input.wav output.wav trim 0 6.85714


Batch trim all flac files in a folder, starting e.g. at 0 and stopping at 3.47826087 seconds:
for f in *.flac;do sox "$f" "${f%.flac}_.flac" trim 0 3.47826087;done


Remove the first 0.047 seconds of an audio file:
sox input.wav output.wav trim 0.047


Combine (concatenate) all flac files in the current directory into one file:
sox --combine concatenate $(ls|grep .flac) out.flac


Add the LADSPA canyon delay effect to a whole folder of flac files in batch mode:
for f in *.flac;do ecasound -i:"$f" -o:"${f%.flac}_.flac" -el:canyon_delay,0.439,-0.14,0.439,0.5,2735.45288;done


Batch fix loop files that click at the end, by adding a fadeout effect to the very end of the file:
for w in *.wav;do sox "$w" "${w%.wav}_.wav" fade 0 $(soxi -D $w) 0.00045;done




List numbered loops files generated from "" from 0.wav...N.wav (used as in parameter for the command "Combine (concatenate) all flac files in the current directory into one file"):
ls -tr


Extract xml metadata from lmms mmpz file (e.g. to get the track measure labels you set):
lmms -d my-lmms-project.mmpz > dump.xml



VIDEO COMMANDS


Convert flash flv video to mp4:
ffmpeg -i input.flv output.mp4



On a related noted, here are my photo tutorials about music production.
My content is under the Creative Commons Attribution 3.0 license. I work hard to publish relevant and useful information in order to help people learn difficult things and find solutions to their problems, so please attribute me if you reuse my content elsewhere.