2015年8月5日 星期三

KTV 自動聲道判斷

一般的 KTV 檔案來源, 大致上分為 VCD 與 DVD 兩類,
除了畫面大小的分別之外, 最主要的差別點是, VCD 歌源
只有一個 stereo audio channel, 標準的作法是 left channel
只包含伴奏, right channel 包含人聲與伴奏.
DVD 歌源包含兩個 stereo audio channels, 第一個 audio channel
是人聲與伴奏, 第二個 audio channel 只有伴奏.

一個標準的 VCD stereo audio channel 的樣子,大概長得像這樣 :

上半邊是左聲道, 下半邊是右聲道的波形, 右聲道因為有伴奏
又有人聲, 所以整體波形的"能量"比較多.
所謂的"標準", 指的是兩個聲道的音量標準是相同的,
也就是在沒有人聲的部分, 兩個聲道的聲音與音量皆相同.
這樣歌曲在切換 伴奏/人聲 的時候,就不會有音量差距.

對於這種標準的波形, 如果分別算出左聲道與右聲道的 RMS
或者 replaygain, 右聲道的 power 一定會比左聲道強, 所以
判斷兩個聲道的能量值, 就可以判斷出人聲是否在正確的聲道.

但是, 如果歌曲的製作不夠優良的話, 波形可能長得像這樣 :
很明顯的,右聲道的音量比左聲道強, 雖然在判斷上結果也是正確,
但是這種歌曲將會造成作 normalization, 還有切換 伴奏/人聲 時,
音量無法很正確的調整.

甚至, 底下的情況會造成誤判 :
這首歌曲,也是標準的左伴奏右人聲的波形, 但是左聲道的整體
音量值調得比右聲道高很多. 這個部分只要看歌曲開始時, 前奏
只有伴奏,還沒人聲時就可以知道, 如果兩個聲道有平衡, 前奏
階段的波形大小應該相同的, 圖中明顯左聲道大很多.
圖中的波形, replaygain 算出來, 左聲道 +0.45, 右聲道 +2.39,
右聲道需要提升的音量比左聲道多(右聲道比較弱). 在軟體判斷上
會造成誤判.
另外一個有趣的情況是, 左右聲道單獨算出來的 replaygain,
與左右聲道一起計算的 replaygain, 常常結果不太能預期到.
例如上圖的 replaygain, 左聲道 +0.45, 右聲道 +2.39,
左右聲道一起算是 -1.44, 目前尚不知為何有這種結果,
至於伴奏的 channel 要做 normalization, 究竟是要拿左聲道
單獨的 replaygain 來調整, 還是拿左右聲道一起算的 replaygain ?
目前還沒定論.

---------------------------------------
更新 :
以目前的實際測試, 利用 replaygain 來判斷人聲的聲道,
會有滿低比例的誤判, 誤判的情況可能為 :
-  兩個聲道的 replaygain 差別很小, 剛好伴奏的聲道算出的 gain 比較小一點
-  DIY 的 KTV 檔案, 兩個聲道的來源不同

最準確的判斷方式, 要利用 spleeter 做伴奏與人聲的分離後,
再針對人聲的 wave 檔做 replaygain 的計算,
純伴奏的音軌, 產生的人聲 wave 檔案, 幾乎沒什麼音量,
算出來的 replaygain 值會超過 +10dB, 基本上應該可以
100% 判斷出正確的人聲聲道.

2015年8月4日 星期二

KTV 歌曲音量均衡方法

一般的原聲原影 KTV 檔案,播放時有個最大的問題,
就是每一首歌的音平水準不同,導致歌語歌之間的
音量差距很大,要一直調整擴大機的音量.

MP3 也會有類似的問題, ReplayGain 就是用來解決
這種問題. 所使用的原理, 就是算出整首歌音量的"能量",
然後算出每首歌,要達到標準音量能量的話,需要調大聲
或調小聲多少分貝(dB).

所以如果要調整 KTV 音量, 讓每首歌播出時的音量
感覺都差不多, 可以利用 ReplayGain 的方法, 算出每一首
歌曲的音量能量, 再得到需要調整的增益(分貝)值, 就可以拿來
設定給點歌軟體, 讓每首歌的預設音量都根據 ReplayGain
算出來的分貝值調整, 就可以讓每首歌音量差不多.

ffmpeg 是一個功能強大的影音處理程式, 裏頭也有包括
計算影音檔中, 各個音軌的 replaygain 的值. 事實上 ffmpeg
可以計算音軌的 RMS 及 replaygain, 兩者都是計算音量的能量,
但是 replaygain 還有根據頻率不同給予不同的能量加權,
也就是高音與低音給人的音量能量感受,有不同的感受.

利用 ffmpeg 計算出來的 replaygain 分貝值,就是該首歌曲
需要調整的音量相對值. 公式則為 :

   音量調整值 = 10^(分貝值/20)

例如算出來的分貝值是 +1.38dB 的話,則表示音量調整值是
10^(1.38/20) = 10^(0.069)=1.17

如果算出來的分貝值是 -2.15dB 的話, 則音量調整值是
10^(-2.15/20) = 10^(-0.1075) = 0.78

因為音量有可能需要調大聲,也可能調小,所以假設
點歌軟體每首歌的預設音量是設定在 40(0最小, 100 最大值),
則往上調大聲最多可以調整 2.5 倍, 調小聲基本上比較沒限制,

所以上面例子中, +1.38dB 的歌曲, 設定給點歌軟體的預設音量值
就是 40*1.17 = 46.8 = 47
另一個 -2.15dB 的歌曲, 設定給點歌軟體的預設音量值為
 40*0.78 = 31.2

所以每首歌算出 replaygain 的增益值之後, 就可以設定
點歌軟體資料庫中, 每首歌曲的預設音量值, 在每首歌曲播出時,
由於預設音量的調整, 讓每首歌聽起來的音量大小都差不多.

至於預設音量的標準值, 我目前建議是設定在 40, 這樣可以讓
歌曲往上調整音量的空間, 最大有 2.5 倍(大約增益值是 +8dB),
往下調整的解析度也還足夠. 如果實際處理的歌曲, 很多都太小聲,
增益值都超過 +8dB 的話, 在考慮把標準音量從 40 往下調整.
基本上點歌軟體的標準音量往下調整的話, 實際從 PC 輸出的音量
也會比較小, 需要靠擴大機端調大輸出音量來補償.

目前我會利用 ffmpeg, 把每首歌曲的 replaygain 增益值算出來,
然後把預設音量值直接算好後, 把每首歌曲檔名修改為類似 :
  Selina_vL_gn46.mpg
  ALin_vR_gn37.mpg

然後再利用程式,掃描點歌軟體的資料庫檔案, 把預設音量的欄位
靠 "_gn" 後面的數值設定進去, 就可以讓每首歌曲的預設音量
根據 replaygain 調整.

至於左右聲道的判斷, 還有增益值的可信度, 會再另外一篇文章再
討論.

2015年7月27日 星期一

Use Access expression builder to calculate field data from another field


1. after the database file is opened, select 查詢設計

2. select the database table which will be used  in the query

3.  select the any data field to display for index in the final query table

4. keep the focus(cursor) on the new display field to input expression

5. press on the expression builder to help to construct the expression

6. input the expression to generate the new field data



7. select 執行(execution) to generate the query result


8. in the query result, you may mark the whole column, then copy/paste to overwrite the original database table

instead of using expression builder in step 6, it is more convenient to
copy/paste the existing expression directly to the field definition in step 7.

Expr1: IIf(InStr(1,[ktv_Song]![Song_FileName],"_gn")=0,40,Val(Mid([ktv_Song]![Song_FileName],InStr(1,[ktv_Song]![Song_FileName],"_gn")+3,2)))


2015年6月5日 星期五

audio filter path of ffmpeg

To use ffmpeg to calculate the audio power,
including "volumedetect" and "replaygain" filter,
there is some trick to apply the filter path.

The target is to calculate the audio power for
the left/right audio channels in the mpeg I stream.
The audio filter path will be composed of 3 parts :
1. select channel by "pan" filter, [0:a] for the input audio stream,
    c0=c0 to select left channel, and c0=c1 for right channel
2. specify the audio channel number by "aformat"
    which is necessary for volumedetect filter afterwards
3. "volumedetect" or "replaygain" filter

For example :

ffmpeg -i vcd.mpg -filter_complex "[0:a]pan=1c|c0=c0[middle];[middle]aformat=channel_layouts=mono[final];[final]volumedetect" -f null NUL

 replacing "c0=c0" by "c0=c1" for right channel(mono),
or "volumedetect" by "replaygain" for RMS or replaygain statistics.

2014年1月3日 星期五

Realtek 8168b under Ubuntu

之前遇到 Ubuntu 機器上, Realtek 8168b 網卡不能上 Gigabit 速度,因為 r8169 driver 不適用,所以用底下的方法,改編 r8168 driver 就好了. 只是這個 driver 本身沒辦法驅動 r8169.

sudo apt-get install --reinstall linux-headers-$(uname -r) linux-headers-generic build-essential dkms
wget http://media.cdn.ubuntu-de.org/forum/attachments/14/31/3005217-r8168-dkms-8.036.00.tar.gz
sudo tar xvf 3005217-r8168-dkms-8.036.00.tar.gz -C /usr/src
sudo dkms add -m r8168-dkms -v 8.036.00
sudo dkms build -m r8168-dkms -v 8.036.00
sudo dkms install -m r8168-dkms -v 8.036.00 
echo "blacklist r8169" | sudo tee -a /etc/modprobe.d/blacklist.conf
做完之後,還要再 update 一下
~$ update-initramfs -u

2013年11月12日 星期二

mount a HDD image under Linux

(1). determine the offset of the partition

# fdisk -l 3.4.61.img

Disk linaro-quantal-developer-20130422-342-A20-3.4.61.img: 944 MB, 944766976 bytes
58 heads, 20 sectors/track, 1590 cylinders, total 1845248 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Device      Boot      Start         End      Blocks   Id  System
3.4.61.img  1          2048     1845247      921600   83  Linux

From the example, the offset of the only partition is
2048*512=1048576

(2). setup the loopback device

# losetup -o 1048576 /dev/loop0 3.4.61.img

(3). mount the partition

# mount /dev/loop0 /mnt/temp

(4). umount the partition and detach the loopback device

# umount /mnt/temp
# losetup -d /dev/loop0

2013年11月6日 星期三

Android build system trace

整個 Android build 的主體,都放在
build 底下, 開始的第一個指令

source build/envsetup.sh

會"建立"幾個 shell 的 function,
在 shell 底下下 set 指令就會印出來,
之後就可以下 lunch, cgrep, godir 等"指令".