HydrogenAudio

Hydrogenaudio Forum => General Audio => Topic started by: krafty on 2011-08-18 00:33:57

Title: Help with shntool !
Post by: krafty on 2011-08-18 00:33:57
This is what I want to achieve:

I want shntool to output a cuesheet with automatic placement of tracks of a given length, using an existing WAV file.
I was playing with it and I was able to split the WAV file itself in slices of 1:00 minute each, but it gave me several files. What I want is the same thing but output to a cuesheet. Any help from the non-unix and unix experts will be appreciated!
Title: Help with shntool !
Post by: DVDdoug on 2011-08-18 01:16:48
I don't know how to do that automatically, but if you had started typing into a text editor, you'd be done by now!

If you want to do that with a 1000 minute file, I suppose you could use a spreadsheet to increment the times, and save-as a text file (and rename to .CUE).
Title: Help with shntool !
Post by: krafty on 2011-08-18 01:25:02
I'm thinking about a bash script....
Title: Help with shntool !
Post by: mjb2006 on 2011-08-18 03:39:44
You were on the right track. shntool's cue sheet generator needs separate files as input, so splitting was the right thing to do.

You could combine the first two commands into one:

I'm not sure if you can do the del after that on the same line. Anyone?

If you needed to do it with an MP3 instead of a lossless source, then this is how I'd do it:
Title: Help with shntool !
Post by: krafty on 2011-08-18 16:39:11
Quote
I'm not sure if you can do the del after that on the same line. Anyone?


I just did it... (I'm on Linux so that could explain if Windows didn't let you?)


shntool split -q -l 1:00 "policy.wav" && shntool cue split-track*.wav > policy.cue && rm split-track*.wav

Now the issue is... to rename the "joined.wav" FILE statement within the cuesheet to inherit the intended WAV filename!
Title: Help with shntool !
Post by: krafty on 2011-08-18 20:30:46
Well, I am going to try to write down a kind of algorithm here so anyone could help me with a bash script... invoked from KDE or GNOME right-click menu "Open With ThisScript..."

- When clicking on the file "thisfile.wav" and opening the context menu and choosing "Open With ThisScript..." would start the script PROCESSING the selected file (not any just .wav in the directory, which will contain many .wav files)

1) The script first needs to grab the file name and store it on a variable ("thisfile.wav")
2) Then shntool must process the file just like the line in the post above....

Quote
shntool split -l 1:00 $WAVFILE && shntool cue split-track*.wav > $WAVFILE.cue && rm split-track*.wav


3) and as a last part, a tool should replace the automatic "joined.wav" FILE statemente within the cuesheet, using the $WAVFILE instead.


So it will end with two files:
thisfile.wav
thisfile.cue  (and inside the cuesheet we would have "thisfile.wav" as the FILE statement)

(it occurred to me that the generated name would be thisfile.wav.cue, so the script should also treat this, removing the ".wav" from it)
Title: Help with shntool !
Post by: mjb2006 on 2011-08-18 22:17:32
Right, you didn't say what OS, so I assumed Windows.
rename the "joined.wav" FILE statement within the cuesheet to inherit the intended WAV filename!

Either of these should do:

Someone else will have to script-ify it for you.
Title: Help with shntool !
Post by: krafty on 2011-08-19 17:23:39
I kind of figured how to make bash process the file, however my problem now is with sed, which can't interpret $1 as a variable, I am sure missing something! Any help appreciated!

Code: [Select]
#!/usr/bin/env bash

shntool split -l 5:00 $1
shntool cue split-track*.wav > $1.cue
rm split-track*.wav

sed 's#joined\.wav#$1\.wav#' $1.cue > $1-fixed.cue
mv -f $1-fixed.cue $1.cue
Title: Help with shntool !
Post by: dutch109 on 2011-08-19 21:14:21
Simple quotes prevent variable names from being interpreted, use double quotes instead, ie :

Code: [Select]
sed "s#joined\.wav#$1\.wav#" $1.cue > $1-fixed.cue
Title: Help with shntool !
Post by: krafty on 2011-08-20 19:32:13
Thanks dutch109...

Code: [Select]
#!/usr/bin/env bash

shntool split -l 5:00 $1
shntool cue split-track*.wav > $1.cue
rm split-track*.wav

sed "s#joined.wav#$1#" $1.cue > $1-fixed.cue
mv -f $1-fixed.cue $1.cue


Now sed is working nicely, thanks for the tip. The FILE statement is returning the proper original name.
But the final result keeps generating:

album.wav
album.wav.cue


I believe I would have to insert a variable to handle the .wav extension in this case.
Any ideas?
Title: Help with shntool !
Post by: dutch109 on 2011-08-20 20:26:53
You can remove the extension with something like this :
Code: [Select]
file_without_ext="${1%\.wav}"


Also instead of creating a new file with sed, you can make your modifications in place with the i option :
Code: [Select]
sed -i "s#joined.wav#$1#" $1.cue
Title: Help with shntool !
Post by: krafty on 2011-08-20 20:56:28
Thanks again dutch109...

Now it's working fully...
Code: [Select]
#!/usr/bin/env bash

shntool split -l 5:00 $1
shntool cue split-track*.wav > $1.cue
rm split-track*.wav

sed -i "s#joined.wav#$1#" $1.cue

audiofilename="${1%\.wav}"
mv -f $1.cue $audiofilename.cue
Title: Help with shntool !
Post by: krafty on 2011-08-21 04:49:22
OK, I thought of integrating more goodies into this script, like insert a fixed "TITLE Speech on Whales" before the INDEXES statements, within the cuesheet. Any thoughts on how to do this? Perhaps a "PERFORMER" statement right after the TITLE statement would be good too. These fields will be fixed initially fixed but if I can get to have TITLE Part 1, TITLE Part 2, that would be ideal.

K3B is accepting the cuesheet very charmly.
Title: Help with shntool !
Post by: dutch109 on 2011-08-21 14:37:35
Mhh, you can try something like this (that will preserve indentation in the cue file) :

Code: [Select]
sed -i "s/^\([ \t]*\)\(INDEX 01.*\)$/\1\2\n\1TITLE Title 1\n\1PERFORMER Performer/" cuesheet.cue
sed -i "s/^\([ \t]*\)\(INDEX 02.*\)$/\1\2\n\1TITLE Title 2\n\1PERFORMER Performer/" cuesheet.cue
etc.

When your bash scripts get over 200-300 lines, think about switching to another scripting language like Python, it's a lot more powerful and easier to maintain than bash.
Title: Help with shntool !
Post by: krafty on 2011-08-21 22:00:26
OK, there seems to be a problem when you record MONO speeches and save  them as MONO, because shntool won't process "non CD-Quality" files, and it considers a "CD-Quality" file, only a stereo file. So I have incremented the use of SoX in the script. The SoX job is to create a new stereo file, based on the readings of the mono file. As far as it goes, it processes FLAC perfectly, and curiously, the output file size is just only one or three megabytes bigger than the original mono file (can somebody please explain this particular case to me? - because I re-saved the new stereo file within Audacity as FLAC and I got much more megabytes, using the same compression level) - after SoX doing its thing, shntool now can properly create the cuesheet, and K3B can burn it smoothly the mono file through this cuesheet.

Code: [Select]
#!/usr/bin/env bash

sox -S $1 -c 2 stereo-$1
shntool split -l 1:00 stereo-$1
shntool cue split-track*.wav > $1.cue
rm split-track*.wav
rm stereo-$1

sed -i "s#joined.wav#$1#" $1.cue

audiofilename="${1%\.flac}"
mv -f $1.cue $audiofilename.cue
Title: Help with shntool !
Post by: krafty on 2011-08-22 15:53:28
I'm having a problem with the script... $1 is handling the whole path instead of just the local directory filename (when outside the script directory or from within KDE or GNOME file managers). Any ideas on how to workaround this?
Title: Help with shntool !
Post by: klonuo on 2011-08-22 22:06:01
krafty, you can use Google for such issues, i.e. http://www.google.com/search?q=bash+site:tldp.org (http://www.google.com/search?q=bash+site:tldp.org)
or specifically http://tldp.org/LDP/LGNET/18/bash.html (http://tldp.org/LDP/LGNET/18/bash.html)

${1##*/} will extract filename from path

Will it work if you run it from base folder? - Check it out.
Title: Help with shntool !
Post by: krafty on 2011-08-23 03:11:08
thanks klonuo...

I followed some guidelines and I got most of things working now....

Code: [Select]
#!/usr/bin/env bash

FULLPATH=`dirname $0`
FILE=`basename $1`
FILENOEXT=`basename $1 .flac`

sox -S -V $FULLPATH/$FILE -c 2 $FULLPATH/stereo.wav

shntool split -l 5:00 $FULLPATH/stereo.wav -d $FULLPATH

shntool cue $FULLPATH/split-track*.wav >> $FULLPATH/$FILENOEXT.cue

rm $FULLPATH/split-track*.wav
rm $FULLPATH/stereo.wav

sed -i "s#joined.wav#$FILE#" $FULLPATH/$FILENOEXT.cue
Title: Help with shntool !
Post by: mjb2006 on 2011-08-23 04:45:27
The reason a mono file comes out not very much bigger than a stereo one is because FLAC supports joint stereo (http://wiki.hydrogenaudio.org/index.php?title=Joint_stereo). Stereo audio can be represented as simple left & right channels (LR or "simple" stereo), or it can be stored as a mid (sum) channel and a side (difference) channel instead (this is MS or "mid-side" stereo). Mono or near-mono content in a stereo stream can be stored much more efficiently using mid-side stereo because the side channel is very quiet, if not silent. FLAC, I think depending on what encoding parameters you use, can pick whichever option saves more space as it encodes each block.
Title: Help with shntool !
Post by: krafty on 2011-08-23 16:09:23
mjb2006, thanks for the explanation. That certainly makes sense.

I can think that SoX is creating a new flac with joint-stereo as default, whereas Audacity is saving as simple stereo!
Title: Help with shntool !
Post by: TBeck on 2011-08-23 16:17:27
I can think that SoX is creating a new flac with joint-stereo as default, whereas Audacity is saving as simple stereo!

Most likely it's beeing caused by Audacity applying dither, which adds uncorrelated noise to the channels. I am no expert for Audacity, but there is a switch to disable dithering.
Title: Help with shntool !
Post by: krafty on 2011-08-23 19:30:57
Thanks TBeck, I am going to check that.
Title: Help with shntool !
Post by: krafty on 2011-08-24 00:27:25
Interesting data:

-rw-r--r-- 1 linuxuser linuxuser  98M 2011-08-21 22:16 source-file.flac  (MONO)
-rw-r--r-- 1 linuxuser linuxuser  98M 2011-08-23 20:16 stereo-aud-nodither-at-all.flac
-rw-r--r-- 1 linuxuser linuxuser 124M 2011-08-23 20:10 stereo-aud-dither.flac
-rw-r--r-- 1 linuxuser linuxuser 117M 2011-08-23 20:06 stereo-sox-dither.flac
-rw-r--r-- 1 linuxuser linuxuser  98M 2011-08-23 20:25 stereo-sox-nodither.flac


SoX did dither the source file.
Audacity by default dithered it too, made it bigger.
When having the options to never dither at all, Audacity renders same filesize for a stereo version.

So most likely this resides on type of dithering.
Title: Help with shntool !
Post by: krafty on 2011-08-24 03:23:11
Well...my further tests reveal that, as long as the audio file resides inside the script directory, it will be fine. But if you call the audiofile from any other location, the script fails... any help would be appreciated. I believe that basename and dirname are not doing their job...
Title: Help with shntool !
Post by: krafty on 2011-08-24 23:37:07
In this new version, the program behaves as expected when in command line.
You can browse any directory and start the command (inside the bin path).
I removed some unnecessary things. However still no success in having the script to call over the FLAC file from the right path in Dolphin or Nautilus, associating the command in the context menu.

Code: [Select]
#!/usr/bin/env bash

FILE=`basename $1`
FILENOEXT=`basename $1 .flac`

sox -S -V $FILE -c 2 stereo.wav

shntool split -l 5:00 stereo.wav

shntool cue split-track*.wav >> $FILENOEXT.cue

rm split-track*.wav
rm stereo.wav

sed -i "s#joined.wav#$FILE#" $FILENOEXT.cue
Title: Help with shntool !
Post by: krafty on 2011-08-26 16:56:02
To work as expected in Nautilus, the following code needed to be inserted:
This will prevent Nautilus to do its operations from $HOME as it usually does.
What remains unclear to me is that if Dolphin supports those kind of scripts or some magic...

The script needs to be executable and inside .gnome2/nautilus-scripts

Code: [Select]
#!/usr/bin/env bash

            # Go to file's directory if it's a file, for Nautilus

            if [ ! -d "$destination" ]; then
                destination="`dirname "$destination"`"
            fi
      
FILE=`basename $1`
FILENOEXT=`basename $1 .flac`

sox -S -V $destination/$FILE -c 2 stereo.wav

shntool split -l 5:00 stereo.wav

shntool cue split-track*.wav >> $FILENOEXT.cue

rm split-track*.wav
rm stereo.wav

sed -i "s#joined.wav#$FILE#" $FILENOEXT.cue

Title: Help with shntool !
Post by: krafty on 2011-08-26 22:32:12
dutch109:

how do I properly do a for loop here, getting $i from "TRACK" statement, but putting the TITLE and PERFORMER idented and after INDEXes statements?

Code: [Select]
for i in $FILENOEXT.cue do
sed -i "s/^\([ \t]*\)\(INDEX 01.*\)$/\1\2\n\1TITLE Part $i\n\1PERFORMER Conferencist/" $FILENOEXT.cue
done
Title: Help with shntool !
Post by: krafty on 2011-08-27 00:01:05
Code: [Select]
konsole --title="Create CUESHEET" -e $SHELL -c "cd %d; mscd %f"


I found out that this statement will work in KDE/Dolphin when Opening the file with the associated script.

Now the issue is, file name with spaces... aren't being processed. Any tips would help!
Title: Help with shntool !
Post by: krafty on 2011-08-27 19:39:22
A much better worked out version of the script:

Code: [Select]
#!/usr/bin/env bash

if [[ $1 = */* ]]; then cd "${1%/*}" || exit; fi


file=${1##*/}
base=${file%.*}

sox -S -V "$file" -c 2 stereo.wav

shntool split -l 5:00 stereo.wav

{
echo "REM DATE 2011";
echo "REM GENRE Rock";
echo "REM COMMENT CD-R";
echo "PERFORMER \"Band\"";
echo "TITLE \"Album\"";
shntool cue split-track*.wav | sed "s#joined.wav#$file#";
} > "$base.cue"

rm split-track*.wav
rm stereo.wav



Update: Found out a way to work with Nautilus launching on terminal, the custom command should be:

Code: [Select]
gnome-terminal --title="Creating Cuesheet for CD..." --hide-menubar -x mscd %f