Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pyaudio
- import numpy as np
- import pygame
- import multiprocessing as mp
- CHUNK = 8192
- FORMAT = pyaudio.paInt16
- CHANNELS = 1
- RATE = 48000
- NP_FORMAT = np.int16
- DELAY_LENGTH = 8
- def delay_modification(data, buffer, index):
- start_index = index
- buffer_len = len(buffer)
- was_filled = buffer[buffer_len-1] != 0
- if was_filled:
- for i in range(len(data) - 1, -1, -1):
- start_index -= 1
- if start_index < 0:
- start_index = buffer_len - 1
- data[i] = data[i] // 2 + buffer[start_index] // 2
- for i in range(len(data)):
- if index == buffer_len:
- index = 0
- was_filled = True
- buffer[index] = data[i]
- index += 1
- return index
- def feed_queue(q):
- buffer = np.zeros(CHUNK * DELAY_LENGTH)
- index = 0
- p = pyaudio.PyAudio()
- stream = p.open(format=FORMAT,
- channels=CHANNELS,
- rate=RATE,
- input=True,
- output=False,
- frames_per_buffer=CHUNK)
- while True:
- data = stream.read(CHUNK, exception_on_overflow=False)
- data = np.fromstring(data, dtype=NP_FORMAT)
- index = delay_modification(data, buffer, index)
- if q.full():
- q.get_nowait()
- q.put(data)
- def main():
- np.set_printoptions(threshold=np.nan)
- queue = mp.Queue(maxsize=RATE // CHUNK)
- p = mp.Process(target=feed_queue, args=(queue,))
- p.start()
- pygame.mixer.init()
- S = pygame.mixer.Sound
- while True:
- d = queue.get()
- S(d).play()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement