Text Based User Interface to stdout - linux

I'm working on a headless device to play music, and I'm using Sox's play command to play the file.
I'm after a way to pipe the output formatted like this:
$play File.wav
File.wav:
File Size: 1.25M
Bit Rate: 64.0k
Encoding: Unsigned PCM Channels:
1 # 8-bit
Samplerate: 8000Hz
Replaygain: off
Duration: 00:02:36.87
In:42.4% 00:01:06.56
[00:01:30.31] Out:532k [ -===|===- ] Clip:0
To be readable in a text file so it can be parsed by PHP and ouputted to the user.
I've tried the standard things like:
$play File.wav >> output.txt
which results in a file being created but an empty one, I don't need it to be realtime, just every few seconds, or even once per run just to check it's going ok.
Any ideas?

Try the following (it would appear that sox outputs on STDERR):
play File.wav >> output.txt 2>&1
Whether it's going to be easy to parse is a different story ... :)
Namely the
[00:01:30.31] Out:532k [ -===|===- ] Clip:0
bit gets broken up over individual lines with your redirect.

Related

Chopping audio files with ffmpeg get None duration python 3

I have a long list of audio files, and some of them are longer than an hour. I am using Python 3.6, Jupyter notebook by connecting to a remote machine and using TinyTag library to get a duration of audio. Ffmpeg version is 2.8.14-0ubuntu0.16.04.1.
My code below goes over the files and if a file is longer than an hour, it splits the file into one-hour long pieces, and a leftover piece less than an hour, and copies the pieces as fname_0, fname_1,fname_2, etc. Before chopped, each file is .m4a but during chopping, they are converted to a .wav file. However, after this chopping process, when reading the duration of pieces, I realized that all the pieces have 'None' duration. Something must be wrong in the command line but I can`t see what that is. Thanks in advance.
# fpaths is the list of filepaths
for i in range(0,len(fpaths)):
fpath=fpaths[i]
fname=os.path.basename(fpath)
fname0=os.path.splitext(fname)[0] #name without extension
tag = TinyTag.get(fname)
if tag.duration > 3600:
cmd2 = "ffmpeg -i %s -f segment -segment_time 3600 -c copy %s" %(fpath, fname0) + "_%d.wav"
os.system(cmd2)
os.remove(fpath)
When I change to the extension from .wav to .m4a in the cmd2 command line, it works. Writing here just in case if someone has the same problem.

SoX Not Reading Wav

I've got a large (5Gb) WAV 64 file that I'm trying to analyse with SoX. On doing;
sox /file/ -n stats
it tells me;
sox FAIL formats: can't open input file '/file/': WAVE: RIFF header not found
On attempting to specify;
sox /file/ -t w64 -n stats
it says;
sox WARN sox: ignoring `-t w64'.
and proceeds to throw the same error as before. This file is W64. How do I convince SoX to accept it?
Looks like ffmpeg can handle wav64 files
Have you tried to create your own tiny wav64 file and see if you can read that ?
Looks like wav64 is targeted for multichannel audio is that your use case ? WAV format itself is very simple and lends itself to allowing people to write two pages of code to read any wav file ... if you exhaust all other options and your life depended on getting your file read, reading the ISO spec or similar on wav64 format then writing your own reader is an option.
Can you share how your wav64 file was created ? Maybe one of us can replicate its creation and battle how to read it back
If its not full of massive multi-channel audio is getting it converted to normal wav an option ?
Maybe, your wave64 file is invalid. A valid one should contain a riff header.
You've added -t w64 flag after the filename. That's why it's ignored.
# This would work! [CHECKED]
# If your file is valid!
sox -t w64 in_file -n stats

Frequency distribution from wav files

I found out that I can use SoX's
play file.wav stat -freq
to generate a table of levels against frequencies for a file.
However, it seems to run in real time, i.e. takes as long to complete as the audio does to play.
How can I generate the same table of frequencies and levels but in the shortest time possible?
The output of SoX is rather slow, but this is mainly caused by the displaying. One solution could be to redirect the output of SoX (which is on the standard error (stderr) stream in the case of stat) to a file.
You could use:
sox file.wav -n stat -freq > out 2>&1
that will redirect stderr to stdout (&1), and then redirect stdout to the file 'out'. See this link for other solutions to redirect stderr.
This should be rather faster (in my case few seconds for a 10 minute file).

Resample audio file from terminal

I currently have a list of file for which I need to change the sample rate for.
I'recently been aware that this is possible using sox But when I try do it, I keep on getting a error message that sox wav: Premature EOF on .wav input file And causes the audio file to be empty.. it seems like that sox is not able to resample an audio file which input = output... which I kinda need, if I have to convert a whole directory of audio files...
Currently used commands:
~/kaldi-trunk/egs/yesno/s5_k_added$ sox 0_0_0_0_1_1_1_1.wav -r 8000 0_0_0_0_1_1_1_1.wav
sox WARN wav: Premature EOF on .wav input file
:~/kaldi-trunk/egs/yesno/s5_k_added$ play 0_0_0_0_1_1_1_1.wav
0_0_0_0_1_1_1_1.wav:
File Size: 44
Encoding: Signed PCM
Channels: 1 # 16-bit
Samplerate: 8000Hz
Replaygain: off
Duration: unknown
In:0.00% 00:00:00.00 [00:00:00.00] Out:0 [ | ] Clip:0
Done.
How do I resample a directory of audio files?
Try changing the output file name, possible putting it in a different directory if you want to keep the same file name.
For example:
sox 0_0_0_0_1_1_1_1.wav -r 8000 ./out/0_0_0_0_1_1_1_1.wav
I believe sox is attempting to read the file while it is actively changing it, sometimes sox does not create a temporary file to output into.
EDIT: If you have a directory of files you all want to change, use this:
$ mkdir out
$ for file in *; do sox ${file} -r 8000 ./out/${file}; done

midi to ogg - pipeline distortion

I am trying to convert midi files to ogg or mp3. Eventually this will happen on a linux webserver but currently I am using a Windows 7 machine. I am using timidity to convert the midi to wav and then either sox or ffmpeg to convert the wav to ogg/mp3.
When I use an intermediate file the process works fine (in the first line below timidity creates file.wav)
timidity.exe file.mid -Ow
sox.exe file.wav file.ogg
However, when I try to pipe the timidity output into sox (as below), the resulting file ogg is horribly distorted
timidity.exe file.mid -Ow -o - | sox.exe -t wav - file.ogg
and I get a warning
sox.exe WARN wav: Premature EOF on .wav input file
I also get the same distortion problem when I replace sox with ffmpeg (and the appropriate command line options), or when I replace ogg with mp3 as the output format.
So what am I doing wrong?
Thanks,
Chris
Regarding the warning itself, you're doing nothing wrong. You may also see a warning from timidity that reads something like
Warning: -: Illegal seek: Can't make valid header
What's happening there is explained in the timidity manual page:
If output is directed to a non-seekable file, or if TiMidity++ is interrupted before closing the file, the file header will contain 0xffffffff in the RIFF and data block length fields.
Note that RIFF is the encoding format commonly called by its file extension, .wav. When timidity writes a RIFF file, it doesn't know how long the file will be, so it writes some placeholder junk in the header and moves on to writing the data. When it finishes with the data, it knows how long the file is, so it goes back to the beginning of the file and writes over that junk in the header. When you write to a pipe, it has no way to go back and rewrite anything: the downstream program has to handle the placeholder junk. Also from the timidity manual page:
The popular sound conversion utility sox is able to read such malformed files, so you can pipe data directly to sox for on-the-fly conversion to other formats.
Thus, the message you mentioned. Sox is informing you that the chef prepared the file wrong BUT SOX IS HAPPY TO EAT IT ANYWAY BECAUSE SOX IS NOT PICKY. Sox is apparently passive-aggressive. Who knew?
You can ignore those warning messages, because now they are telling you something you already know. Or, you can use a raw format and explicitly tell timidity and sox how to play well with one another:
timidity file.midi -Or1Ssl -s44.1 -o- | sox -t raw -b 16 -e signed -r 44.1k -c 2 - file.ogg
As for the distortion, that may be caused in part by quirks in the audio libraries on the Windows system. I note that the pipeline in the question, sans .exe extensions, produces output with no notable distortion on a linux system. Using a well-defined raw format in the pipeline may also help with that issue.
Note that for Ogg output, you can now get that directly from timidity:
timidity file.midi -o file.ogg -Ov

Resources