Last active
January 29, 2020 16:59
-
-
Save kamiyaowl/00d5f1f189e3109ef57b57f5c9c9222b to your computer and use it in GitHub Desktop.
PYNQ-Z2のPython上でコンプレッサーを実装してみた https://twitter.com/kamiya_owl/status/1221425698029498368
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pynq.overlays.base import BaseOverlay | |
base = BaseOverlay("base.bit", download=False) | |
# あとでグラフに書きたい場合はtrue | |
is_debug_capture = True | |
l_src_arr = [] | |
r_src_arr = [] | |
l_dst_arr = [] | |
r_dst_arr = [] | |
# from audio_adau1761.h | |
I2S_DATA_RX_L_REG = 0x00 | |
I2S_DATA_RX_R_REG = 0x04 | |
I2S_DATA_TX_L_REG = 0x08 | |
I2S_DATA_TX_R_REG = 0x0C | |
I2S_STATUS_REG = 0x10 | |
pAudio = base.audio | |
pAudio.select_line_in() # line入力を使う | |
# 24bit 2の補数表現がおかしく変換されるのでもとに戻す | |
def int24_to_int(src): | |
if ((src >> 23) & 0x1) == 0x1: | |
# 負数 24bitまでを反転させて1を足す | |
return -(((~src) & 0xffffff) + 1) | |
else: | |
# 正の数 | |
return src | |
# int24に戻す | |
def float_to_int24(src): | |
return int(0x7fffff*src); | |
# コンプレッサーみたいなエフェクトを掛ける | |
# src: 元データ 浮動小数点 | |
# threash: 補正対象のレベル 浮動小数点 | |
# ratio: threshを超え場合の圧縮量 | |
# return: 処理後の音データ 浮動小数点 | |
def compress(src, threash, ratio): | |
if abs(src) < threash: | |
return src | |
# threash位置からの加算分を求める | |
diff = (abs(src) - threash) / ratio | |
# 符号を付けて返せば終わり | |
if src < 0: | |
return -(threash + diff) | |
else: | |
return (threash + diff) | |
while True: | |
# 音声の取得 | |
l_src = int24_to_int(pAudio.mmio.read_reg(offset=I2S_DATA_RX_L_REG))/0x7fffff | |
r_src = int24_to_int(pAudio.mmio.read_reg(offset=I2S_DATA_RX_R_REG))/0x7fffff | |
# l_src, r_srcが-1.0~+1.0の値を取るので好きに音声処理する | |
# floatからint24に戻す | |
l_dst = compress(l_src, 0.1, 2) | |
r_dst = compress(r_src, 0.1, 2) | |
# 音声の書き込み | |
pAudio.mmio.write_reg(data=float_to_int24(l_dst), offset=I2S_DATA_TX_L_REG) | |
pAudio.mmio.write_reg(data=float_to_int24(r_dst), offset=I2S_DATA_TX_R_REG) | |
# デバッグ用 | |
if is_debug_capture: | |
l_src_arr.append(l_src) | |
r_src_arr.append(r_src) | |
l_dst_arr.append(l_dst) | |
r_dst_arr.append(r_dst) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# さっき取得したデータのグラフを書く | |
%matplotlib inline | |
import matplotlib.pyplot as plt | |
plt.rcParams['figure.figsize'] = (15.0, 15.0) | |
from ipywidgets import * | |
plt.subplot(2,1,1) | |
plt.title('ADAU1761 Lch In/Out') | |
plt.plot(l_src_arr, label='Lch In') | |
plt.plot(l_dst_arr, label='Lch Out') | |
plt.legend() | |
plt.subplot(2,1,2) | |
plt.title('ADAU1761 Rch In/Out') | |
plt.plot(r_src_arr, label='Rch In') | |
plt.plot(r_dst_arr, label='Rch Out') | |
plt.legend() | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment