10

So I am trying to play a simple beep after my code has finished because it takes a while to crunch a lot of data. I have tried other examples on the web, but nothing seems to work. I have tried:

import sys sys.stdout.write('\a') sys.stdout.flush() 

which outputs the word bel in the IDE shell, but nothing else

I have also tried:

import os print('\a') 

which outputs the same exact thing.

And I have tried

import os os.system('play --no-show-progress --null --channels 1 synth %s sine %f' % ( 4, 4)) 

which simply outputs:

sh: play: command not found 

Anybody have any ideas?

Also here is a jpg containing the word bell. It looks funny... Image of 'Bel'

I also made sure my volume was up

UPDATE: It might just be my IDE. I tried launching python directly from terminal and using write('\a') and it worked. It just won't work in wing 101

2
  • 1
    sys.stdout.write('\a') works for me, at least on macOS Sierra 10.12.3 Commented Feb 10, 2017 at 2:13
  • on Mac 14.2 sys.stdout.write('\a') works in terminal - not vscode - with a "tok" and outputs 1. print('\a') also works and no output. Commented Dec 20, 2023 at 9:55

3 Answers 3

37

Acutually,sys.stdout.write('\a') works for me,but not in IDE,try to run this code in Terminal.You will hear the system sound.

Also you can try this two command:

  • say

  • afplay


e.g.

import os os.system('say "Beer time."') import os os.system('afplay /System/Library/Sounds/Sosumi.aiff') 

Run man say to see more details.

say Hello, World.
say -v Alex -o hi -f hello_world.txt.
say --interactive=/green spending each day the color of the leaves.
say -o hi.aac 'Hello, [[slnc 200]] World'.
say -o hi.m4a --data-format=alac Hello, World.

Hope this helps.

Sign up to request clarification or add additional context in comments.

3 Comments

this works. By chance though do you know how to make a generic bell or whistle and not human speech?
Asweome. How can we make the text a variable? Say txt = "Beer time." and os.system('say txt')
@chikitin os.system(f"say {txt}")
8

In Mac, you can:

# List of voices: say -v ? import os def say(msg = "Finish", voice = "Victoria"): os.system(f'say -v {voice} {msg}') 

Usage:

# Run your program/experiment... say() # say finish at the end # Or say something more exciting say("Great! Finally finished, please check the result!") # Or switch to another voice say("Great!", "Alex") 

You can also find other human language examples in my GitHub repo:https://github.com/hkpeterpeter/finish_alert

Comments

0

Install pygame, https://www.pygame.org/.

Then go and have a look at the answers here: Simple Pygame Audio at a Frequency

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.