파이썬 winsound Beep - paisseon winsound Beep

 

모니터링을 할 경우 어떤한 조건이 Fail 발생할 때, 비프음을 발생시켜 간단하게 모니터링을 할 수 있다.

 

DOCS : https://docs.python.org/3/library/winsound.html?highlight=winsound#module-winsound

 

import winsound as ws

def beepsound():
    freq = 2000    # range : 37 ~ 32767
    dur = 1000     # ms
    ws.Beep(freq, dur) # winsound.Beep(frequency, duration)

print(beepsound())

 

공유하기

게시글 관리

구독하기System Admin

저작자표시 비영리 변경금지

  • 카카오스토리
  • 트위터
  • 페이스북

'Python > Python for Windows' 카테고리의 다른 글

[Python] win32evtlog 모듈을 이용한 윈도우 이벤트 로그 추출  (0)2018.05.20[Python] 리스트의 index를 활용하여 문자열 분리  (0)2018.05.13[Python] subprocess 모듈과 DOS ping 명령어를 이용한 핼스 체크  (0)2018.05.09[Python] sys모듈로 입력인수 처리하기  (0)2018.05.07[Python] Check shared folder of Windows operating system using subprocess and regular expression  (0)2018.05.04[Python] SQLite 메모리DB를 활용한 파이썬 IIS 로그파서  (0)2018.04.27[Python] xcopy DOS명령어를 조합한 증분 백업  (0)2018.04.20[Python] 윈도우 서버에서 SMTP서버로 메일 발송 (2)  (0)2018.04.18

PYTHON, sound 모듈, winsound module, 알람, 윈도우 비프음, 파이썬, 파이썬 beep, 파이썬 비프음, 파이썬 소리

, 댓글 0개가 달렸습니다

댓글을 달아 주세요

비밀글

Maybe you have just begun coding in Python. Maybe the entire outset seems inconclusive since you have been working with regular IDEs and would like to obtain a bit more out of what your code does on the respective console and maybe a little music or tune might just liven it up. This article serves the purpose of introducing to you the winsound module, an object or file with a set of attributes or functions, specific to the task of generating or playing sound or a sound file. Note: The winsound module is defined only for execution on a Windows Platform, hence the name WINsound. Since, the winsound module is a builtin, there is no need for you to install it prior to executing it. The basic action would be to

import winsound

and then based upon the kind of output you would like, type out the following functions:

  • winsound.Beep( ) The functionality devoted to this method is to generate a ‘Beep’ sound. However, the user is required to input the frequency value and the duration of the sound (these are parameters that shall be passed while calling the function). Note: The frequency must be in the range 37 through 32,767 hertz. 

Python3




import winsound

 

# frequency is set to 500Hz

freq= 500

 

# duration is set to 100 milliseconds            

The respective audio file named 'Welcome.wav' is executed.
0=
The respective audio file named 'Welcome.wav' is executed.
2

The respective audio file named 'Welcome.wav' is executed.

The respective audio file named 'Welcome.wav' is executed.
4

  • Output:

Windows system will produce a ‘Beep’ sound with the given frequency for the given duration of time.

  • Building further on the code above, things can be taken to another level by implementing a ‘for’ loop to increment the frequency and duration. This has been done in the code below: 

Python3




import winsound

 

freq=

The respective audio file named 'Welcome.wav' is executed.
2

The respective audio file named 'Welcome.wav' is executed.
0=
Play Windows question sound
2

 

Play Windows question sound
3

Play Windows question sound
4
Play Windows question sound
5
Play Windows question sound
6
Play Windows question sound
7
Play Windows question sound
8
Play Windows question sound
9import0import1import2

import3import4

import3freqimport7=

The respective audio file named 'Welcome.wav' is executed.
2

import3

The respective audio file named 'Welcome.wav' is executed.
0import7=
Play Windows question sound
2

  • Output: 

Consecutive notes with frequency differences of 100Hz and time duration 50 milliseconds greater
than  the previous time duration are produced.

  • winsound.PlaySound( ) With the PlaySound function, things can get slightly advanced, not to mention interesting. Keep in mind that this function is only compatible with .wav files. Two parameters are passed in the function: ‘filename’ and the flag – winsound.SND_FILENAME, which is required for the Platform API to refer to the output file. The flags are as defined below:
FlagsDescriptionSND_FILENAMEThe sound parameter is the name of a WAV file.SND_LOOPPlay the sound repeatedlySND_MEMORYThe sound parameter to PlaySound() is a memory image of a WAV file, as a bytes-like object.SND_ASYNCReturn immediately, allowing sounds to play asynchronously.SND_NODEFAULTIf the specified sound cannot be found, do not play the system default sound.SND_NOSTOPDo not interrupt sounds currently playing.
  • Example: 

Python3




import winsound

 

winsound7winsound8winsound9 # frequency is set to 500Hz0# frequency is set to 500Hz1

 

# frequency is set to 500Hz2

# frequency is set to 500Hz3# frequency is set to 500Hz0# frequency is set to 500Hz5

  • Output:
The respective audio file named 'Welcome.wav' is executed.
  • SND_ALIAS The sound parameter should be interpreted as a control panel sound association name. The Windows registry keys are associated with sound names. If the registry contains no such name, play the system default sound unless SND_NODEFAULT. All Win32 systems support the following:
PlaySound() nameControl panel sound nameSystemAsteriskAsteriskSystemExclamationExclamationSystemExitExit WindowsSystemHandCritical StopSystemQuestionQuestion
  • Example: 

Python3




import winsound

 

 

# frequency is set to 500Hz8

# frequency is set to 500Hz9

  • Output:
Play Windows question sound

There are various other winsound functions, most of which are particular to specific tasks, some of which deal with runtime parameters. Nonetheless, the functions mentioned above should suffice as long as the idea is to play around to get an idea of what can be accomplished using this module.