python中等时音调的拍频变化

python中等时音调的拍频变化,python,python-3.x,Python,Python 3.x,我刚刚开始学习并开始编写一个基本的python脚本来生成相同的代码。以下是我为执行以下操作而编写的脚本: 产生拍频的梯形波 用拍频梯形波调制基频正弦波 #!/usr/bin/env python # encoding: utf-8 """ Small program for creating Isochronic Tones of desired base frequency, beat frequency and ramp frequencies """ import math impo

我刚刚开始学习并开始编写一个基本的python脚本来生成相同的代码。以下是我为执行以下操作而编写的脚本:

  • 产生拍频的梯形波
  • 用拍频梯形波调制基频正弦波

    #!/usr/bin/env python
    # encoding: utf-8
    
    """
    Small program for creating Isochronic Tones of desired base frequency, beat frequency and ramp frequencies
    """
    
    import math
    import wave
    import struct
    import array
    import sys
    
    def make_isochronic_wave(beat_freq, beat_ramp_percent, beat_sampl_rate, beat_time, base_freq, amplitude):
    
    #
    # The time for which the trapezoidal beat frequency wave is present in the entire cycle
    #
    up_time = 1 / beat_freq
    
    #
    # Gap between consequtive trapezoidal beats
    #
    gap_percent = up_time * 0.15
    
    #
    # To accomodate gaps
    #
    up_time = up_time * 0.85
    
    #
    # Total number of samples
    #
    data_size = beat_sampl_rate * beat_time
    
    #
    # No. of gaps per sec = No. of beats per sec
    #
    no_of_gaps = beat_freq
    
    #
    # Samples per gap = percentage of total time allocated for gaps * No. of samples per sec
    #
    sampls_per_gap = gap_percent * beat_sampl_rate
    
    #
    # Total number of samples in all the gaps
    #
    gap_sampls = no_of_gaps * sampls_per_gap
    
    #
    # Change the beat sample rate to accomodate gaps
    #
    beat_sampl_rate = beat_sampl_rate - gap_sampls
    
    #
    # nsps = Number of Samples Per Second
    #
    # NOTE: Please see the image at: 
    #
    beat_nsps_defined = beat_sampl_rate * up_time
    
    beat_nsps_inc = beat_nsps_defined * beat_ramp_percent
    
    beat_nsps_dec = beat_nsps_defined * beat_ramp_percent
    
    beat_nsps_stable = beat_nsps_defined - (beat_nsps_inc + beat_nsps_dec)
    
    beat_nsps_undefined = beat_sampl_rate - beat_nsps_defined
    
    #
    # Trapezoidal values
    #
    values = []
    
    #
    # Trapezoidal * sine == Isochronic values
    #
    isoch_values = []
    
    #
    # Samples constructed
    #
    sampls_const = 0
    
    #
    # Iterate till all the samples in data_size are constructed
    #
    while sampls_const < data_size:
        prev_sampl_value_inc = 0.0
        prev_sampl_value_dec = 1.0
        #
        # Construct one trapezoidal beat wave (remember this is not the entire sample)
        #
        for sampl_itr in range(0, int(beat_sampl_rate)):
            if sampl_itr < beat_nsps_inc:
                value = prev_sampl_value_inc + (1 / beat_nsps_inc)
                prev_sampl_value_inc = value
                values.append(value)
            if sampl_itr > beat_nsps_inc:
                if sampl_itr < (beat_nsps_inc + beat_nsps_stable):
                    value = 1
                    values.append(value)
                elif (sampl_itr > (beat_nsps_inc + beat_nsps_stable)) and (sampl_itr < (beat_nsps_inc + beat_nsps_stable + beat_nsps_dec)):
                    value = prev_sampl_value_dec - (1 / beat_nsps_dec)
                    prev_sampl_value_dec = value
                    values.append(value)
    
        #
        # Add the gap cycles
        #
        for gap_iter in range(0, int(sampls_per_gap)):
            values.append(0)
    
        #
        # Increment the number of samples constructed to reflect the values
        #
        sampls_const = sampls_const + beat_nsps_defined + gap_sampls
    
    #
    # Open the wave file
    #
    wav_file = wave.open("beat_wave_%s_%s.wav" % (base_freq, beat_freq), "w")
    
    #
    # Define parameters
    #
    nchannels = 2
    sampwidth = 2
    framerate = beat_sampl_rate
    nframes = data_size
    comptype = "NONE"
    compname = "not compressed"
    
    wav_file.setparams((nchannels, sampwidth, framerate, nframes, comptype, compname))
    
    #
    # Calculate isochronic wave point values
    #
    value_iter = 0
    
    for value in values:
        isoch_value = value * math.sin(2 * math.pi * base_freq * (value_iter / beat_sampl_rate))
        value_iter = value_iter + 1
        isoch_values.append(isoch_value)
    
    #
    # Create the wave file (in .wav format)
    #
    for value in isoch_values:
        data = array.array('h')
        data.append(int(value * amplitude / 2)) # left channel
        data.append(int(value * amplitude / 2)) # right channel
    
        wav_file.writeframes(data.tostring())
    
    
    wav_file.close()
    
    try:
        base_freq = int(sys.argv[1], 10)
        beat_freq = int(sys.argv[2], 10)
        sample_rate = int(sys.argv[3], 10)
        output_time = int(sys.argv[4], 10)
        ramp_percent = float(sys.argv[5])
        amplitude = float(sys.argv[6])
    
        make_isochronic_wave(beat_freq, ramp_percent, sample_rate, output_time, base_freq, amplitude)
    except:
        msg = """
    <program> <base freqency> <beat frequency> <sample rate> <output time> <ramp percent> <amplitude>
    """
        print (msg)
    
    #/usr/bin/env python
    #编码:utf-8
    """
    用于创建所需基频、拍频和斜坡频率的等时音调的小程序
    """
    输入数学
    输入波
    导入结构
    导入数组
    导入系统
    def生成等时波(拍频、拍频斜率百分比、拍频采样率、拍频时间、基频、振幅):
    #
    #梯形拍频波在整个周期内出现的时间
    #
    上升时间=1/拍频
    #
    #等比梯形拍之间的间隙
    #
    差距百分比=上升时间*0.15
    #
    #弥补差距
    #
    上升时间=上升时间*0.85
    #
    #样本总数
    #
    数据大小=节拍采样率*节拍时间
    #
    #每秒间隔数=每秒拍数
    #
    无间隔=拍频
    #
    #每个间隙的样本数=分配给间隙的总时间百分比*每秒的样本数
    #
    每差距抽样率=差距百分比*抽样率
    #
    #所有间隙中的样本总数
    #
    间隙取样=无间隙*每个间隙取样
    #
    #更改节拍采样率以适应间隙
    #
    节拍采样率=节拍采样率-间隔采样率
    #
    #nsps=每秒的样本数
    #
    #注:请参见以下图片:
    #
    节拍nsps定义=节拍采样率*启动时间
    跳动量nsps inc=跳动量nsps定义*跳动量斜坡百分比
    跳动量nsps dec=跳动量nsps定义*跳动量斜坡百分比
    节拍nsps稳定=节拍nsps已定义-(节拍nsps公司+节拍nsps十二月)
    节拍未定义=节拍采样率-节拍未定义
    #
    #梯形值
    #
    值=[]
    #
    #梯形*正弦==等时值
    #
    isoch_值=[]
    #
    #构建的样本
    #
    sampls_const=0
    #
    #迭代直到构造出数据大小为的所有样本
    #
    当采样常数<数据大小:
    上一个采样值inc=0.0
    上一个采样值=1.0
    #
    #构造一个梯形拍波(记住这不是整个样本)
    #
    对于范围内的采样率(0,int(拍采样率)):
    如果sampl\U itrbeat\u nsps\u公司:
    如果sampl\u itr<(beat\u nsps\u公司+beat\u nsps\u稳定):
    值=1
    values.append(值)
    elif(数据采集系统>(数据采集系统+数据采集系统稳定)和(数据采集系统<(数据采集系统+数据采集系统稳定+数据采集系统稳定)):
    值=上一个采样值(1/beat\U nsps\U dec)
    上一个样本值下一个数据=值
    values.append(值)
    #
    #添加间隙周期
    #
    对于范围内的间隙(0,int(每个间隙采样)):
    值。追加(0)
    #
    #增加为反映值而构造的样本数
    #
    sampls\u const=sampls\u const+节拍\u nsps\u定义+间隔\u sampls
    #
    #打开wave文件
    #
    wav_file=wave.open(“beat_wave_%s_%s.wav”%(基本频率,拍频),“w”)
    #
    #定义参数
    #
    n通道=2
    sampwidth=2
    帧速率=拍采样率
    n帧=数据大小
    comptype=“无”
    compname=“未压缩”
    setparams((nchannels、sampwidth、帧速率、nframes、comptype、compname))
    #
    #计算等时波点值
    #
    值=0
    对于值中的值:
    isoch\u value=value*math.sin(2*math.pi*base\u freq*(值/拍采样率))
    数值=数值+1
    isoch_值。追加(isoch_值)
    #
    #创建wave文件(以.wav格式)
    #
    对于isoch_值中的值:
    数据=数组。数组('h')
    data.append(int(值*振幅/2))#左声道
    data.append(int(值*振幅/2))#右通道
    wav_file.writeframes(data.tostring())
    wav_文件.close()
    尝试:
    基本频率=int(sys.argv[1],10)
    拍频=int(sys.argv[2],10)
    采样率=int(sys.argv[3],10)
    输出时间=int(sys.argv[4],10)
    斜坡百分比=浮动(系统argv[5])
    振幅=浮动(系统argv[6])
    生成等时波(拍频、斜率、采样率、输出时间、基频、振幅)
    除:
    msg=”“”
    

    上面的格式与audacity使用IsoMod插件生成的格式类似。但是,我想生成一个节拍频率降低为斜坡的音调。为此,我增强了上面的脚本,以在循环中每秒调用一次梯形波生成。但是,设置wav文件的参数以供编写多次(由于斜坡上拍频的变化导致数据大小的变化),我得到以下错误

    G:\>python gen_isochronic_tones.py 70 10 5 11025 5 0.15 8000
    gen_isochronic_tones.py:195: DeprecationWarning: tostring() is deprecated. Use tobytes() instead.
      make_isochronic_wave(beat_freq_start, beat_freq_end, ramp_percent, sample_rate, output_time, base_freq, amplitude)
    Traceback (most recent call last):
      File "gen_isochronic_tones.py", line 195, in <module>
        make_isochronic_wave(beat_freq_start, beat_freq_end, ramp_percent, sample_rate, output_time, base_freq, amplitude)
      File "gen_isochronic_tones.py", line 156, in make_isochronic_wave
        wav_file.setparams((nchannels, sampwidth, framerate, data_size, comptype, compname))
      File "G:\Python37-32\lib\wave.py", line 399, in setparams
        raise Error('cannot change parameters after starting to write')
    wave.Error: cannot change parameters after starting to write
    
    G:\>python gen\u isochronic\u tones.py 70 10 5 11025 5 0.15 8000
    gen_isochronic_tones.py:195:不推荐使用警告:tostring()已不推荐使用。请改用tobytes()。
    生成等时波(拍频开始、拍频结束、斜坡百分比、采样率、输出时间、基频、振幅)
    回溯(最近一次呼叫最后一次):
    文件“gen_isochronic_tones.py”,第195行,在
    生成等时波(拍频开始、拍频结束、斜坡百分比、采样率、输出时间、基频、振幅)
    文件“gen_isochronic_tones.py”,第156行,在make_isochronic_wave中
    setparams((nchannels,sampwidth,帧速率,数据大小,comptype,compname))
    setparams中第399行的文件“G:\Python37-32\lib\wave.py”
    引发错误('开始写入后无法更改参数')
    wave.Error:开始写入后无法更改参数
    
    看起来wave模块只允许更改一次参数(即上面的数据大小)。您知道如何更改数据大小