Tsubasa
Tsubasa

Reputation: 1429

How to play the sound in background while the text is being dynamically displayed in python?

I want to play the beep sound continuously till the text is being displayed dynamically on the terminal. Here goes my code.

import time
import sys
import winsound


winsound.Beep(1000, 5000)

def printf(s):

    for c in s:
        sys.stdout.write('%s' % c)
        sys.stdout.flush()
        time.sleep(0.0203)

printf("Hello I am Jishan Bhattacharya.")

Upvotes: 0

Views: 1167

Answers (2)

Isdj
Isdj

Reputation: 1856

You can create a new thread and start running it, create a bool to stop thread though this will cause little pauses.

There is no apparent way to use beep and make it stop dynamically.

import threading

class beeper(threading.Thread):
    def run(self):
        self.keeprunning = True
        while self.keeprunning:
            winsound.Beep(freq, dur) // make a short dur to make sure it stops soon after printing ends
beep =  beeper()
beep.start()
printf(string)
beep.keeprunning = False

Upvotes: 0

Stavros Avramidis
Stavros Avramidis

Reputation: 865

You will need to do something like this, but my testing showed my that you cann't use it with Beep.You could save the sound as a wav file,instead.

winsound.PlaySound(sound, winsound.SND_ASYNC)

Upvotes: 1

Related Questions