There's a couple of issues.
The first is that scipi.io.wavefile.write()
only writes an uncompressed WAV file (ref: https://docs.scipy.org/doc/scipy/reference/generated/scipy.io.wavfile.write.html ). You might name it .mp3
, but it's not compressed that way.
The next issue is that pygame.mixer.music
will not .load()
uncompressed WAV files. So... what to do...
One work-around is to use the base pygame.mixer
, which is happy to load uncompressed WAV. And while I don't have an 'CABLE Input (VB-Audio Virtual Cable)' device, I do get a nice file of silence, which I validated with the sound-editing program Audacity, and this seems to play OK.
import sounddevice as sd
from scipy.io.wavfile import write
import pygame
import time
import random
pygame.init()
pygame.mixer.init(devicename='CABLE Input (VB-Audio Virtual Cable)')
fs = 44100 # Sample rate
seconds = 00.1 # Duration of recording
def main():
for x in range(10000):
number = random.randint(1,9999999)
myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=2)
sd.wait() # Wait until recording is finished
filename = f'output/output{str(number)}.wav'
write(filename, fs, myrecording) # Save as uncompressed WAV file
# PLAY MIC SOUND HERE
print( "Playing [" + filename + "]" )
#pygame.mixer.music.load(filename) #Load the wav
#pygame.mixer.music.play() #Play it
#while ( pygame.mixer.music.get_busy() ): # wait for the sound to end
# time.sleep(00.1)
sound = pygame.mixer.Sound(filename) #Load the wav
sound.play() #Play it
while ( pygame.mixer.get_busy() ): # wait for the sound to end
time.sleep(00.1)
main()
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…