Quantcast
Channel: mattintosh note
Viewing all articles
Browse latest Browse all 878

SOX で Upsampling? stdout with mpv とか

$
0
0

この前、Raspberry Piで cdrdao と toc2cue、bchunk でオーディオの取り込みが出来るようにした。

mattintosh.hatenablog.com

AACへのエンコードは長い間 Nero AAC Codec を使ってたが、結構古いしそろそろ最近のエンコーダの方がいいかなと思い、変えてみることにした。

最近よく「ハイレゾ」って聞くけど色々放浪してたら、SoXでサンプリングレートを弄ったりする遊びがあるらしい。(Windowsでは foober2000 のプラグインに入ってるっぽい?)

http://www.hcn.zaq.ne.jp/___/unix/sox-ja.html#E-rateにある最高品質再サンプルを参考に 44.1kHz s16le を 96kHz f32le に変換。

sox -G infile.wav outfile.wav rate -v-I-b9096000 dither -S

ファイルサイズがトンデモナイことになる。これは流石に困るので、mpv で AACに変換する。Libavcodecだから FFmpegとかと同じかね。FFmpegAACを使うときに -strict experimentalの指定が面倒くさい。

soxから標準出力(stdout)を行う場合はタイプを指定する必要がある。標準出力ファイル名は -。(-t f32でもいいのか? Precision が 32-bit にならない?)

sox -G infile -e floating-point -b32-t raw - rate -v-I-b9096000 dither -S

mpv 側の標準入力で受け取る場合も -でもいいが、明示的にファイルディスクリプタを指定できる fd://012書式があるのでそちらを使う。RAW で送られてくるのでソースのフォーマットなどを指定する必要がある。オプションの書き方が FFmpegともまた違って面白い。--oac=aacを指定するとバージョンによっては Experimental Codec の注意書きが出るようだ(Arch Linux ARM の mpv では出ないが、Ubuntuの 16.04 の mpv では出る)。

mpv --no-config--demuxer=rawaudio--demuxer-rawaudio-channels=2--demuxer-rawaudio-format=floatle--demuxer-rawaudio-rate=96000 fd://0--oac=aac--oacopts=ab=192k

SoXではスペクトログラムも作成出来るのでやってみる。

sox infile.wav -n spectrogram -h
Input File : '干物妹!うまるちゃん - UMARU THE BEST - 01.wav' Channels : 2 Sample Rate : 44100 Precision : 16-bit Duration : 00:03:32.01 = 9349788 samples = 15901 CDDA sectors File Size : 37.4M Bit Rate : 1.41M Sample Encoding: 16-bit Signed Integer PCM

f:id:mattintosh4:20160824001803p:plain

Input File : '干物妹!うまるちゃん - UMARU THE BEST - 01 (96kHz).wav' Channels : 2 Sample Rate : 96000 Precision : 24-bit Duration : 00:03:32.01 = 20353280 samples ~ 15901 CDDA sectors File Size : 163M Bit Rate : 6.14M Sample Encoding: 32-bit Floating Point PCM

f:id:mattintosh4:20160824001817p:plain

ぱっと見、44.1kHz の方は 22.05kHz をぶっちぎってるような…。

では AACに変換したものから PCM に書き戻してスペクトログラムを見てみる。mpv はオプション調べるのがめんどくさかったのでとりあえず ffmpegを使う。

ffmpeg -i infile-96kHz.m4a -f f32le hoge.f32le
sox -b32-c2-r96000-t f32 hoge.f32le -n spectrogram -h

f:id:mattintosh4:20160824010652p:plain

なんか所々ぴょんぴょんしたものもある。これはそこそこまともに戻ってる方なんだろうか。


今回から mpv でエンコードしてみたけど、音質は AACにしては頑張ってる方なんじゃないだろうか。(iPhoneで聴く限りは)

とりあえず一括変換用のスクリプト書いてみた。設定はまた暇なときだなぁ。仕事あがりで聴き比べようとしても疲れてもう無理だ…。

#!/bin/shset-eset-x:${1:?}

which sox mpv

for f
docase${f}in
    *.wav|*.WAV);;
    *)continue;;esac
    
    sox -G"${f}"-e floating-point -b32-t raw - rate -v-I-b9096000 dither -S \
    | mpv --no-config--demuxer=rawaudio \
                      --demuxer-rawaudio-channels=2 \
                      --demuxer-rawaudio-format=floatle \
                      --demuxer-rawaudio-rate=96000 \
                      fd://0 \
                      -o"${f%.*}".m4a --oac=aac--oacopts=ab=192kdone

追記(2016年8月24日)

SoXのオプションを少し読んだのでちょい修正。

書式が少し特殊である。

sox [gopts][[fopts] infile]... [fopts] outfile [effect [effopt]]...

ショートオプションだとわかりづらいのでロングオプションを使用。

今回のサンプルは bchunkcdrとして書き出したので --endianも指定する。自分の場合、cdrdaoのあと、dd conv=swabをかけているので littleだが場合によっては bigかもしれない。

sox -V3--buffer65536\--guard\--multi-threaded\--show-progress\--type cdr \--endian little \
        CROW\'SCLAW\ -\ Shelling\ And\ Torpedoing\ -\ 02.cdr \--bits24\--type raw - \
        rate -v-I96000\
        dither -S\
| mpv --no-config\--demuxer{=rawaudio,-rawaudio-{channels=2,format=s24le,rate=96000}}\
      fd://0\-o outfile.m4a \--oac=aac\--oacopts=ab=192k
sox: SoX v14.4.2 sox INFO formats: `CROW'SCLAW - Shelling And Torpedoing - 02.cdr': overriding file-type byte-order Input File : 'CROW'SCLAW - Shelling And Torpedoing - 02.cdr' Channels : 2 Sample Rate : 44100 Precision : 16-bit Duration : 00:03:10.65 = 8407812 samples = 14299 CDDA sectors File Size : 33.6M Bit Rate : 1.41M Sample Encoding: 16-bit Signed Integer PCM Endian Type : little Reverse Nibbles: no Reverse Bits : no Output File : '-' (raw) Channels : 2 Sample Rate : 96000 Precision : 24-bit Duration : 00:03:10.65 = 18302720 samples ~ 14299 CDDA sectors Sample Encoding: 24-bit Signed Integer PCM Endian Type : little Reverse Nibbles: no Reverse Bits : no Comment : 'Processed by SoX'sox INFO sox: effects chain: input 44100Hz 2 channels sox INFO sox: effects chain: gain 44100Hz 2 channels sox INFO sox: effects chain: rate 96000Hz 2 channels sox INFO sox: effects chain: gain 96000Hz 2 channels sox INFO sox: effects chain: dither 96000Hz 2 channels sox INFO sox: effects chain: output 96000Hz 2 channels

Viewing all articles
Browse latest Browse all 878

Trending Articles