[Tutor] Regarding inability of Python Module Winsound to produce beep in decimal frequency

Dennis Lee Bieber wlfraed at ix.netcom.com
Fri Aug 13 16:20:02 EDT 2021


On Fri, 13 Aug 2021 16:23:52 +0530, Umang Goswami <go.umang108 at gmail.com>
declaimed the following:


>I am Umang Goswami, a Python developer and student working on a huge
>project for automation of music instruments. I am producing the musical
>notes using the Beep function of Winsound Module(
>https://docs.python.org/3/library/winsound.html) by passing frequency as a
>argument to the function.
>
		"Automation" of musical instruments already exists for MIDI
compatible equipment (keyboard-synthesizers and sound cards). And
pitch-riders have existed for things like flutes and other winds (which
produce only one note at a time). Strings are more difficult, unless played
one note only.

	The Winsound module was never meant to be "pure" for music. It's more
of an alarm feature invoked by applications to notify the use of events
they may need to investigate. As documented:
"""
The winsound module provides access to the basic sound-playing machinery
provided by Windows platforms. It includes functions and several constants.

winsound.Beep(frequency, duration)

    Beep the PC’s speaker. The frequency parameter specifies frequency, in
hertz, of the sound, and must be in the range 37 through 32,767. The
duration parameter specifies the number of milliseconds the sound should
last. If the system is not able to beep the speaker, RuntimeError is
raised.
"""

	If you want /music/ you should be looking at MIDI control of the
computer's sound card, rather than a software (CPU intensive?) generation
of a square wave. Just one library
https://mido.readthedocs.io/en/latest/intro.html

	Of course, MIDI notes are not specified by frequency, but by "note"
(0-127 I believe is the range). There /are/ "pitch-bend" controllers that
allow one to blur the core note to sharp or flat, but nothing precise in
terms of raw frequency. 


>Now whenever i provide frequency of any note in decimal(for example
>277.1826 for C4 note) it shows following error:
>Traceback (most recent call last):
>  File "C:\Users\Umang Goswami\Desktop\Umang  Goswami\test.py", line 2, in
><module>
>    winsound.Beep(111.11,111111)
>TypeError: integer argument expected, got float
>
>Now I have  to round up the frequencies. This is hurting the quality,

	I wouldn't "round UP" the frequencies... Round to nearest integer would
be better (round nearest: 277 vs round up: 278).


	You might want to peruse https://wiki.python.org/moin/PythonInMusic as
it lists more packages than I want to explore. There are plenty for
playing/recording WAV data, plenty of MIDI, etc.


-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
	wlfraed at ix.netcom.com    http://wlfraed.microdiversity.freeddns.org/



More information about the Tutor mailing list