News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Beep sounds

Started by felipe, July 30, 2017, 03:29:58 AM

Previous topic - Next topic

felipe

"A long time ago, all PC computers shared a common 8254 programable interval timer chip for the generation of primitive sounds. The Beep function was written specifically to emit a beep on that piece of hardware.

On these older systems, muting and volume controls have no effect on Beep; you would still hear the tone. To silence the tone, you used the following commands:

net stop beep

sc config beep start= disabled

Since then, sound cards have become standard equipment on almost all PC computers. As sound cards became more common, manufacturers began to remove the old timer chip from computers. The chips were also excluded from the design of server computers. The result is that Beep did not work on all computers without the chip. This was okay because most developers had moved on to calling the MessageBeep function that uses whatever is the default sound device instead of the 8254 chip.

Eventually because of the lack of hardware to communicate with, support for Beep was dropped in Windows Vista and Windows XP 64-Bit Edition.

In Windows 7, Beep was rewritten to pass the beep to the default sound device for the session. This is normally the sound card, except when run under Terminal Services, in which case the beep is rendered on the client."


This is from the microsoft site:https://msdn.microsoft.com/es-es/library/windows/desktop/ms679277(v=vs.85).aspx

So it is old, but can be used. So you can try this little game (very simple and useless probably) or watch the code.
Btw, some values (in the permitted range) will not produce audible sound (if any) and others will not produce the sound with the desired lenght (but this is not my fault)  :lol:. Come on! is just a game!  :lol: But you can comment whatever you want (as the forum permits  :redface:). Enjoy  :bgrin: (or not  :()
:lol:


.386
.model      flat,stdcall
option      casemap:none
include     \masm32\include\windows.inc
include     \masm32\include\kernel32.inc
include     \masm32\include\user32.inc
includelib  \masm32\lib\kernel32.lib
includelib  \masm32\lib\user32.lib

.data?
align 4
stdinput    dword   ?
stdoutput   dword   ?
chrswrit    dword   ?
chrsread    dword   ?
freq        dword   ?
milisec     dword   ?
erflag      dword   ?
beepstate   dword   ?
wasoff      dword   ?


.data
align 1
mens00      byte    'Welcome',0ah,0dh
            byte    '=======',0ah,0dh,0ah
            byte    'With this simple program you will be able to do some sounds.',0ah,0dh
            byte    'Just choose the frequency desired and then the',0ah,0dh
            byte    'time lapse for the tone you have just chose.',0ah,0dh           
inpbuff     byte    256 dup(' ')
conname     byte    'BEEP SOUNDS',0
toneprom    byte    0ah,'###############################################################',0d,0ah
            byte    'Enter the tone for the sound you will create.',0ah,0dh
            byte    'This tone must be a number that represents the frequency',0ah,0dh
            byte    'of the tone, using hertz (hz) as the measured unit.',0ah,0dh
            byte    'Allowed values are in the range of 37-32767: '
msgerror    byte    0ah,0dh,'Error, some value was not correct!',0d,0ah
            byte    'Press ENTER key to continue with the program.'
timeprom    byte    0ah,'###############################################################',0d,0ah
            byte    'Enter the time for the sound (the lapse of the sound).',0ah,0dh
            byte    'It should be a number that represents milliseconds (ms) of sound.',0ah,0dh
            byte    'This will be the length of the sound:  '
caption     byte    'CONFIGURATION DETECTED',0
content     byte    'The beep of your computer is off. This program will not work with the beep off.',0ah,0dh
            byte    'You can turn it on and later turn it off again.',0ah,0dh
            byte    'Turn the beep on?',0
msgcap      byte    'PROGRAM INFORMATION',0
msgcont     byte    'This program can not continue.',0ah,0dh
            byte    'You can try it later again.',0
mboxcap     byte    'CONFIRM PLEASE',0
mboxcon     byte    'Do you wish to play again?',0
mbxcon      byte    'Do you wish to turn off the beep again?',0   
           

.code
align 4
start:
            push    SPIF_UPDATEINIFILE
            lea     eax,beepstate
            push    eax
            push    0
            push    SPI_GETBEEP
            call    SystemParametersInfo                    ; Check the beep state.
            cmp     beepstate,TRUE                          ; If is the beep on the program starts.
            je      consolego
            push    MB_YESNO or MB_ICONINFORMATION
            lea     eax,caption
            push    eax
            lea     eax,content
            push    eax
            push    NULL
            call    MessageBox                              ; We ask the user if we should turn on the beep.
            cmp     eax,IDYES
            je      turnon
            push    MB_OK or MB_ICONSTOP
            lea     eax,msgcap
            push    eax
            lea     eax,msgcont
            push    eax
            push    NULL
            call    MessageBox                              ; We say goodbye.
            jmp     goodbye


align 4
turnon:
            mov     wasoff,1                                ; To know it was off at the begining.
            push    SPIF_UPDATEINIFILE
            push    NULL
            push    TRUE
            push    SPI_SETBEEP
            call    SystemParametersInfo                    ; We turn on the beep and starts the program.

align 4
consolego:
            call    AllocConsole                            ; Creates the console for the program.
            lea     eax,conname
            push    eax
            call    SetConsoleTitle                         ; Set the title of the console window.
            push    STD_INPUT_HANDLE
            call    GetStdHandle                            ; Handle for the input of the console
            mov     stdinput,eax
            push    STD_OUTPUT_HANDLE
            call    GetStdHandle                            ;    and the output.
            mov     stdoutput,eax
            push    NULL                                    ; Reserved by Windows.
            lea     eax,chrswrit
            push    eax
            push    174                                     ; Number of chars to write.
            lea     eax,mens00                               
            push    eax
            push    stdoutput
            call    WriteConsole
            mov     erflag,0

align 4
mainloop:
            push    NULL                                    ; Reserved by Windows.
            lea     eax,chrswrit
            push    eax
            push    269                                     ; Number of chars to write.
            lea     eax,toneprom
            push    eax
            push    stdoutput
            call    WriteConsole
            push    NULL                                    ; Reserved by Windows.
            lea     eax,chrsread
            push    eax
            push    256                                     ; Number of chars to read (as a maximum).
            lea     eax,inpbuff
            push    eax
            push    stdinput
            call    ReadConsole
            lea     esi,inpbuff
            push    esi
            call    checkit                                 ; Checks the syntax (just numbers allowed).
            cmp     erflag,0
            jne     bye
            lea     esi,inpbuff
            push    esi
            call    chksize                                 ; Checks the size of the number.
            cmp     erflag,0
            jne     bye
            mov     freq,edi
            push    NULL
            lea     eax,chrswrit
            push    eax
            push    227
            lea     eax,timeprom
            push    eax
            push    stdoutput
            call    WriteConsole
            push    NULL
            lea     eax,chrsread
            push    eax
            push    256
            lea     eax,inpbuff
            push    eax
            push    stdinput
            call    ReadConsole
            lea     esi,inpbuff
            push    esi
            call    checkit
            cmp     erflag,0
            jne     bye
            lea     esi,inpbuff
            push    esi
            call    chksize
            cmp     erflag,0
            jne     bye
            mov     milisec,edi
            push    milisec
            push    freq
            call    Beep
            push    MB_YESNO or MB_ICONQUESTION
            lea     eax,mboxcap
            push    eax
            lea     eax,mboxcon
            push    eax
            push    NULL
            call    MessageBox                              ; We ask the user to play again.
            cmp     eax,IDYES
            jne     oksobye                                 ; No, so ok bye.
            call    FreeConsole
            jmp     consolego                               ; Yes so we start the program again.





align 4
oksobye:
            cmp     wasoff,0                                ; It was turned off at the begining or not?
            je      sayonara                                ; No, so sayonara.       
            push    MB_YESNO or MB_ICONQUESTION
            lea     eax,mboxcap
            push    eax
            lea     eax,mbxcon
            push    eax
            push    NULL
            call    MessageBox                              ; Yes so we ask to turn the beep off (as it was before).
            cmp     eax,IDYES
            jne     sayonara                                ; No, so sayonara to the user.
            push    SPIF_UPDATEINIFILE
            push    NULL
            push    FALSE
            push    SPI_SETBEEP
            call    SystemParametersInfo                    ; We turn the beep off
            jmp     sayonara                                ;   and then we say sayonara to the user.



align 4
bye:

            push    NULL                                    ; Reserved by Windows.
            lea     eax,chrsread
            push    eax
            push    1                                       ; Chars to read.
            lea     eax,inpbuff
            push    eax
            push    stdinput
            call    ReadConsole                             ; For the ENTER key after we get the error in out program (for bad entries).
            call    FreeConsole
            jmp     consolego                               ; Try again.
           

align 4
sayonara:
            call    FreeConsole                             ; The program finish here, so sayonara to the console window too.
           


align 4
goodbye:
            push    0                                       ; Exit code to Windows.
            call    ExitProcess                             
           

align 4     
checkit:                                                    ; We check here the syntax of the entry (just numbers allowed).
            push    ebp
            mov     ebp,esp
            mov     ecx,chrsread
            dec     ecx                                     ; Discard 0ah and 0dh.
            dec     ecx
            mov     esi,[ebp+8]                             ; This is the address that was stored in esi (that was pushed) I did this
                                                            ;  like an  automatic thing (i haven't think yet if it is usefull).


align 4
justnumbers:
            cmp     byte ptr[esi],30h
            jb      error
            cmp     byte ptr[esi],39h
            ja      error
            inc     esi
            loop    justnumbers
            mov     esp,ebp
            pop     ebp
            ret     4

align 4
error:
            push    NULL
            lea     eax,chrswrit
            push    eax
            push    83
            lea     eax,msgerror
            push    eax
            push    stdoutput
            call    WriteConsole                            ; We write to the console the msg error.
            mov     erflag,1                                ; This flag tell us if there was an error in the mainloop.
            mov     esp,ebp
            pop     ebp
            ret     4


align 4
chksize:                                                    ; This procedure tell us if the number is in the correct range.
            push    ebp
            mov     ebp,esp
            mov     esi,[ebp+8]                             ; Address of the string of numbers.
            mov     ecx,chrsread
            dec     ecx                                     ; Discard 0ah and 0dh.
            dec     ecx
            add     esi,ecx                                 ; From right to left.
            dec     esi
            mov     ebx,1
            xor     edi,edi                                 ; Sum start in 0.



align 4
getbinary:
            movzx   eax,byte ptr[esi]
            and     eax,0000000fh                           ; Convert to unpacked bcd.
            xor     edx,edx
            mul     ebx
            add     edi,eax                                 ; Store the binary sum.
            imul    ebx,10                                  ; Increases powers of ten.
            dec     esi
            loop    getbinary   
            cmp     edi,37
            jl      errsize                                 ; Out of freq. range.
            cmp     edi,32767
            jg      errsize                                 ; Out of freq. range.
            mov     esp,ebp
            pop     ebp
            ret     4



align 4
errsize:                                                    ; We show here the error msg.
            push    NULL                                    ; Reserved by Windows.
            lea     eax,chrswrit
            push    eax
            push    83
            lea     eax,msgerror
            push    eax
            push    stdoutput
            call    WriteConsole
            mov     erflag,1                                ; For use in the mainloop of the program.
            mov     esp,ebp
            pop     ebp
            ret     4
           
            end     start                                   ; That's all that's all that's all folks!
           








felipe

I have corrected some minor problem:

I added a logical flag to see if was the beep off before the program execution. Now it will not show the message to turn the beep off (as it was) if it was on.  :bgrin:

felipe

Another problem found, that was corrected: 174 characters are written in the first call to writeconsole (it said 176 before). So no more unwanted character "1" will appear again after pushing the yes button, to play again.  :bgrin:

anunitu

The original use of the beep was for boot error codes.

https://www.computerhope.com/beep.htm

felipe