fft试图变声

import wave
import numpy as np
import simpleaudio as sa
import matplotlib.pyplot as plt
import copy


x = np.arange(0, 10, 0.1)

y = x * 2




project_path = "/home/amazing/python/sound"

music_file_path = project_path+"/data/2.wav"


def change_sp(sp):

    l1, r1 = 0, 4000
    l2, r2 = 4000, 8000

    nan = copy.deepcopy(sp[l1:r1])
    nv = copy.deepcopy(sp[l2:r2])

    sp[l1:r1] = nv
    sp[l2:r2] = nan

    sp = np.flip(sp)

    nan = copy.deepcopy(sp[l1:r1])
    nv = copy.deepcopy(sp[l2:r2])

    sp[l1:r1] = nv
    sp[l2:r2] = nan

    sp = np.flip(sp)

    return sp

def show_sp(sp, freq, title):
    plt.title(title)
    plt.plot(freq, sp.real)
    plt.plot(freq, sp.imag)
    plt.show()


def show_sp_low(sp, title):
    max_s = 2400
    sp = sp[:max_s]
    sq = [i for i in range(len(sp))]
    plt.title(title)
    plt.plot(sq, sp.real)
    plt.plot(sq, sp.imag)
    plt.show()

def show_sp_raw(sp, title):
    plt.title(title)
    plt.plot(sp.real)
    plt.plot(sp.imag)
    plt.show()

if __name__ == '__main__':

    #打开wav文件 ,open返回一个的是一个Wave_read类的实例,通过调用它的方法读取WAV文件的格式和数据。
    f = wave.open(music_file_path, "rb")
    #读取格式信息
    #一次性返回所有的WAV文件的格式信息,它返回的是一个组元(tuple):声道数, 量化位数(byte单位), 采样频率, 采样点数, 压缩类型, 压缩类型的描述。wave模块只支持非压缩的数据,因此可以忽略最后两个信息
    params = f.getparams()
    # print(params)
    # _wave_params(nchannels=2, sampwidth=2, framerate=8000, nframes=32322, comptype='NONE', compname='not compressed')
    nchannels, sampwidth, framerate, nframes = params[:4]
    #读取波形数据
    #读取声音数据,传递一个参数指定需要读取的长度(以取样点为单位)
    sound_buffer = f.readframes(nframes)
    f.close()

    # play_buffer (audio_data, num_channels, bytes_pre_sample, sample_rate)
    # play_obj = sa.play_buffer(sound_buffer, nchannels, sampwidth, framerate)
    #
    # play_obj.wait_done()

    # #将波形数据转换成数组
    # #需要根据声道数和量化单位,将读取的二进制数据转换为一个可以计算的数组
    wave_data = np.frombuffer(sound_buffer, dtype=np.int16)
    # print(np.shape(wave_data))
    # (64644,)
    # #将wave_data数组改为2列, 声道数是2,所以改为2
    wave_data = wave_data.reshape((-1, 2))
    # print(np.shape(wave_data))
    #转置数据
    wave_data = wave_data.T
    # print(np.shape(wave_data))
    # (2, 32322)

    left_channel_sound_data = wave_data[0]
    right_channel_sound_data = wave_data[1]

    n = nframes
    d = 1 / framerate
    x = [d * i for i in range(nframes)]

    plt.title("left")
    plt.plot(x, left_channel_sound_data)
    plt.show()

    play_obj = sa.play_buffer(left_channel_sound_data.tobytes(), 1, sampwidth, framerate)
    # play_obj = sa.play_buffer(right_channel_sound_data.tobytes(), 1, sampwidth, framerate)

    play_obj.wait_done()

    sp = np.fft.fft(left_channel_sound_data)
    freq = np.fft.fftfreq(nframes, 1/framerate)

    show_sp_raw(sp, "sp raw")
    # show_sp(sp, freq, "sp")
    show_sp_low(sp, "sp low")

    sp_change = change_sp(sp)

    # show_sp_raw(sp_change, "sp_change raw")
    # show_sp(sp_change, freq, "sp_change")
    # show_sp_low(sp_change, "sp_change low")

    left_ifft = np.fft.ifft(sp_change)

    plt.title("ifft")
    plt.plot(x, left_ifft)
    plt.show()

    left_ifft_real_int16 = np.array(left_ifft.real, dtype=np.uint16)

    play_obj = sa.play_buffer(left_ifft_real_int16.tobytes(), 1, sampwidth, framerate)
    # play_obj = sa.play_buffer(right_channel_sound_data.tobytes(), 1, sampwidth, framerate)

    play_obj.wait_done()


文章目录