0

I'm generating a text-to-speech mp3 file using gTTS module. Then I store it on a buffer using BytesIO().

Here, mp3 = BytesIO(). When I try to set a file location using the .seek() function like mp3.seek(0) where 0 signifies the start of the buffer, it gives the following error -

ValueError: invalid whence (-1, should be 0, 1 or 2)
Traceback (most recent call last):
  File "C:/Users/%SOME_PATH%/test_audio.py", line 17, in <module>
    sayText("Hello World")
  File "C:/Users/%SOME_PATH%/test_audio.py", line 14, in sayText
    mixer.music.load(mp3)
pygame.error: ModPlug_Load failed

I don't know why it is calling it an invalid whence. The valid ones are 0, 1, 2, and I'm passing the value 0 and I don't see the point in changing it something else other than 0.

Here's the full code -

from gtts import gTTS
from io import BytesIO
from pygame import mixer

def sayText(tosaytext):
    # Use gTTS to Store Speech on Buffer
    tts = gTTS(text=tosaytext, lang='en-uk')
    mp3 = BytesIO()
    tts.write_to_fp(mp3)
    mp3.seek(0)
    
    # Play from Buffer
    mixer.init()
    mixer.music.load(mp3)
    mixer.music.play()

sayText("Hello World")

Can anyone point out where I'm going wrong with any solution to it? Thank You!

NotMyName
  • 85
  • 1
  • 9
  • You could start your debugger and go into `mixer.music.load` to find out the problem. – Thomas Sablik Nov 12 '20 at 13:33
  • It appears as if the problem occurs inside `mixer.music.load`, not in your `seek` call. The top `ValueError` line might be getting printed out by `mixer.music.load` as a diagnostic. – Karl Knechtel Nov 12 '20 at 13:35
  • @KarlKnechtel Yes, I went into debugger and confirmed the error is being thrown by `mixer.music.load`. I'm not too sure why it is throwing an error. – NotMyName Nov 12 '20 at 13:52

0 Answers0