def delay_effect(Gfb,Gdp,Gff,delay_sec): BLOCKSIZE = 1024 # Number of frames per block RECORD_SECONDS = 500 # delay_sec = 0.5 p = pyaudio.PyAudio() WIDTH = 2 # bytes per sample RATE = 44100 # Sampling rate (samples/second) parameters.d = int( math.floor( RATE * parameters.delay_sec ) ) # Open an output audio stream p = pyaudio.PyAudio() stream = p.open(format = p.get_format_from_width(WIDTH), channels = 2, rate = RATE, input = True, output = True ) output_block = [0.0 for n in range(0, 2*BLOCKSIZE)] delay_buff = [0.0 for n in range(0, parameters.d)] num_blocks = int(RATE / BLOCKSIZE * RECORD_SECONDS) k = 0 print ("**** Playing Delay effect****") for i in range(0, num_blocks): if app.MODE != "Delay": break # Get sample from wave file input_string = stream.read(BLOCKSIZE) # Convert string to number input_value = struct.unpack('hh'* BLOCKSIZE, input_string) for n in range(0, BLOCKSIZE): # Update buffer delay_buff[k] = input_value[2*n] + parameters.delay_Gfb * delay_buff[k] k = k + 1 if k >= parameters.d: # We have reached the end of the buffer. Circle back to front. k = 0 output_block[2*n] = parameters.delay_Gdp * input_value[2*n] + parameters.delay_Gff * delay_buff[k]; output_block[2*n] = clip16(output_block[2*n]) output_block[2*n+1] = clip16(output_block[2*n]) # Clip output value to 16 bits and convert to binary string output_string = struct.pack('hh'* BLOCKSIZE , *output_block) # Write output value to audio stream stream.write(output_string) print("**** Done ****") stream.stop_stream() stream.close() p.terminate()
def my_callback(input_string, block_size, time_info, status): input_frame = struct.unpack('hh', input_string) sample_left = clip16(gain_L * input_frame[0]) sample_right = clip16(gain_R * input_frame[1]) # output_data.append(sample_left) # output_data.append(sample_right) # OR use 'extend' method output_frame = [sample_left, sample_right] output_data.extend(output_frame) return (input_string, pyaudio.paContinue)
def my_callback(input_string, block_size, time_info, status): global kr, kw, n # Create output (initialize to zero) in_sample = struct.unpack('h' * block_size, input_string) for i in range(0,block_size): out_sample = buffer[int(kr)] buffer[kw] = in_sample[i] # Increment read index kr = kr + 1 + W * math.sin( 2 * math.pi * f0 * n / RATE) # Note: kr is not integer! # Ensure that 0 <= kr < buffer_MAX if kr >= buffer_MAX: # End of buffer. Circle back to front. kr = 0 # Increment write index kw = kw + 1 if kw == buffer_MAX: # End of buffer. Circle back to front. kw = 0 out_value[i] = clip16( GAIN * out_sample) n+=1 output_data.append(out_value[i]) # Convert output values to binary string output_string = struct.pack('h'*block_size, *out_value) return (output_string, pyaudio.paContinue)
def my_callback_fun(input_string, block_size, time_info, status): global counter N = block_size # Number of frames if counter > 0: print 'block size is', block_size print block_size, time_info, status counter = counter - 1 for n in range(N): # Do difference equation for block rand_val = random.random() if rand_val < THRESHOLD: x = 15000 else: x = 0 y[n] = b0 * x - a1 * y[n - 1] - a2 * y[n - 2] # What happens when n = 0? # In Python negative indices cycle to end, so it works.. y[n] = clip16(y[n]) # Convert output values to binary string output_string = struct.pack('h' * N, *y) return (output_string, pyaudio.paContinue) # Return data and status
def my_callback(input_string, block_size, time_info, status): global n, kr, kw in_sample = struct.unpack('h', input_string)[0] out_sample = buffer[int(kr)] buffer[kw] = in_sample # Increment read index kr = kr + 1 + W * math.sin( 2 * math.pi * f0 * n / RATE ) # Note: kr is not integer! # Ensure that 0 <= kr < buffer_MAX if kr >= buffer_MAX: # End of buffer. Circle back to front. kr = 0 # Increment write index kw = kw + 1 if kw == buffer_MAX: # End of buffer. Circle back to front. kw = 0 n += 1 sample = clip16( GAIN * out_sample ) output_data.append(sample) # output_string = struct.pack('h' *block_size, sample) return (input_string, pyaudio.paContinue)
def my_callback(input_string, block_size, time_info, status): global n, kr, kw in_sample = struct.unpack('h', input_string)[0] out_sample = buffer[int(kr)] buffer[kw] = in_sample # Increment read index kr = kr + 1 + W * math.sin(2 * math.pi * f0 * n / RATE) # Note: kr is not integer! # Ensure that 0 <= kr < buffer_MAX if kr >= buffer_MAX: # End of buffer. Circle back to front. kr = 0 # Increment write index kw = kw + 1 if kw == buffer_MAX: # End of buffer. Circle back to front. kw = 0 n += 1 sample = clip16(GAIN * out_sample) output_data.append(sample) # output_string = struct.pack('h' *block_size, sample) return (input_string, pyaudio.paContinue)
def delay(input_tuple, RATE): #not yet fs_Hz = RATE Gfb = 0.5 # feed-back gain g0 = 0.6 # direct-path gain g1 = 0.4 # feed-forward gain delay_sec = 1 buffer_length = len(input_tuple) # minimal-length buffer delay = buffer_length - 500 buffer = [0.0 for i in range(0, buffer_length)] output_value = [0.0 for n in range(0, buffer_length)] m1 = delay m2 = 0 for i in range(0, buffer_length): output_value[ i] = g0 * input_tuple[i] + g1 * buffer[m1] # Compute output value buffer[i] = input_tuple[i] # Update buffer # Increment buffer index m1 = m1 + 1 if m1 >= buffer_length: m1 = 0 for n in range(0, len(output_value)): output_value[n] = myfunctions.clip16(output_value[n]) return output_value
def my_callback_fun(input_string, BLOCKSIZE, time_info, status): # Do difference equation for block for n in range(BLOCKSIZE): rand_val = random.random() if rand_val < THRESHOLD: x = 15000 else: x = 0 y[n] = b0 * x - a1 * y[n-1] - a2 * y[n-2] # What happens when n = 0? # In Python negative indices cycle to end, so it works.. y[n] = clip16(y[n]) # If numpy is available, then clipping can be done using: # y = np.clip(y, -32000, 32000) # Convert numeric list to binary string data = struct.pack('h' * BLOCKSIZE, *y); # Write binary string to audio output stream # stream.write(data, BLOCKSIZE) return (data, pyaudio.paContinue)
def delay(input_tuple,RATE): #not yet fs_Hz = RATE Gfb = 0.5 # feed-back gain g0 = 0.6 # direct-path gain g1 = 0.4 # feed-forward gain delay_sec = 1 buffer_length = len(input_tuple) # minimal-length buffer delay = buffer_length-500 print "in delay" buffer = [ 0.0 for i in range(0,buffer_length) ] output_value = [0.0 for n in range(0,buffer_length)] m1 = delay m2 = 0 for i in range(0,buffer_length): output_value[i] = g0 * input_tuple[i] + g1 * buffer[m1] # Compute output value buffer[i] = input_tuple[i] # Update buffer # Increment buffer index m1 = m1 + 1 if m1 >= buffer_length: m1 = 0 for n in range(0,len(output_value)): output_value[n] = myfunctions.clip16(output_value[n]) return output_value
def vibrato(input_tuple, RATE, BLOCK, n, kr): BLOCK = BLOCK * 2 W = 0.6 W1 = 0.0 kw = BLOCK / 2 f0 = 0.1 frac = 0 # print len(input_tuple) buffer1 = [0.0 for i in range(0, BLOCK)] output_value = [0.0 for i in range(0, BLOCK)] for i in range(0, BLOCK): output_sample = ( (1 - frac) * buffer1[int(kr) - 1] + frac * buffer1[int(kr)]) / 2 buffer1[kw] = input_tuple[i] kr = kr + 1 # + W * math.sin( 2 * math.pi * f0 * n /RATE) frac = W * math.sin(2 * math.pi * f0 * n / RATE) if kr >= BLOCK: kr = 0 # print kr kw = kw + 1 if kw == BLOCK: kw = 0 output_value[i] = myfunctions.clip16(W1 * input_tuple[i] + output_sample) # print output_value return output_value
def my_callback_fun(input_string, block_size, time_info, status): global counter N = block_size # Number of frames if counter > 0: print 'block size is',block_size print block_size, time_info, status counter = counter -1 for n in range(N): # Do difference equation for block rand_val = random.random() if rand_val < THRESHOLD: x = 15000 else: x = 0 y[n] = b0 * x - a1 * y[n-1] - a2 * y[n-2] # What happens when n = 0? # In Python negative indices cycle to end, so it works.. y[n] = clip16(y[n]) # Convert output values to binary string output_string = struct.pack('h'*N, *y) return (output_string, pyaudio.paContinue) # Return data and status
def my_callback_fun(input_string, BLOCKSIZE, time_info, status): # Do difference equation for block for n in range(BLOCKSIZE): rand_val = random.random() if rand_val < THRESHOLD: x = 15000 else: x = 0 y[n] = b0 * x - a1 * y[n - 1] - a2 * y[n - 2] # What happens when n = 0? # In Python negative indices cycle to end, so it works.. y[n] = clip16(y[n]) # If numpy is available, then clipping can be done using: # y = np.clip(y, -32000, 32000) # Convert numeric list to binary string data = struct.pack('h' * BLOCKSIZE, *y) # Write binary string to audio output stream # stream.write(data, BLOCKSIZE) return (data, pyaudio.paContinue)
def vibrato(input_tuple,RATE,BLOCK,n,kr): BLOCK = BLOCK*2 W = 0.6 W1 = 0.0 kw = BLOCK/2 f0 = 0.1 frac = 0 # print len(input_tuple) buffer1 = [0.0 for i in range(0,BLOCK)] output_value = [0.0 for i in range(0,BLOCK)] for i in range(0,BLOCK): output_sample = ((1-frac) * buffer1[int(kr) - 1] + frac * buffer1[int(kr)]) / 2 buffer1[kw] = input_tuple[i] kr = kr + 1 # + W * math.sin( 2 * math.pi * f0 * n /RATE) frac = W * math.sin( 2 * math.pi * f0 * n/RATE) if kr >= BLOCK: kr = 0 # print kr kw = kw + 1 if kw == BLOCK: kw = 0 output_value[i] = myfunctions.clip16(W1*input_tuple[i] + output_sample) # print output_value return output_value
def lowpassfilter(input_tuple, RATE): # lowpass fs_Hz = RATE b1, a1 = signal.butter(4, 0.05, 'low') output_value = signal.filtfilt(b1, a1, input_tuple) for n in range(0, len(output_value)): output_value[n] = myfunctions.clip16(output_value[n]) return output_value
def highbandpassfilter(input_tuple, RATE): fs_Hz = RATE h_bp_pass_Hz = np.array([1500.0, 2500.0]) b1, a1 = signal.butter(4, h_bp_pass_Hz / (fs_Hz / 2.0), 'bandpass') output_value = signal.filtfilt(b1, a1, input_tuple) for n in range(0, len(output_value)): output_value[n] = myfunctions.clip16(output_value[n]) return output_value
def lowbandpassfilter(input_tuple, RATE): fs_Hz = RATE l_bp_stop_Hz = np.array([200.0, 1000.0]) b1, a1 = signal.butter(4, l_bp_stop_Hz / (fs_Hz / 2.0), 'bandpass') output_value = signal.filtfilt(b1, a1, input_tuple) for n in range(0, len(output_value)): output_value[n] = myfunctions.clip16(output_value[n]) return output_value
def lowbandpassfilter(input_tuple,RATE): fs_Hz = RATE l_bp_stop_Hz = np.array([200.0, 1000.0]) b1, a1 = signal.butter(4,l_bp_stop_Hz/(fs_Hz / 2.0), 'bandpass') output_value = signal.filtfilt(b1,a1,input_tuple) for n in range(0,len(output_value)): output_value[n] = myfunctions.clip16(output_value[n]) return output_value
def highbandpassfilter(input_tuple,RATE): fs_Hz = RATE h_bp_pass_Hz = np.array([1500.0, 2500.0]) b1, a1 = signal.butter(4,h_bp_pass_Hz/(fs_Hz / 2.0), 'bandpass') output_value = signal.filtfilt(b1,a1,input_tuple) for n in range(0,len(output_value)): output_value[n] = myfunctions.clip16(output_value[n]) return output_value
def lowpassfilter(input_tuple,RATE): # lowpass fs_Hz = RATE b1, a1 = signal.butter(4,0.05, 'low') output_value = signal.filtfilt(b1,a1,input_tuple) for n in range(0,len(output_value)): output_value[n] = myfunctions.clip16(output_value[n]) return output_value
def my_callback_fun(input_string, block_size, time_info, status): N = block_size # Number of frames # Convert string to tuple of numbers input_block = struct.unpack('h'*2*N, input_string) # 2*N for stereo # Create output (initialize to zero) output_block = [0.0 for n in range(2*N)] for n in range(N): output_block[2*n] = clip16( gain_L * input_block[2*n] ) output_block[2*n+1] = clip16( gain_R * input_block[2*n+1] ) # Convert output values to binary string output_string = struct.pack('h'*2*N, *output_block) # 2*N for stereo return (output_string, pyaudio.paContinue) # Return data and status
def my_callback(input_string, block_size, time_info, status): global counter if counter > 0: print 'block size is',block_size print 'time info is',time_info print 'status is', status counter = counter -1 input_frame = struct.unpack('hh', input_string) sample_left = clip16( gain_L * input_frame[0] ) sample_right = clip16( gain_R * input_frame[1] ) # output_data.append(sample_left) # output_data.append(sample_right) # OR use 'extend' method output_frame = [sample_left, sample_right] output_data.extend(output_frame) return (input_string, pyaudio.paContinue)
def funN5(input_tuple): #vibrato from myfunctions import clip16 f0 = 2 W = 0.2 # W = 0 for no effect BUFFER_LEN = 64 # Set buffer length. buffer = np.zeros(BUFFER_LEN) # list of zeros # Buffer (delay line) indices kr = 0 # read index kw = int(0.5 * BUFFER_LEN) # write index (initialize to middle of buffer) # Get previous and next buffer values (since kr is fractional) for n in range(0, 64): kr_prev = int(math.floor(kr)) frac = kr - kr_prev # 0 <= frac < 1 kr_next = kr_prev + 1 if kr_next == BUFFER_LEN: kr_next = 0 # Compute output value using interpolation print(frac) print(buffer[kr_prev]) print(buffer[kr_next]) print(type(buffer)) y0 = (1-frac) * buffer[kr_prev] + frac * buffer[kr_next] # Update buffer buffer[kw] = input_tuple # Increment read index kr = kr + 1 + W * math.sin( 2 * math.pi * f0 * n / RATE ) # Note: kr is fractional (not integer!) # Ensure that 0 <= kr < BUFFER_LEN if kr >= BUFFER_LEN: # End of buffer. Circle back to front. kr = kr - BUFFER_LEN # Increment write index kw = kw + 1 if kw == BUFFER_LEN: # End of buffer. Circle back to front. kw = 0 output_bytes = struct.pack('h', int(clip16(y0))) return(output_bytes)
def my_callback_fun(binary_input_data, block_size, time_info, status): N = block_size # Number of frames # Convert binary data to tuple of numbers input_block = struct.unpack('h' * N, binary_input_data) # Create output (initialize to zero) output_block = [0.0 for n in range(N)] for n in range(N): output_block[n] = clip16(gain * input_block[n]) # Convert output values to binary data binary_output_data = struct.pack('h' * N, *output_block) return (binary_output_data, pyaudio.paContinue) # Return data and status
def my_callback_fun(input_string, block_size, time_info, status): global counter N = block_size # Number of frames if counter > 0: print 'block size is',block_size counter = counter -1 # Convert string to tuple of numbers input_block = struct.unpack('h'*N, input_string) # Create output (initialize to zero) output_block = [0.0 for n in range(N)] for n in range(N): output_block[n] = clip16(gain * input_block[n]) # Convert output values to binary string output_string = struct.pack('h'*N, *output_block) return (output_string, pyaudio.paContinue) # Return data and status
def my_callback_fun(input_string, block_size, time_info, status): global counter N = block_size # Number of frames if counter > 0: print 'block size is', block_size counter = counter - 1 # Convert string to tuple of numbers input_block = struct.unpack('h' * N, input_string) # Create output (initialize to zero) output_block = [0.0 for n in range(N)] for n in range(N): output_block[n] = clip16(gain * input_block[n]) # Convert output values to binary string output_string = struct.pack('h' * N, *output_block) return (output_string, pyaudio.paContinue) # Return data and status
def my_callback(input_string, block_size, time_info, status): global n, kr, kw # print kr print "******" output_string = "" in_sample = struct.unpack("h" * BLOCKSIZE, input_string) # print len(in_sample) sample = [0.0 for i in range(buffer_MAX)] buffer = [0.0 for i in range(buffer_MAX)] output_data = [0.0 for i in range(buffer_MAX)] for i in range(0, BLOCKSIZE): out_sample = buffer[int(kr)] buffer[kw] = in_sample[i] # Increment read index kr = kr + 1 + W * math.sin(2 * math.pi * f0 * n / RATE) # print kr # Note: kr is not integer! # Ensure that 0 <= kr < buffer_MAX if kr >= buffer_MAX: # End of buffer. Circle back to front. kr = 0 # Increment write index kw = kw + 1 if kw == buffer_MAX: # End of buffer. Circle back to front. kw = 0 # time.sleep(0.01) # out_sample[i] = int(out_sample[i]) # print out_sample sample[i] = clip16(GAIN * (out_sample)) output_data[i] = sample[i] n += 1 output_string += struct.pack("h" * len(output_data), *output_data) return (output_string, pyaudio.paContinue)
def my_callback(input_string, block_size, time_info, status): global n, kr, kw # print kr print "******" output_string = "" in_sample = struct.unpack('h' * BLOCKSIZE, input_string) # print len(in_sample) sample = [0.0 for i in range(buffer_MAX)] buffer = [0.0 for i in range(buffer_MAX)] output_data = [0.0 for i in range(buffer_MAX)] for i in range(0, BLOCKSIZE): out_sample = buffer[int(kr)] buffer[kw] = in_sample[i] # Increment read index kr = kr + 1 + W * math.sin(2 * math.pi * f0 * n / RATE) # print kr # Note: kr is not integer! # Ensure that 0 <= kr < buffer_MAX if kr >= buffer_MAX: # End of buffer. Circle back to front. kr = 0 # Increment write index kw = kw + 1 if kw == buffer_MAX: # End of buffer. Circle back to front. kw = 0 # time.sleep(0.01) # out_sample[i] = int(out_sample[i]) # print out_sample sample[i] = clip16(GAIN * (out_sample)) output_data[i] = sample[i] n += 1 output_string += (struct.pack('h' * len(output_data), *output_data)) return (output_string, pyaudio.paContinue)
# Convert string to number input_value = struct.unpack('h', input_string)[0] # Compute output value output_value = Gdp * input_value + Gff * buffer[k]; # Update buffer buffer[k] = input_value + Gfb * buffer[k] # Increment buffer index k = k + 1 if k == buffer_length: # We have reached the end of the buffer. Circle back to front. k = 0 # Clip output value to 16 bits and convert to binary string output_string = struct.pack('h', clip16(output_value)) # Write output value to audio stream stream.write(output_string) # Get next frame (sample) input_string = wf.readframes(1) print("**** Done ****") stream.stop_stream() stream.close() p.terminate()
def my_callback_fun(binary_input_data, block_size, time_info, status): input_tuple = struct.unpack('h', binary_input_data) output_sample = clip16(GAIN * input_tuple[0]) output_list.append(output_sample) return(binary_input_data, pyaudio.paContinue)
# Initialize phase om = 2 * math.pi * f0 / RATE theta = 0 # Create block (initialize to zero) output_block = BLOCKSIZE * [0] for i in range(0, NumBlocks): input_bytes = stream.read(BLOCKSIZE) # Read audio input stream input_tuple = struct.unpack('h' * BLOCKSIZE, input_bytes) # Convert X = np.fft.fft(input_tuple) # Go through block for n in range(0, BLOCKSIZE): # Amplitude modulation (frequency f0) theta = theta + om output_block[n] = int(clip16(input_tuple[n] * math.cos(theta))) # keep theta betwen -pi and pi while theta > math.pi: theta = theta - 2 * math.pi Y = np.fft.fft(output_block) # Convert values to binary data output_bytes = struct.pack('h' * BLOCKSIZE, *output_block) # Write binary data to audio output stream stream.write(output_bytes) # Update y-data of plot plt.subplot(2, 1, 1)
def delay_effect(): BLOCKSIZE = 1024 # Number of frames per block RECORD_SECONDS = 20 delay_sec = 0.5 p = pyaudio.PyAudio() WIDTH = 2 # bytes per sample RATE = 44100 # Sampling rate (samples/second) d = int( math.floor( RATE * delay_sec ) ) # Set parameters of delay system Gfb = .55 # feed-back gain Gdp = 1.0 # direct-path gain Gff = .500 # feed-forward gain # Gff = 0.0 # feed-forward gain (set to zero for no effect) # Open an output audio stream p = pyaudio.PyAudio() stream = p.open(format = p.get_format_from_width(WIDTH), channels = 2, rate = RATE, input = True, output = True ) output_block = [0.0 for n in range(0, 2*BLOCKSIZE)] delay_buff = [0.0 for n in range(0, d)] num_blocks = int(RATE / BLOCKSIZE * RECORD_SECONDS) k = 0 print ("**** Playing ****") for i in range(0, num_blocks): # Get sample from wave file input_string = stream.read(BLOCKSIZE) # Convert string to number input_value = struct.unpack('hh'* BLOCKSIZE, input_string) # t = t + 1 # if t == 2: # delay = input_value # t = 0 for n in range(0, BLOCKSIZE): # Update buffer delay_buff[k] = input_value[2*n] + Gfb * delay_buff[k] k = k + 1 if k == d: # We have reached the end of the buffer. Circle back to front. k = 0 output_block[2*n] = Gdp * input_value[2*n] + Gff * delay_buff[k]; output_block[2*n] = clip16(output_block[2*n]) output_block[2*n+1] = clip16(output_block[2*n]) # # Compute output value # delay[n] = input_value[2*(n-d)] + Gfb * delay[(n-d)] # output_block[2*n] = Gdp * input_value[2*n] + Gff * delay[n] # output_block[2*n] = cli p16(output_block[2*n]) # output_block[2*n+1] = clip16(output_block[2*n]) # # if output_value <= 0.1: # break # delay[n] = input_value[2*(n-d)] # output_block[2*n] = Gdp * input_value[2*n] + Gff * delay[n] # # output_block[2*n] = Gfb*output_block[2*(n-d)] + input_value[2*n] + (Gff-Gfb)*input_value[2*(n-d)] # output_block[2*n] = clip16(output_block[2*n]) # output_block[2*n+1] = clip16(output_block[2*n]) # for n in range(0,BLOCKSIZE): # # output_block[2*n] = input_value[2*n]*Gdp + Gff * buffer[n]; # # output_block[2*n+1] = input_value[2*n]*Gdp + Gff * buffer[n]; # output_block[2*n] = input_value[2*n]*Gdp # buffer[n] = input_value[2*n] + Gfb * buffer[n] # # output_bolck = clip16_arr(output_block)+ Gff*buffer # output_block = [x + y for x, y in zip(clip16_arr(output_block), Gff * clip16_arr(buffer))] # Clip output value to 16 bits and convert to binary string output_string = struct.pack('hh'* BLOCKSIZE , *output_block) # Write output value to audio stream stream.write(output_string) print("**** Done ****") stream.stop_stream() stream.close() p.terminate()
# shift_array(Y,1) # shift_array(X,1) # # print 'aaaa\n' # print output_block # Update y-data of plot output_block_fft = np.fft.fft(output_block,RATE*2) # output_block_fft = np.fft.fft(output_block,RATE*2) # print (output_block_fft),'aaa\n' print np.log10(output_block_fft[50:70]) line.set_ydata(np.log10(abs(output_block_fft))*20) plt.draw() # np.array(output_block).tolist() for n in range(0,BLOCKSIZE): # output_block[n] = int(output_block[n]) output_block[n] = myfunctions.clip16(np.real(output_block[n])) # Convert values to binary string output_string = struct.pack('h' * BLOCKSIZE, *output_block) # output_string = struct.pack('h' * BLOCKSIZE, *input_tuple) stream.write(output_string) plt.close() stream.stop_stream() stream.close() p.terminate() print '* Done'
print('* Start') while len(input_bytes) > 0: # Convert binary data to number x0, = struct.unpack('h', input_bytes) # Compute output value # y(n) = b0 x(n) + G x(n-N) y0 = b0 * x0 + G * buffer[0] # Update buffer buffer.append(x0) del buffer[0] # remove first value # Convert output value to binary data output_bytes = struct.pack('h', int(clip16(y0))) # Write output value to audio stream stream.write(output_bytes) # Get next frame (sample) input_bytes = wf.readframes(1) print('* Finished') stream.stop_stream() stream.close() p.terminate() wf.close()
# w4 = w3 # w3 = w2 # w2 = w1 # w1 = w0 x4 = x3 x3 = x2 x2 = x1 x1 = x0 y4 = y3 y3 = y2 y2 = y1 y1 = y0 # Compute output value output_value = int(clip16(y0)) # Integer in allowed range # Convert output value to binary string output_string = struct.pack('h', output_value) ww.writeframes(output_string) # Write binary string to audio stream stream.write(output_string) # Get next frame from wave file input_string = wf.readframes(1) print('* Finished') stream.stop_stream() stream.close() ww.close()
def flanger_effect(): BLOCKSIZE = 1024 # Number of frames per block RECORD_SECONDS = 500 p = pyaudio.PyAudio() WIDTH = 2 # bytes per sample RATE = 44100 # Sampling rate (samples/second) stream = p.open(format = p.get_format_from_width(WIDTH), channels = 2, rate = RATE, input = True, output = True) # Create a buffer (delay line) for past values # Create block (initialize to zero) output_block = [0.0 for n in range(0, 2*BLOCKSIZE)] # Number of blocks in wave file num_blocks = int(RATE / BLOCKSIZE * RECORD_SECONDS) # Create a buffer (delay line) for past values # buffer_MAX = 1024 # Buffer length buffer = [0.0 for i in range(BLOCKSIZE)] # Initialize to zero # Buffer (delay line) indices kr = 0 # read index kw = int(0.5 * BLOCKSIZE) # write index (initialize to middle of buffer) kw = BLOCKSIZE/2 output_all = '' # output signal in all (string) # Initialize angle theta = 0.0 print ('**** Playing Flanger effect****') # Loop through wave file for i in range(0, num_blocks): # Block-to-block angle increment theta_del = (float(BLOCKSIZE*parameters.flanger_f)/RATE - math.floor(BLOCKSIZE*parameters.flanger_f/RATE)) * 2.0 * math.pi if app.MODE != "Flanger": break # Get sample from wave file input_string = stream.read(BLOCKSIZE) # Convert string to number input_value = struct.unpack('hh'* BLOCKSIZE, input_string) # Go through block for n in range(0, BLOCKSIZE): # Amplitude modulation (f0 Hz cosine) # Get previous and next buffer values (since kr is fractional) kr_prev = int(math.floor(kr)) kr_next = kr_prev + 1 frac = kr - kr_prev # 0 <= frac < 1 #print frac if kr_next >= BLOCKSIZE: kr_next = kr_next - BLOCKSIZE # Compute output value using interpolation k = buffer[kr_prev] * (1-frac) r = buffer[kr_next] * frac output_block[2*n] = k + r + input_value[2*n] * parameters.flanger_gain output_block[2*n] = clip16(output_block[2*n]) output_block[2*n+1] = output_block[2*n] # buffer # print '--------------' buffer[kw] = input_value[2*n] # Increment read index kr = kr + 1 + parameters.flanger_w * math.sin( 2 * math.pi * parameters.flanger_f * n / RATE + theta) # Note: kr is fractional (not integer!) # Ensure that 0 <= kr < buffer_MAX if kr >= BLOCKSIZE: # End of buffer. Circle back to front. kr = 0 # Increment write index kw = kw + 1 if kw == BLOCKSIZE: # End of buffer. Circle back to front. kw = 0 theta1 = theta + theta_del # print output_block output_string = struct.pack('hh'* BLOCKSIZE , *output_block) # Write output to audio stream stream.write(output_string) output_all = output_all + output_string print('* Done') stream.stop_stream() stream.close() p.terminate()
input_tuple = struct.unpack("h" * BLOCKSIZE, input_string) # Convert # Go through block for n in range(0, BLOCKSIZE): # Amplitude modulation (f0 Hz cosine) # output_block[n] = input_tuple[n] * math.cos(2*math.pi*n*f0/RATE + theta) # output_block[n] = input_tuple[n] * 1.0 # for no processing # Difference equation X[6] = input_tuple[n] Yvalue = a[1] * Y[5] + a[2] * Y[4] + a[3] * Y[3] + a[4] * Y[2] + a[5] * Y[1] + a[6] * Y[0] Y[6] = b[0] * X[6] + b[1] * X[5] + b[2] * X[4] + b[3] * X[3] + b[4] * X[2] + b[5] * X[1] + b[6] * X[0] - Yvalue # unclipped_value = np.real(Y[7] * cmath.exp(I * 2 *math.pi*n*f0/RATE)) # output_block[n] = clip16(unclipped_value) output_block[n] = clip16(Y[6]) Y = shift(Y, 1) X = shift(X, 1) # Set angle for next block # theta = theta + theta_del # line.set_ydata(output_block) # Update y-data of plot # plt.draw() # Convert values to binary string output_string = struct.pack("h" * BLOCKSIZE, *output_block) # Write binary string to audio output stream stream.write(output_string) # append new to total
def callback_record(input_string, block_size, time_info, status): data = struct.unpack('h', input_string) sample = clip16(GAIN * data[0]) output_data.append(sample) return (None, pyaudio.paContinue)
input_string = stream.read(BLOCKSIZE) # BLOCKSIZE = number of frames read input_tuple = struct.unpack('hh'*BLOCKSIZE, input_string) # Convert # Go through block for n in range(0, BLOCKSIZE): # Amplitude modulation (f0 Hz cosine) # output_block[n] = input_tuple[n] * math.cos(2*math.pi*n*f0/RATE + theta) # output_block[n] = input_tuple[n] * 1.0 # for no processing # Difference equation X[7] = input_tuple[2*n] Yvalue = a[1] * Y[6] + a[2] * Y[5] + a[3] * Y[4] + a[4] * Y[3] + a[5] * Y[2] + a[6] * Y[1] + a[7] * Y[0] Y[7] = b[0] * X[7] + b[1] * X[6] + b[2] * X[5] + b[3] * X[4] + b[4] * X[3] + b[5] * X[2] + b[6] * X[1] + b[7] * X[0]- Yvalue unclipped_value = np.real(Y[7] * cmath.exp(I * 2 *math.pi*n*f0/RATE)) output_block[2*n] = clip16(unclipped_value) output_block[2*n+1] = output_block[2*n] Y = shift(Y,1) X = shift(X,1) # Set angle for next block # theta = theta + theta_del # line.set_ydata(output_block) # Update y-data of plot # plt.draw() # Convert values to binary string output_string = struct.pack('hh' * BLOCKSIZE, *output_block) # Write binary string to audio output stream stream.write(output_string)
line, = plt.plot([], [], color = 'blue') # Create empty line line.set_xdata(t) # x-data of plot (time) output_data = [] # Open audio device: p = pyaudio.PyAudio() stream = p.open(format = p.get_format_from_width(WIDTH), channels = CHANNELS, rate = RATE, input = True, output = False) for i in range(1, NumBlocks): input_string = stream.read(BLOCKSIZE)# Read audio input stream input_tuple = struct.unpack('h'*BLOCKSIZE, input_string) # Convert sample = clip16( GAIN * input_tuple[0]) output_data.append(sample) line.set_ydata(input_tuple) # Update y-data of plot # # Print block number: # if i % 20 == 0: # print i, plt.draw() plt.close() stream.stop_stream() # Convert output signal to binary string output_string = struct.pack('h'*len(output_data), *output_data) # write data to wave file wf = wave.open(filename, 'w')
# Difference equation y0 = b0 * x0 + b2 * x2 + b4 * x4 - a1 * y1 - a2 * y2 - a3 * y3 - a4 * y4 # Delays x4 = x3 x3 = x2 x2 = x1 x1 = x0 y4 = y3 y3 = y2 y2 = y1 y1 = y0 # Compute output value output_value = int(clip16(10 * y0)) # Number # output_value = int(clip16(x0) # Bypass filter (listen to input directly) # Convert output value to binary string output_bytes = struct.pack('h', output_value) # Write binary string to audio stream stream.write(output_bytes) print('* Finished') stream.stop_stream() stream.close() p.terminate()
# Get previous and next buffer values (since kr is fractional) kr_prev = int(math.floor(kr)) kr_next = (kr_prev + 1) % BLOCKSIZE frac = kr - kr_prev # 0 <= frac < 1 # if kr_next >= BLOCKSIZE: # kr_next = kr_next - BLOCKSIZE # Compute output value using interpolation prev = (1 - frac) * buffer[kr_prev] nextt = frac * buffer[kr_next] # output_block[n] = prev + nextt output_block[2 * n] = (prev + nextt) * gain + input_value[n] output_block[2 * n + 1] = (prev + nextt) * gain + input_value[n] output_block[2 * n] = clip16(output_block[2 * n]) output_block[2 * n + 1] = clip16(output_block[2 * n + 1]) # Update buffer (pure delay) buffer[kw] = input_value[n] # Increment read index kr = kr + 1 + W * math.sin(2 * math.pi * f0 * n / RATE + theta) # Note: kr is fractional (not integer!) # Ensure that 0 <= kr < BLOCKSIZE if kr >= BLOCKSIZE: # End of buffer. Circle back to front. kr = 0 # Increment write index
if rand_val_r < THRESHOLD: x_r = 15000 else: x_r = 0 rand_val_l = random.random() if rand_val_l < THRESHOLD: x_l = 15000 else: x_l = 0 y[2*n] = b0r * x_r - a1r * y[2*(n-1)] - a2 * y[2*(n-2)] # What happens when n = 0? # In Python negative indices cycle to end, so it works.. y[2*n] = clip16(y[2*n]) y[2*n-1] = b0l * x_l - a1l * y[2*(n-1)-1] - a2 * y[2*(n-2)-1] # What happens when n = 0? # In Python negative indices cycle to end, so it works.. y[2*n-1] = clip16(y[2*n-1]) y_plot_r[n] = y[2*n] + 10000 y_plot_l[n] = y[2*n+1] - 10000 liner.set_ydata(y_plot_r) linel.set_ydata(y_plot_l) plt.title('Block {0:d}'.format(i)) plt.draw()
def flanger_effect(f, w, gain): BLOCKSIZE = 1024 # Number of frames per block RECORD_SECONDS = 10 # f0 = f # W1 = w # f1 = f0 # W2 = W1 gain = gain p = pyaudio.PyAudio() WIDTH = 2 # bytes per sample RATE = 44100 # Sampling rate (samples/second) number_of_devices = p.get_device_count() print('There are {0:d} devices'.format(number_of_devices)) property_list = [ 'defaultSampleRate', 'maxInputChannels', 'maxOutputChannels' ] for i in range(0, number_of_devices): print('Device {0:d} has:'.format(i)) for s in property_list: print ' ', s, '=', p.get_device_info_by_index(i)[s] stream = p.open(format=p.get_format_from_width(WIDTH), channels=2, rate=RATE, input=True, output=True) # Create a buffer (delay line) for past values # Create block (initialize to zero) output_block = [0.0 for n in range(0, 2 * BLOCKSIZE)] # Number of blocks in wave file num_blocks = int(RATE / BLOCKSIZE * RECORD_SECONDS) # Create a buffer (delay line) for past values # buffer_MAX = 1024 # Buffer length buffer = [0.0 for i in range(BLOCKSIZE)] # Initialize to zero # buffer2 = [0.0 for i in range(BLOCKSIZE)] # Initialize to zero # Buffer (delay line) indices kr = 0 # read index kw = int(0.5 * BLOCKSIZE) # write index (initialize to middle of buffer) kw = BLOCKSIZE / 2 # Buffer (delay line) indices # kr2 = 0 # read index # kw2 = int(0.5 * BLOCKSIZE) # write index (initialize to middle of buffer) # kw2 = BLOCKSIZE/2 output_all = '' # output signal in all (string) # Initialize angle theta = 0.0 # theta2 = 0.0 # Block-to-block angle increment theta_del = (float(BLOCKSIZE * f) / RATE - math.floor(BLOCKSIZE * f / RATE)) * 2.0 * math.pi # theta_del2 = (float(BLOCKSIZE*f1)/RATE - math.floor(BLOCKSIZE*f1/RATE)) * 2.0 * math.pi print('* Playing...') # Loop through wave file for i in range(0, num_blocks): # Get sample from wave file input_string = stream.read(BLOCKSIZE) # Convert string to number input_value = struct.unpack('hh' * BLOCKSIZE, input_string) # Get previous and next buffer values (since kr is fractional) # Go through block for n in range(0, BLOCKSIZE): # Amplitude modulation (f0 Hz cosine) # Get previous and next buffer values (since kr is fractional) kr_prev = int(math.floor(kr)) kr_next = kr_prev + 1 frac = kr - kr_prev # 0 <= frac < 1 #print frac if kr_next >= BLOCKSIZE: kr_next = kr_next - BLOCKSIZE # Compute output value using interpolation k = buffer[kr_prev] * (1 - frac) r = buffer[kr_next] * frac output_block[2 * n] = k + r + input_value[2 * n] * gain output_block[2 * n] = clip16(output_block[2 * n]) output_block[2 * n + 1] = output_block[2 * n] # buffer # print '--------------' buffer[kw] = input_value[2 * n] # Increment read index kr = kr + 1 + w * math.sin(2 * math.pi * f * n / RATE + theta) # Note: kr is fractional (not integer!) # Ensure that 0 <= kr < buffer_MAX if kr >= BLOCKSIZE: # End of buffer. Circle back to front. kr = 0 # Increment write index kw = kw + 1 if kw == BLOCKSIZE: # End of buffer. Circle back to front. kw = 0 theta1 = theta + theta_del # print output_block output_string = struct.pack('hh' * BLOCKSIZE, *output_block) # Write output to audio stream stream.write(output_string) output_all = output_all + output_string print('* Done') stream.stop_stream() stream.close() p.terminate()
def wah_effect(fw,damp): RECORD_SECONDS = 15 BLOCKSIZE = 1024 # Number of frames per block p = pyaudio.PyAudio() WIDTH = 2 # bytes per sample RATE = 44100 # Sampling rate (samples/second) # damp = 0.015 gain = 1 # w = 1 # f = 1 fw_min = 300 fw_max = 9000 # fw = 2000 delta = fw / RATE number_of_devices = p.get_device_count() print('There are {0:d} devices'.format(number_of_devices)) property_list = ['defaultSampleRate', 'maxInputChannels', 'maxOutputChannels'] for i in range(0, number_of_devices): print('Device {0:d} has:'.format(i)) for s in property_list: print ' ', s, '=', p.get_device_info_by_index(i)[s] stream = p.open(format = p.get_format_from_width(WIDTH), channels = 2, rate = RATE, input = True, output = True) # Create a buffer (delay line) for past values # Create block (initialize to zero) yh = [0.0 for n in range(0, 2*BLOCKSIZE)] yb = [0.0 for n in range(0, 2*BLOCKSIZE)] yl = [0.0 for n in range(0, 2*BLOCKSIZE)] output_block = [0.0 for n in range(0, 2*BLOCKSIZE)] output_all = '' # output signal in all (string) num_blocks = int(RATE / BLOCKSIZE * RECORD_SECONDS) print ('* Playing...') # Loop through wave file for i in range(0, num_blocks): # Get sample from wave file input_string = stream.read(BLOCKSIZE) # Convert string to number input_value = struct.unpack('hh'* BLOCKSIZE, input_string) fc = fw_min f1 = 2 * math.sin(math.pi*fc/RATE) # yh = [0.0 for n in range(0, 2*BLOCKSIZE)] # yb = [0.0 for n in range(0, 2*BLOCKSIZE)] # yl = [0.0 for n in range(0, 2*BLOCKSIZE)] # yh[0] = input_value[0] # yb[0] = f1 * yh[0] # yl[0] = f1 * yb[0] # Get previous and next buffer values (since kr is fractional) # fc = fw_min # if fc >= fw_max: # delta = -delta # if fc <= fw_min: # delta = -delta # Go through block for n in range(0, BLOCKSIZE): if fc >= fw_max: delta = -delta if fc <= fw_min: delta = -delta f1 = 2 * math.sin(math.pi*fc/RATE) Q1 = 2 * damp yh[2*n] = input_value[2*n] - yl[2*(n-1)] - Q1 * yb[2*(n-1)] yb[2*n] = f1 * yh[2*n] + yb[2*(n-1)] yl[2*n] = f1 * yb[2*n] + yl[2*(n-1)] fc = fc + delta yb[2*n] = clip16(yb[2*n]) yb[2*n+1] = clip16(yb[2*n]) output_string = struct.pack('hh'* BLOCKSIZE , *yb) # Write output to audio stream stream.write(output_string) output_all = output_all + output_string print('* Done') stream.stop_stream() stream.close() p.terminate()
def flanger_effect(f,w,gain): BLOCKSIZE = 1024 # Number of frames per block RECORD_SECONDS = 10 # f0 = f # W1 = w # f1 = f0 # W2 = W1 gain = gain p = pyaudio.PyAudio() WIDTH = 2 # bytes per sample RATE = 44100 # Sampling rate (samples/second) number_of_devices = p.get_device_count() print('There are {0:d} devices'.format(number_of_devices)) property_list = ['defaultSampleRate', 'maxInputChannels', 'maxOutputChannels'] for i in range(0, number_of_devices): print('Device {0:d} has:'.format(i)) for s in property_list: print ' ', s, '=', p.get_device_info_by_index(i)[s] stream = p.open(format = p.get_format_from_width(WIDTH), channels = 2, rate = RATE, input = True, output = True) # Create a buffer (delay line) for past values # Create block (initialize to zero) output_block = [0.0 for n in range(0, 2*BLOCKSIZE)] # Number of blocks in wave file num_blocks = int(RATE / BLOCKSIZE * RECORD_SECONDS) # Create a buffer (delay line) for past values # buffer_MAX = 1024 # Buffer length buffer = [0.0 for i in range(BLOCKSIZE)] # Initialize to zero # buffer2 = [0.0 for i in range(BLOCKSIZE)] # Initialize to zero # Buffer (delay line) indices kr = 0 # read index kw = int(0.5 * BLOCKSIZE) # write index (initialize to middle of buffer) kw = BLOCKSIZE/2 # Buffer (delay line) indices # kr2 = 0 # read index # kw2 = int(0.5 * BLOCKSIZE) # write index (initialize to middle of buffer) # kw2 = BLOCKSIZE/2 output_all = '' # output signal in all (string) # Initialize angle theta = 0.0 # theta2 = 0.0 # Block-to-block angle increment theta_del = (float(BLOCKSIZE*f)/RATE - math.floor(BLOCKSIZE*f/RATE)) * 2.0 * math.pi # theta_del2 = (float(BLOCKSIZE*f1)/RATE - math.floor(BLOCKSIZE*f1/RATE)) * 2.0 * math.pi print ('* Playing...') # Loop through wave file for i in range(0, num_blocks): # Get sample from wave file input_string = stream.read(BLOCKSIZE) # Convert string to number input_value = struct.unpack('hh'* BLOCKSIZE, input_string) # Get previous and next buffer values (since kr is fractional) # Go through block for n in range(0, BLOCKSIZE): # Amplitude modulation (f0 Hz cosine) # Get previous and next buffer values (since kr is fractional) kr_prev = int(math.floor(kr)) kr_next = kr_prev + 1 frac = kr - kr_prev # 0 <= frac < 1 #print frac if kr_next >= BLOCKSIZE: kr_next = kr_next - BLOCKSIZE # Compute output value using interpolation k = buffer[kr_prev] * (1-frac) r = buffer[kr_next] * frac output_block[2*n] = k + r + input_value[2*n] * gain output_block[2*n] = clip16(output_block[2*n]) output_block[2*n+1] = output_block[2*n] # buffer # print '--------------' buffer[kw] = input_value[2*n] # Increment read index kr = kr + 1 + w * math.sin( 2 * math.pi * f * n / RATE + theta) # Note: kr is fractional (not integer!) # Ensure that 0 <= kr < buffer_MAX if kr >= BLOCKSIZE: # End of buffer. Circle back to front. kr = 0 # Increment write index kw = kw + 1 if kw == BLOCKSIZE: # End of buffer. Circle back to front. kw = 0 theta1 = theta + theta_del # print output_block output_string = struct.pack('hh'* BLOCKSIZE , *output_block) # Write output to audio stream stream.write(output_string) output_all = output_all + output_string print('* Done') stream.stop_stream() stream.close() p.terminate()
def wah_effect(f_lfo, fc_min, w): RECORD_SECONDS = 10 BLOCKSIZE = 1024 # Number of frames per block gain = 1 # w = 1 # f_lfo = 0.2 fc_min = 250 fc_max = 1200 # initial # fc = fc_min p = pyaudio.PyAudio() WIDTH = 2 # bytes per sample RATE = 44100 # Sampling rate (samples/second) number_of_devices = p.get_device_count() print ("There are {0:d} devices".format(number_of_devices)) property_list = ["defaultSampleRate", "maxInputChannels", "maxOutputChannels"] for i in range(0, number_of_devices): print ("Device {0:d} has:".format(i)) for s in property_list: print " ", s, "=", p.get_device_info_by_index(i)[s] stream = p.open(format=p.get_format_from_width(WIDTH), channels=2, rate=RATE, input=True, output=True) # Create a buffer (delay line) for past values # Create block (initialize to zero) output_block = [0.0 for n in range(0, 2 * BLOCKSIZE)] output_block_filt = [0.0 for n in range(0, 2 * BLOCKSIZE)] bandpass = [0.0 for n in range(0, 2 * BLOCKSIZE)] mix = [0.0 for n in range(0, 2 * BLOCKSIZE)] # Create a buffer (delay line) for past values # buffer_MAX = 1024 # Buffer length buffer = [0.0 for i in range(BLOCKSIZE)] # Initialize to zero buffer2 = [0.0 for i in range(BLOCKSIZE)] # Initialize to zero # Buffer (delay line) indices kr = 0 # read index kw = int(0.5 * BLOCKSIZE) # write index (initialize to middle of buffer) kw = BLOCKSIZE / 2 # Buffer (delay line) indices # kr2 = 0 # read index # kw2 = int(0.5 * BLOCKSIZE) # write index (initialize to middle of buffer) # kw2 = BLOCKSIZE/2 output_all = "" # output signal in all (string) fc_bandpass = 0.0 # Initialize angle theta = 0.0 # theta2 = 0.0 # Block-to-block angle increment theta_del = (float(BLOCKSIZE * f_lfo) / RATE - math.floor(BLOCKSIZE * f_lfo / RATE)) * 2.0 * math.pi # theta_del2 = (float(BLOCKSIZE*f1)/RATE - math.floor(BLOCKSIZE*f1/RATE)) * 2.0 * math.pi num_blocks = int(RATE / BLOCKSIZE * RECORD_SECONDS) print ("* Playing...") # Loop through wave file for i in range(0, num_blocks): # Get sample from wave file input_string = stream.read(BLOCKSIZE) # Convert string to number input_value = struct.unpack("hh" * BLOCKSIZE, input_string) # X = np.fft.fft(input_value) # dB = 20 * np.log10(abs(X)) # if np.max(dB) > 80: # fc = 800 # else: # fc = 400 # 100 ,900 # 400 , 2000 # 400 , 1200 # fc = (f_max - f_min) / 2 # f_max = 2 * fc + f_min bandpass = butter_bandpass_filter(input_value, fc_min, fc_max, RATE, order=1) # # Get previous and next buffer values (since kr is fractional) # fc_bandpass = fc_bandpass + delta # if fc_bandpass >= fc_max: # delta = -delta # if fc_bandpass <= fc_min: # delta = -delta # Go through block for n in range(0, BLOCKSIZE): # Amplitude modulation (f0 Hz cosine) # Get previous and next buffer values (since kr is fractional) kr_prev = int(math.floor(kr)) kr_next = kr_prev + 1 frac = kr - kr_prev # 0 <= frac < 1 # print frac if kr_next >= BLOCKSIZE: kr_next = kr_next - BLOCKSIZE # Compute output value using interpolation k = buffer[kr_prev] * (1 - frac) r = buffer[kr_next] * frac k2 = buffer2[kr_prev] * (1 - frac) r2 = buffer2[kr_next] * frac # butter_bandpass_filter(data, lowcut, highcut, fs, order=5): # output_block[2*n] = butter_bandpass_filter(mix[2*n],500.0,3000.0,2000,order = 5) + mix[2*n] output_block[2 * n] = k + r + k2 + r2 output_block[2 * n] = clip16(output_block[2 * n]) output_block[2 * n + 1] = output_block[2 * n] # buffer # print '--------------' buffer[kw] = input_value[2 * n] buffer2[kw] = bandpass[2 * n] # Increment read index kr = kr + 1 + w * math.sin(2 * math.pi * f_lfo * n / RATE + theta) # Note: kr is fractional (not integer!) # f = fl_min + delta_lfo # Ensure that 0 <= kr < buffer_MAX if kr >= BLOCKSIZE: # End of buffer. Circle back to front. kr = 0 # Increment write index kw = kw + 1 if kw == BLOCKSIZE: # End of buffer. Circle back to front. kw = 0 fc_bandpass = fc_min + 0.5 * w * (1 + math.sin(2 * math.pi * f_lfo * n / RATE)) f_max = 2 * fc_bandpass + fc_min theta = theta + theta_del # print output_block # output_block[2*n] = butter_bandpass_filter(mix[2*n],500.0,3000.0,2000,order = 5) + mix[2*n] # if fc >=fc_max: # fc = fc_min # output_block_filt = butter_bandpass_filter(output_block,fc,3000.0,2000,order = 5) # # output_block = clip16_arr(output_block) + clip16_arr(output_block_filt) # fc = fc_min + 200 # output_block = [x + y for x, y in zip(clip16_arr(output_block_filt), output_block)] # output_block = clip16_arr(output_block) output_string = struct.pack("hh" * BLOCKSIZE, *output_block) # output_block = [0.0 for n in range(0, 2*BLOCKSIZE)] # Write output to audio stream stream.write(output_string) output_all = output_all + output_string print ("* Done") stream.stop_stream() stream.close() p.terminate()
def wah_effect(f_lfo,fc_min,w): RECORD_SECONDS = 10 BLOCKSIZE = 1024 # Number of frames per block gain = 1 # w = 1 # f_lfo = 0.2 fc_min = 250 fc_max = 1200 # initial # fc = fc_min p = pyaudio.PyAudio() WIDTH = 2 # bytes per sample RATE = 44100 # Sampling rate (samples/second) number_of_devices = p.get_device_count() print('There are {0:d} devices'.format(number_of_devices)) property_list = ['defaultSampleRate', 'maxInputChannels', 'maxOutputChannels'] for i in range(0, number_of_devices): print('Device {0:d} has:'.format(i)) for s in property_list: print ' ', s, '=', p.get_device_info_by_index(i)[s] stream = p.open(format = p.get_format_from_width(WIDTH), channels = 2, rate = RATE, input = True, output = True) # Create a buffer (delay line) for past values # Create block (initialize to zero) output_block = [0.0 for n in range(0, 2*BLOCKSIZE)] output_block_filt = [0.0 for n in range(0, 2*BLOCKSIZE)] bandpass = [0.0 for n in range(0, 2*BLOCKSIZE)] mix = [0.0 for n in range(0, 2*BLOCKSIZE)] # Create a buffer (delay line) for past values # buffer_MAX = 1024 # Buffer length buffer = [0.0 for i in range(BLOCKSIZE)] # Initialize to zero buffer2 = [0.0 for i in range(BLOCKSIZE)] # Initialize to zero # Buffer (delay line) indices kr = 0 # read index kw = int(0.5 * BLOCKSIZE) # write index (initialize to middle of buffer) kw = BLOCKSIZE/2 # Buffer (delay line) indices # kr2 = 0 # read index # kw2 = int(0.5 * BLOCKSIZE) # write index (initialize to middle of buffer) # kw2 = BLOCKSIZE/2 output_all = '' # output signal in all (string) fc_bandpass = 0.0 # Initialize angle theta = 0.0 # theta2 = 0.0 # Block-to-block angle increment theta_del = (float(BLOCKSIZE*f_lfo)/RATE - math.floor(BLOCKSIZE*f_lfo/RATE)) * 2.0 * math.pi # theta_del2 = (float(BLOCKSIZE*f1)/RATE - math.floor(BLOCKSIZE*f1/RATE)) * 2.0 * math.pi num_blocks = int(RATE / BLOCKSIZE * RECORD_SECONDS) print ('* Playing...') # Loop through wave file for i in range(0, num_blocks): # Get sample from wave file input_string = stream.read(BLOCKSIZE) # Convert string to number input_value = struct.unpack('hh'* BLOCKSIZE, input_string) # X = np.fft.fft(input_value) # dB = 20 * np.log10(abs(X)) # if np.max(dB) > 80: # fc = 800 # else: # fc = 400 # 100 ,900 # 400 , 2000 # 400 , 1200 # fc = (f_max - f_min) / 2 # f_max = 2 * fc + f_min bandpass = butter_bandpass_filter(input_value,fc_min,fc_max,RATE,order = 1) # # Get previous and next buffer values (since kr is fractional) # fc_bandpass = fc_bandpass + delta # if fc_bandpass >= fc_max: # delta = -delta # if fc_bandpass <= fc_min: # delta = -delta # Go through block for n in range(0, BLOCKSIZE): # Amplitude modulation (f0 Hz cosine) # Get previous and next buffer values (since kr is fractional) kr_prev = int(math.floor(kr)) kr_next = kr_prev + 1 frac = kr - kr_prev # 0 <= frac < 1 #print frac if kr_next >= BLOCKSIZE: kr_next = kr_next - BLOCKSIZE # Compute output value using interpolation k = buffer[kr_prev] * (1-frac) r = buffer[kr_next] * frac k2 = buffer2[kr_prev] * (1-frac) r2 = buffer2[kr_next] * frac # butter_bandpass_filter(data, lowcut, highcut, fs, order=5): # output_block[2*n] = butter_bandpass_filter(mix[2*n],500.0,3000.0,2000,order = 5) + mix[2*n] output_block[2*n] = k + r + k2 +r2 output_block[2*n] = clip16(output_block[2*n]) output_block[2*n+1] = output_block[2*n] # buffer # print '--------------' buffer[kw] = input_value[2*n] buffer2[kw] = bandpass[2*n] # Increment read index kr = kr + 1 + w * math.sin( 2 * math.pi * f_lfo * n / RATE + theta) # Note: kr is fractional (not integer!) # f = fl_min + delta_lfo # Ensure that 0 <= kr < buffer_MAX if kr >= BLOCKSIZE: # End of buffer. Circle back to front. kr = 0 # Increment write index kw = kw + 1 if kw == BLOCKSIZE: # End of buffer. Circle back to front. kw = 0 fc_bandpass = fc_min + 0.5 * w * (1 + math.sin(2 * math.pi * f_lfo * n /RATE )) f_max = 2 * fc_bandpass + fc_min theta = theta + theta_del # print output_block # output_block[2*n] = butter_bandpass_filter(mix[2*n],500.0,3000.0,2000,order = 5) + mix[2*n] # if fc >=fc_max: # fc = fc_min # output_block_filt = butter_bandpass_filter(output_block,fc,3000.0,2000,order = 5) # # output_block = clip16_arr(output_block) + clip16_arr(output_block_filt) # fc = fc_min + 200 # output_block = [x + y for x, y in zip(clip16_arr(output_block_filt), output_block)] # output_block = clip16_arr(output_block) output_string = struct.pack('hh'* BLOCKSIZE , *output_block) # output_block = [0.0 for n in range(0, 2*BLOCKSIZE)] # Write output to audio stream stream.write(output_string) output_all = output_all + output_string print('* Done') stream.stop_stream() stream.close() p.terminate()
def my_callback_fun(input_string, block_size, time_info, status): input_tuple = struct.unpack('h', input_string) output_sample = clip16(GAIN * input_tuple[0]) output_list.append(output_sample) return (input_string, pyaudio.paContinue)
# Loop through blocks for i in range(NumBlocks): # Do difference equation for block for n in range(BLOCKLEN): rand_val = random.random() if rand_val < THRESHOLD: x = 15000 else: x = 0 y[n] = b0 * x - a1 * y[n - 1] - a2 * y[n - 2] # What happens when n = 0? # In Python negative indices cycle to end, so this works! y[n] = int(clip16(y[n])) # Convert numeric list to binary data output_bytes = struct.pack('h' * BLOCKLEN, *y) # Write binary data to audio output stream stream.write(output_bytes, BLOCKLEN) print('* Finished') stream.stop_stream() stream.close() p.terminate()
# Convert string to number input_value = struct.unpack('h', input_string)[0] # Compute output value output_value = Gdp * input_value + Gff * buffer[k] # Update buffer buffer[k] = input_value # Increment buffer index k = k + 1 if k >= BUFFER_LEN: # The index has reached the end of the buffer. Circle the index back to the front. k = 0 # Convert output value to binary string output_string = struct.pack('h', int(clip16(output_value))) # Write output value to audio stream stream.write(output_string) # Get next frame (sample) input_string = wf.readframes(1) print("* Finished *") stream.stop_stream() stream.close() p.terminate()
# Get previous and next buffer values (since kr is fractional) kr_prev = int(math.floor(kr)) kr_next = (kr_prev + 1) % BLOCKSIZE frac = kr - kr_prev # 0 <= frac < 1 # if kr_next >= BLOCKSIZE: # kr_next = kr_next - BLOCKSIZE # Compute output value using interpolation prev = (1-frac) * buffer[kr_prev] nextt = frac * buffer[kr_next] # output_block[n] = prev + nextt output_block[2*n] = (prev + nextt) * gain + input_value[n] output_block[2*n+1] = (prev + nextt) * gain + input_value[n] output_block[2*n] = clip16(output_block[2*n]) output_block[2*n+1] = clip16(output_block[2*n+1]) # Update buffer (pure delay) buffer[kw] = input_value[n] # Increment read index kr = kr + 1 + W * math.sin( 2 * math.pi * f0 * n / RATE + theta) # Note: kr is fractional (not integer!) # Ensure that 0 <= kr < BLOCKSIZE if kr >= BLOCKSIZE: # End of buffer. Circle back to front. kr = 0 # Increment write index
for i in range(0, NumBlocks): # Do difference equation for block for n in range(BLOCKSIZE): rand_val = random.random() if rand_val < THRESHOLD: x = 15000 else: x = 0 y[n] = b0 * x - a1 * y[n-1] - a2 * y[n-2] # What happens when n = 0? # In Python negative indices cycle to end, so it works.. y[n] = clip16(y[n]) # If numpy is available, then clipping can be done using: # y = np.clip(y, -32000, 32000) # Convert numeric list to binary string data = struct.pack('h' * BLOCKSIZE, *y); # Write binary string to audio output stream stream.write(data, BLOCKSIZE) print 'Done.' stream.stop_stream() stream.close() p.terminate()
if rand_val_r < THRESHOLD: x_r = 15000 else: x_r = 0 rand_val_l = random.random() if rand_val_l < THRESHOLD: x_l = 15000 else: x_l = 0 y[2*n] = b0 * x_r - a1 * y[2*(n-1)] - a2 * y[2*(n-2)] # What happens when n = 0? # In Python negative indices cycle to end, so it works.. y[2*n] = clip16(y[2*n]) y[2*n+1] = b0 * x_l - a1 * y[2*(n-1)+1] - a2 * y[2*(n-2)+1] # What happens when n = 0? # In Python negative indices cycle to end, so it works.. y[2*n+1] = clip16(y[2*n+1]) # If numpy is available, then clipping can be done using: # y = np.clip(y, -32000, 32000) # Convert numeric list to binary string data = struct.pack('hh' * BLOCKSIZE, *y); # Write binary string to audio output stream stream.write(data, BLOCKSIZE)
# Do difference equation for block for n in range(BLOCKSIZE): rand_val = random.random() if rand_val < 0.0005: x = 5000 * (random.random() - 0.5) else: x = 0 # Easy way: define two vectors y1 and y2 y1[n] = b10 * x - a11 * y1[n-1] - a12 * y1[n-2] y2[n] = b20 * y1[n] - a21 * y2[n-1] - a22 * y2[n-2] for n in range(BLOCKSIZE): output[n] = int( clip16( y2[n] )) # Convert numeric list to binary string data = struct.pack('h' * BLOCKSIZE, *output); line.set_ydata(output) plt.pause(0.001) plt.draw() # Convert numeric list to binary string data = struct.pack('h' * BLOCKSIZE, *output); # Write binary string to audio output stream stream.write(data, BLOCKSIZE) print('* Finished.')
def callback_record(input_string, block_size, time_info, status): data = struct.unpack("h", input_string) sample = clip16(GAIN * data[0]) output_data.append(sample) return (None, pyaudio.paContinue)
# Go through block for n in range(0, BLOCKSIZE): # Amplitude modulation (f0 Hz cosine) # output_block[n] = input_tuple[n] * math.cos(2*math.pi*n*f0/RATE + theta) # output_block[n] = input_tuple[n] * 1.0 # for no processing # Difference equation X[6] = input_tuple[n] Yvalue = a[1] * Y[5] + a[2] * Y[4] + a[3] * Y[3] + a[4] * Y[2] + a[ 5] * Y[1] + a[6] * Y[0] Y[6] = b[0] * X[6] + b[1] * X[5] + b[2] * X[4] + b[3] * X[3] + b[ 4] * X[2] + b[5] * X[1] + b[6] * X[0] - Yvalue # unclipped_value = np.real(Y[7] * cmath.exp(I * 2 *math.pi*n*f0/RATE)) # output_block[n] = clip16(unclipped_value) output_block[n] = clip16(Y[6]) Y = shift(Y, 1) X = shift(X, 1) # Set angle for next block # theta = theta + theta_del # line.set_ydata(output_block) # Update y-data of plot # plt.draw() # Convert values to binary string output_string = struct.pack('h' * BLOCKSIZE, *output_block) # Write binary string to audio output stream stream.write(output_string) # append new to total
def wah_effect(): RECORD_SECONDS = 500 BLOCKSIZE = 1024 # Number of frames per block gain = 1 fc_max = 1200 # initial p = pyaudio.PyAudio() WIDTH = 2 # bytes per sample RATE = 44100 # Sampling rate (samples/second) stream = p.open(format = p.get_format_from_width(WIDTH), channels = 2, rate = RATE, input = True, output = True) # Create a buffer (delay line) for past values # Create block (initialize to zero) output_block = [0.0 for n in range(0, 2*BLOCKSIZE)] output_block_filt = [0.0 for n in range(0, 2*BLOCKSIZE)] bandpass = [0.0 for n in range(0, 2*BLOCKSIZE)] mix = [0.0 for n in range(0, 2*BLOCKSIZE)] # Create a buffer (delay line) for past values buffer = [0.0 for i in range(BLOCKSIZE)] # Initialize to zero buffer2 = [0.0 for i in range(BLOCKSIZE)] # Initialize to zero # Buffer (delay line) indices kr = 0 # read index kw = int(0.5 * BLOCKSIZE) # write index (initialize to middle of buffer) kw = BLOCKSIZE/2 output_all = '' # output signal in all (string) fc_bandpass = 0.0 # Initialize angle theta = 0.0 num_blocks = int(RATE / BLOCKSIZE * RECORD_SECONDS) print ('**** Playing Auto-Wah ****') # Loop through wave file for i in range(0, num_blocks): if app.MODE != "WahWah": break # Block-to-block angle increment <----reduce calculation theta_del = (float(BLOCKSIZE*parameters.wahwah_f_lfo)/RATE - math.floor(BLOCKSIZE*parameters.wahwah_f_lfo/RATE)) * 2.0 * math.pi # Get sample from wave file input_string = stream.read(BLOCKSIZE) # Convert string to number input_value = struct.unpack('hh'* BLOCKSIZE, input_string) bandpass = butter_bandpass_filter(input_value,parameters.wahwah_fc_min,fc_max,RATE,order = 1) # Go through block for n in range(0, BLOCKSIZE): # Amplitude modulation (f0 Hz cosine) # Get previous and next buffer values (since kr is fractional) kr_prev = int(math.floor(kr)) kr_next = kr_prev + 1 frac = kr - kr_prev # 0 <= frac < 1 #print frac if kr_next >= BLOCKSIZE: kr_next = kr_next - BLOCKSIZE # Compute output value using interpolation k = buffer[kr_prev] * (1-frac) r = buffer[kr_next] * frac k2 = buffer2[kr_prev] * (1-frac) r2 = buffer2[kr_next] * frac output_block[2*n] = k + r + k2 +r2 output_block[2*n] = clip16(output_block[2*n]) output_block[2*n+1] = output_block[2*n] # buffer buffer[kw] = input_value[2*n] buffer2[kw] = bandpass[2*n] # Increment read index kr = kr + 1 + parameters.wahwah_w * math.sin( 2 * math.pi * parameters.wahwah_f_lfo * n / RATE + theta) # Note: kr is fractional (not integer!) # Ensure that 0 <= kr < buffer_MAX if kr >= BLOCKSIZE: # End of buffer. Circle back to front. kr = 0 # Increment write index kw = kw + 1 if kw == BLOCKSIZE: # End of buffer. Circle back to front. kw = 0 fc_bandpass = parameters.wahwah_fc_min + 0.5 * parameters.wahwah_w * (1 + math.sin(2 * math.pi * parameters.wahwah_f_lfo * n /RATE )) f_max = 2 * fc_bandpass + parameters.wahwah_fc_min theta = theta + theta_del output_string = struct.pack('hh'* BLOCKSIZE , *output_block) # Write output to audio stream stream.write(output_string) output_all = output_all + output_string print('* Done') stream.stop_stream() stream.close() p.terminate()