- UID
- 2
- 积分
- 8682
- 帖子
- 2905
- 主题
- 199
- 论坛币
- 11739
- 威望
- 16
- EP值
- 2349
- MP值
- 15
- 阅读权限
- 200
- 注册时间
- 2011-8-3
- 在线时间
- 2597 小时
- 最后登录
- 2024-8-28
|
说明
需要先添加tcAudio模块, 下载地址 http://www.tcax.org/forum.php?mod=viewthread&tid=198
附件为完整工程, 其中test.mp3可以被替换, 推荐的音频格式为wav, mp3, ogg.
本例子仅作为一个展示用例.- from tcaxPy import *
- from util.tcAudio import *
- SPECWIDTH = 368
- SPECHEIGHT = 127
- BANDS = 28
- def Spectrum(fft): # convert fft data to graph
- bars = []
- b0 = 0
- for x in range(BANDS):
- peak = 0 # peak of a certain bar
- b1 = pow(2, x * 10.0 / (BANDS - 1))
- if b1 > 1023:
- b1 = 1023
- if b1 <= b0:
- b1 = b0 + 1 # make sure it uses at least 1 FFT bin
- while b0 < b1:
- if peak < fft[1 + b0]:
- peak = fft[1 + b0]
- b0 += 1
- y = int(sqrt(peak) * 3 * SPECHEIGHT - 4) # scale it (sqrt to make low values more visible)
- if y > SPECHEIGHT:
- y = SPECHEIGHT # cap it
- bars.append(y)
- return bars
- def tcaxPy_User():
- file_name = GetVal(val_OutFile) + '.ass'
- ass_header = GetVal(val_AssHeader)
- ASS_FILE = CreateAssFile(file_name, ass_header)
- tcAudioInit()
- channel = tcAudioOpen('test.mp3')
- freq = tcAudioGetFreq(channel) # usually 44100 Hz
- duration = tcAudioGetDuration(channel)
- num = int(duration * freq / 2048)
- for i in range(num):
- ASS_BUF = []
- fft = tcAudioGetFFT(channel, 2048)
- bars = Spectrum(fft)
- bar_width = int(SPECWIDTH / BANDS)
- count = len(bars)
- for j in range(count):
- start = i * 100 * 2048 / freq
- end = (i + 1) * 100 * 2048 / freq
- x = (1280 - SPECWIDTH) / 2 + bar_width * j
- y = 700
- FSC = fsc((bar_width - 2) * 100, bars[j] * 100)
- CLR = color1(FmtRGB(j * 10 % 255, randint(0, 20), randint(20, 60))) + alpha1(50) # a little change
- ass_main(ASS_BUF, SubL(start, end, 0, Pix_Style), an(1) + pos(x, y) + FSC + CLR, PixPt())
- WriteAssFile(ASS_FILE, ASS_BUF) # write the buffer in memory to the file
- progress(i + 1, num)
- tcAudioFin()
- FinAssFile(ASS_FILE)
复制代码 |
|