How To Fix Laggy/Static-y sound with PyAudio /u/DaRealNoteed Python Education

I found a cool “binary music” thing on the internet where each binary number, if it turns from a 0 to a 1, plays a different note on the piano for 1 second. I tried making it in python:

import time import threading import pyaudio as pa import numpy as np import sys print("LOADING...") # Constants AMPLITUDE = 0.5 DURATION = 1 SAMPLE_RATE = 44100 note_frequencies = { 0: 329.63, # E4 1: 261.63, # C4 2: 293.66, # D4 3: 349.23, # F4 4: 392.00, # G4 5: 440.00, # A4 6: 493.88, # B4 7: 523.25, # C5 8: 587.33, # D5 9: 659.26, # E5 } # Functions def playsound(frequency): global AMPLITUDE, DURATION, SAMPLE_RATE t = np.linspace(0, DURATION, int(SAMPLE_RATE * DURATION), False) note = np.sin(frequency * t * 2 * np.pi) audio = note * AMPLITUDE p = pa.PyAudio() stream = p.open(format=pa.paFloat32, channels=1, rate=SAMPLE_RATE, output=True) def play_audio(): stream.write(audio.astype(np.float32).tobytes()) stream.stop_stream() stream.close() p.terminate() threading.Thread(target=play_audio).start() # Get Song Length song_length = len(note_frequencies) prev_bc = [0] * song_length # Loop try: while True: for counter in range(2**song_length): bc = list(int(digit) for digit in bin(counter)[2:].zfill(song_length)) for j in range(song_length): if bc[j] != prev_bc[j] and bc[j] == 1: playsound(note_frequencies[j]) print('r' + ''.join(str(num) for num in bc), end='', flush=True) prev_bc = bc time.sleep(.2) except KeyboardInterrupt: print('r' + "Stopping...",end='', flush=True) time.sleep(2) print('r' + "Stopped",end='', flush=True) sys.exit(0) 

Because I don’t rly get audio libraries I had an AI make most of the audio playback code. The audio does play but it is filled with static randomly in between and sounds HORRIBLE. I tried the SoundDevice module first but I wanted the notes to overlap and it wasn’t good with overlapping sounds so I stuck with the PyAudio module. Is this problem just a PyAudio disadvantage, and if so is there a better module where I can still have overlapping sounds playing? If not, how can I fix my problem? (sorry for my bad English)

submitted by /u/DaRealNoteed
[link] [comments]

​r/learnpython I found a cool “binary music” thing on the internet where each binary number, if it turns from a 0 to a 1, plays a different note on the piano for 1 second. I tried making it in python: import time import threading import pyaudio as pa import numpy as np import sys print(“LOADING…”) # Constants AMPLITUDE = 0.5 DURATION = 1 SAMPLE_RATE = 44100 note_frequencies = { 0: 329.63, # E4 1: 261.63, # C4 2: 293.66, # D4 3: 349.23, # F4 4: 392.00, # G4 5: 440.00, # A4 6: 493.88, # B4 7: 523.25, # C5 8: 587.33, # D5 9: 659.26, # E5 } # Functions def playsound(frequency): global AMPLITUDE, DURATION, SAMPLE_RATE t = np.linspace(0, DURATION, int(SAMPLE_RATE * DURATION), False) note = np.sin(frequency * t * 2 * np.pi) audio = note * AMPLITUDE p = pa.PyAudio() stream = p.open(format=pa.paFloat32, channels=1, rate=SAMPLE_RATE, output=True) def play_audio(): stream.write(audio.astype(np.float32).tobytes()) stream.stop_stream() stream.close() p.terminate() threading.Thread(target=play_audio).start() # Get Song Length song_length = len(note_frequencies) prev_bc = [0] * song_length # Loop try: while True: for counter in range(2**song_length): bc = list(int(digit) for digit in bin(counter)[2:].zfill(song_length)) for j in range(song_length): if bc[j] != prev_bc[j] and bc[j] == 1: playsound(note_frequencies[j]) print(‘r’ + ”.join(str(num) for num in bc), end=”, flush=True) prev_bc = bc time.sleep(.2) except KeyboardInterrupt: print(‘r’ + “Stopping…”,end=”, flush=True) time.sleep(2) print(‘r’ + “Stopped”,end=”, flush=True) sys.exit(0) Because I don’t rly get audio libraries I had an AI make most of the audio playback code. The audio does play but it is filled with static randomly in between and sounds HORRIBLE. I tried the SoundDevice module first but I wanted the notes to overlap and it wasn’t good with overlapping sounds so I stuck with the PyAudio module. Is this problem just a PyAudio disadvantage, and if so is there a better module where I can still have overlapping sounds playing? If not, how can I fix my problem? (sorry for my bad English) submitted by /u/DaRealNoteed [link] [comments] 

I found a cool “binary music” thing on the internet where each binary number, if it turns from a 0 to a 1, plays a different note on the piano for 1 second. I tried making it in python:

import time import threading import pyaudio as pa import numpy as np import sys print("LOADING...") # Constants AMPLITUDE = 0.5 DURATION = 1 SAMPLE_RATE = 44100 note_frequencies = { 0: 329.63, # E4 1: 261.63, # C4 2: 293.66, # D4 3: 349.23, # F4 4: 392.00, # G4 5: 440.00, # A4 6: 493.88, # B4 7: 523.25, # C5 8: 587.33, # D5 9: 659.26, # E5 } # Functions def playsound(frequency): global AMPLITUDE, DURATION, SAMPLE_RATE t = np.linspace(0, DURATION, int(SAMPLE_RATE * DURATION), False) note = np.sin(frequency * t * 2 * np.pi) audio = note * AMPLITUDE p = pa.PyAudio() stream = p.open(format=pa.paFloat32, channels=1, rate=SAMPLE_RATE, output=True) def play_audio(): stream.write(audio.astype(np.float32).tobytes()) stream.stop_stream() stream.close() p.terminate() threading.Thread(target=play_audio).start() # Get Song Length song_length = len(note_frequencies) prev_bc = [0] * song_length # Loop try: while True: for counter in range(2**song_length): bc = list(int(digit) for digit in bin(counter)[2:].zfill(song_length)) for j in range(song_length): if bc[j] != prev_bc[j] and bc[j] == 1: playsound(note_frequencies[j]) print('r' + ''.join(str(num) for num in bc), end='', flush=True) prev_bc = bc time.sleep(.2) except KeyboardInterrupt: print('r' + "Stopping...",end='', flush=True) time.sleep(2) print('r' + "Stopped",end='', flush=True) sys.exit(0) 

Because I don’t rly get audio libraries I had an AI make most of the audio playback code. The audio does play but it is filled with static randomly in between and sounds HORRIBLE. I tried the SoundDevice module first but I wanted the notes to overlap and it wasn’t good with overlapping sounds so I stuck with the PyAudio module. Is this problem just a PyAudio disadvantage, and if so is there a better module where I can still have overlapping sounds playing? If not, how can I fix my problem? (sorry for my bad English)

submitted by /u/DaRealNoteed
[link] [comments] 

Leave a Reply

Your email address will not be published. Required fields are marked *