2017-04-07 3 views
0

古い学校やインディーズのビデオゲームのように、遅れてテキストを印刷するコードがあります。Python 3.6:オーディオをより早く再生する方法

すべてが機能していますが、私が望むやり方ではありません。 今は音が遅すぎるように、サウンドの印刷と再生をより速くします。

これを可能にする方法はありますか?ここで

私のコードは次のとおりです。

注:これはpycharmに遅れたが、端末/ CMDで正常に動作します。

import sys 
import time 
from pydub import AudioSegment 
from pydub.playback import play 



def print_delay(string_in): 
    sound_1 = "text_beep.wav" 
    sound_play = AudioSegment.from_wav(sound_1) 


    for char in string_in: 
     sys.stdout.write(char) 
     sys.stdout.flush() 
     play(sound_play) 

     if char != ",": 
      time.sleep(0.01) 
     if char == ".": 
      time.sleep(0.20) 
     if char == ",": 
      time.sleep(0.10) 

    print("\n") 

string_hello = "Hello World, this is a sample text.\nI want want this to print out faster without being delayed by the sound file.\nIs there any faster way to do this?" 

print_delay(string_hello) 

答えて

0

Woohoo! 私はそれを理解しました。

という名前のモジュールを使用し:あなたのサウンドファイルが短い音であることを指定することができます「pyglet」

イム100%確実ではないが、それはpygletのように見えますが。これが事実なら、引数 "streaming = False"を渡すことができます。この引数は、基本的にはサウンドをすぐに再生するように指示し、順番にCPUパワーを減らします。私はこれがサウンドファイルを私が望むやり方で演奏させているのかどうかは分かりませんが、それはそうかもしれません。

誰かが確実に知っている場合は、私に知らせてください。ここで

は私のソースです:https://pythonhosted.org/pyglet/programming_guide/playing_sounds_and_music.html

import sys 
import time 
import pyglet 



def print_delay(string_in): 
    sound_1 = pyglet.resource.media("text_beep.wav", streaming=False) 


    for char in string_in: 
     sound_1.play() 
     sys.stdout.write(char) 
     sys.stdout.flush() 

     if char != ",": 
      time.sleep(0.05) 
     if char == ".": 
      time.sleep(0.70) 
     if char == ",": 
      time.sleep(0.50) 


    print("\n") 

string_hello = "Hello World, this is a sample text.\nI want want this to print out faster without being delayed by the sound file.\nIs there any faster way to do this?" 

print_delay(string_hello) 
関連する問題