Skip to main content

OSWavePlayer

This is a simple wave player that uses the command line to issue instructions to the OS to play .wav files.

I wanted to write the simplest cross-platform wave player, that can do the basics and would not have any sort of dependencies. It simply issues subprocess calls to the operating system to play the sound, and as long as the operating system has not been significantly altered (you have not removed the operating system's wave playing applications) it should work. The player can issue commands to stop the subprocesses to stop playing the sounds.

Note:

(This module uses a simple process, and is expected to be dependable, but can be very slow in Windows. If you have time senstive sounds as in a fast-paced video game, for example, I would strongly recommend using the "preferredwaveplayer" module. This can be installed by using the commmand: pip install preferredwaveplayer then the import statement from preferredwaveplayer import *. Then all the same functions will be available below. This is a much faster wave player than the oswaveplayer in Windows.)

-Windows10 uses the Media.Soundplayer module built into Windows 10

-Linux uses ALSA which is part of the Linux kernel since version 2.6 and later

-MacOS uses the afplay module which is present OS X 10.5 and later`

To use the module simply add:

from oswaveplayer import *

(or from preferredwaveplayer import * if you are opting for the faster waveplayer.) and this will import all its functions.

The module essentially contains 6 functions: playwave, stopwave, getIsPlaying, loopwave, stoploop, and getIsLoopPlaying.

yourSound = playwave("goodvibes.wav")   #starts sound, returns a reference to it
stopwave(yourSound)                     #stops your sound
getIsPlaying(yourSound)                 #used to see if your process is playing
playwave("goodvibes.wav")               #can use to playsong if no stop is needed
yourBackgroundSong = loopwave("bgsong.wav")      #starts sound loop
stoploop(yourBackgroundSong)                     #stops sound loop
getIsLoopPlaying(yourBackgroundSong)             #used to see if loop is playing

Here are some examples on how to use them. Note that with 'playwave' can be used as a standalone function, but if you want to stop the file from playing, you will have to use the return value of playwave. Read a little further and the examples should be obvious.

Examples:

To play a wave file:

playwave("coolsong.wav") #-> this plays the wav file

mysong=playwave("coolsong.wav") #-> this plays the wav file and also returns the song subprocess

To stop your song:

stopwave(mysong) # -> this stops the subprocess, mysong, which you created in the line above

To find out if your wave file is playing:

isitplaying = getIsPlaying(mysong) -> sets a variable to True or False, depending on if process is running

print(getIsPlaying(mysong)) -> prints True or False depending on if process is running

if getIsPlaying(mysong)==True:
    print("Yes, your song is playing")
else:
    print("Your song is not playing")

To play a wave file synchronously (block execution of program until sound has finished playing):

playwave("coolsong.wav",1) #-> this plays the wav file synchronously
or
playwave("coolsong.wav",block=True)

To play a wave file in a continous loop:

myloop=loopwave("mybackgroundsong.wav")

This starts a background loop playing, but also returns a reference to the background process so it can be stopped.

To stop the continuous loop from playing:

stoploop(myloop)

To see if the loop is playing:

isLooping = getIsLoopPlaying(myloop)   #-> True if sound is looping, otherwise False
print(getIsLoopPlaying(myloop))        #-> prints True if loop is playing 

Notes about using this module as a replacement in the playsound module:

Additionally, I included an alias to the function named 'playsound', and if used, the default block will be true, or synchronous play. This way, the module can be used in place of the playsound module (https://github.com/TaylorSMarks/playsound/blob/master/playsound.py) with the same syntax. If the playsound module does not work for you, or if it is no longer maintained, you can load this module and use the import statement below for .wav files only. Just for semantics I inlcluded a 'stopsound' alias to the function stopwave.

Use:

from oswaveplayer import playsound

for backwards compatibility with the playsound module - .wav files only.

Release files for oswaveplayer 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for oswaveplayer 0.3.0
File Size Uploaded
oswaveplayer-0.3.0.tar.gz 5.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for oswaveplayer 0.3.0
File Interpreter ABI Platform
oswaveplayer-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 10.9 kB

Release files / oswaveplayer-0.3.0.tar.gz

Download URL oswaveplayer-0.3.0.tar.gz
Size 5.2 kB
Tags Source
SHA-256 checksum
How to use checksums
534f18e9807cf8d2c4a3cd8e05ec4c3a1a3cd1bd372d6b76271c32defd8beb74
BLAKE2b-256 checksum
How to use checksums
748a2b4669e1f6c985f17dcd85656e3a7f39e9f581a15caed7b2e6ad275fd662
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.8.10

Release files / oswaveplayer-0.3.0-py3-none-any.whl

Download URL oswaveplayer-0.3.0-py3-none-any.whl
Size 5.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5ea2f0b79841a125de85ebdf13c3063b857ee0ce107bd86a4cbd26415fa61658
BLAKE2b-256 checksum
How to use checksums
7a78af3ebd0102f9af95909b312bcff6496260efbaa7145c11298d7fb2e548c7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.8.10

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

0.2.9

2 release files

0.2.8

2 release files

0.2.7

2 release files

0.2.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page