import pyaudio
import numpy as np
from maix import camera, image, display
import time

CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
p = pyaudio.PyAudio()

img = image.new(size=(320, 240), color=(0, 0, 0), mode="RGB")  # 创建一张背景图
stream = p.open(format=FORMAT,
                channels=CHANNELS,
                rate=RATE,
                input=True,
                frames_per_buffer=CHUNK)

intensity_recorder = []
intensity_show = [0]*40
print(len(intensity_show))
# 循环读取音频数据

f_t = np.fft.fftfreq(CHUNK, 1 / RATE)    # 传入信号长度及采样率，返回对应频率

while True:
    stream.start_stream()
    str_data = stream.read(CHUNK)  # 从麦克风获取一帧音频数据
    stream.stop_stream()
    print("type str_data:",type(str_data))
    print("str_data:",str_data)
    # 将音频数据转换为NumPy数组
    arr = np.frombuffer(str_data, dtype=np.int16)    #在这里用numpy.frombuffer与
                                                     #numpy.fromstring效果相同
    print("arr:",arr)
    print("type arr1:",type(arr))
    arr.shape = -1,2
    # 将数组转置
    arr = arr.T
    print("arr2:",arr)
    print("3",len(arr[0].tolist()))
    f_signal = np.fft.fft(arr[0])
    #f_signal=np.abs(f_signal)
    #print("ft:",f_t)
    
    #print("ft len:",len(f_t.tolist()))
    #print("f_signal:",f_signal)
    #print("f_signal len:",len(f_signal.tolist()))
    # 取前512个数据，即正频率部分
    fft_data = f_signal[:512]

    # 将前512个数据分成32组，每组包含16个数据
    fft_data = np.split(fft_data, 32)

    # 对每组数据进行平均，得到每组的平均幅值
    avg_data = np.abs(fft_data).mean(axis=1)
    print(avg_data)
    img.clear()
    for i in range(0,32):
        height=int(avg_data.tolist()[i]/500)
        intensity_show[i] = image.new(size=(7, height), color=(40, 144, 152), mode="RGB")
        img.draw_image(intensity_show[i], i*8, 240-height)
    #img.draw_string(30, 20, "intensity"+str(intensity), scale=1.0,color=(255, 255, 255), thickness=1)
    display.show(img)
    print("main_thread_time:", time.time())

# 关闭流和PyAudio对象
stream.stop_stream()
stream.close()
p.terminate()