Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
442 views
in Technique[技术] by (71.8m points)

python - Exception has occurred: error mpg123_seek: Invalid RVA mode. (code 12)

I am recording audio using sounddevice and I want to play it through a virtual audio cable through pygame, I keep receiving this error Exception has occurred: error mpg123_seek: Invalid RVA mode. (code 12)

My code is below:

import sounddevice as sd
from scipy.io.wavfile import write
import random
import pygame
import time

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
        write(f'output/output{str(number)}.mp3', fs, myrecording)  # Save as WAV file `

        # PLAY MIC SOUND HERE
        pygame.mixer.music.load(f'output/output{str(number)}.mp3') #Load the mp3  
        pygame.mixer.music.play() #Play it
        time.sleep(00.1)

main()

Any help is appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...