Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a sound in OSX using python 3

Tags:

python

macos

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

like image 756
Abid Rizvi Avatar asked Feb 10 '17 02:02

Abid Rizvi


1 Answers

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.

like image 186
McGrady Avatar answered Sep 29 '22 03:09

McGrady