Accesare speaker pe Win7

Acest forum este dedicat intrebarilor de programare care nu-si au locul in unul din celelalte forumuri
Post Reply
u0m3
Membru
Membru
Posts: 80
Joined: 21 Dec 2008, 19:11
Judet: Bucureşti
Contact:

Accesare speaker pe Win7

Post by u0m3 » 10 May 2011, 01:50

Salut. Vin cu o problema mai delicata oarecum: stie cineva daca este posibil sa "accesez" speaker-ul (cel de pe placa de baza) pe Windows 7?

La prima vedere ar parea o problema simpla...

Code: Select all

::Beep(dwFrequency, dwTime);
Daca folosesti un OS mai vechi decat Windows XP 64bit (pe 32bit am inteles ca merge). In rest:
  • Windows XP 64bit / Windows Vista 32/64 bit - dezactivat (nu se aude)
  • Windows 7 - tonul este reprodus prin placa de sunet
Am cautat pe Google si aparent este foarte usor de accesat acest "dispozitiv" din assembler. Doar ca am gasit exemple doar pentru assembler-ul pentru DOS.
Presupun ca se poate "traduce" si pentru MASM, dar intrebare este: ar fi o idee buna sau risc sa produc BSOD-uri?
MSDN wrote: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.
Daca are importanta, exemplul pe care l-am gasit este acesta:

Code: Select all

; 
; PIANO3.ASM  [ For true DOS ] 
; One-Octave 'Piano' Program 
; Free from Annie 
; 
;   Use keyboard number keys 1 through 8 to play the notes. 
;   Space bar toggles the 'sustain' function.  ESC exits. 
; 
;     This code assembles, as-is, with the A86 assembler. 
; 
;        To assemble: A86 PIANO3.ASM 
; 
code    segment 
        org     100h            ;DOS .COM file 
        jmp     start           ;go start the program 
; 
tog     db      0               ;our 'sustain' flag 
; 
start: 
        mov     dx,offset msg   ;point DX to sign-on message 
        call    prt_str         ;print it 
; 
; Get user keypress. 
; 
get_key: 
        mov     ah,0            ;function 0 - wait for keypress 
        int     16h             ;call ROM BIOS keyboard services 
        cmp     al,27           ;was ESC pressed? 
        jz      quit            ;yes, so go exit 
        cmp     al,32           ;was SPACE pressed? 
        jz      toggle          ;yes, so go toggle 'sustain' mode 
; 
; Filter out all keys except '1' through '8' by checking the scan code. 
; 
        cmp     ah,02h          ;less than '1'? 
        jl      get_key         ;yes, so ignore it 
        cmp     ah,09h          ;greater than '8'? 
        jg      get_key         ;yes, so ignore it 
; 
; Set up the tone parameters. 
; 
        sub     al,21h          ;change scan code to to digit (0-9) 
        and     al,00000111xb   ;mask off upper 5 bits 
        shl     al,1            ;* by 2 (2 bytes/word) 
        cbw                     ;byte --> word in AX 
        mov     bx,ax           ;put in BX (for table) 
        mov     ax,0            ;numerator (low word) 
        mov     dx,12h          ;(high word) 
        div     word ptr [table] + bx  ;divisor from table 
        mov     bx,ax           ;save quotient in BX 
; 
; Set 1/pitch into timer, then turn on tone. 
; 
        mov     al,10110110xb   ;the magic number... 
        mov     dx,43h          ; 
        out     dx,al           ; 
        mov     ax,bx           ;1/pitch into AX 
        mov     dx,42h          ; 
        out     dx,al           ; 
        mov     al,ah           ;MSB to AL, then... 
        out     dx,al           ; 
        mov     dx,61h          ; 
        in      al,dx           ; 
        or      al,3            ;turn on bits 0 and 1... 
        out     dx,al           ; 
        call    clr_buf         ;yes, so go clear keyboard buffer 
        cmp     tog,1           ;is 'sustain' on? 
        je      get_key         ;go get another keypress 
; 
; Delay for 2/18ths of a second. 
; 
delay: 
        mov     ah,00h          ;function 0 - get system timer tick 
        int     01Ah            ;call ROM BIOS time-of-day services 
        add     dx,2            ;add our delay value to DX 
        mov     bx,dx           ;store result in BX 
pozz: 
        int     01Ah            ;call ROM BIOS time-of-day services 
        cmp     dx,bx           ;has the delay duration passed? 
        jl      pozz            ;no, so go check again 
        call    stopnote        ;go turn off the note 
        jmp     get_key         ;go get another keypress 
; 
quit: 
        jmp     exit            ;'lilypad' to exit 
; 
; Toggle 'sustain' mode. 
; 
toggle: 
        cmp     tog,0           ;'sustain' mode currently off? 
        je      turn_on         ;yes, so go turn it on 
        mov     tog,0           ;no, so set 'sustain' flag to 'off' 
        call    stopnote        ;turn off note 
        call    clr_buf         ;go clear keyboard buffer 
        mov     dx,offset off   ;point DX to appropriate screen msg 
        call    prt_str         ;print it 
        jmp     get_key         ;go get another keypress 
turn_on: 
        mov     tog,1           ;set 'sustain' flag to 'on' 
        call    clr_buf         ;go clear keyboard buffer 
        mov     dx,offset onn   ;point DX to appropriate screen msg 
        call    prt_str         ;print it 
        jmp     get_key         ;go get another keypress 
; 
; Clear the keyboard buffer. 
; 
clr_buf: 
        push    es              ;preserve ES 
        push    di              ;preserve DI 
        mov     ax,40h          ;BIOS segment in AX 
        mov     es,ax           ;transfer to ES 
        mov     ax,1Ah          ;keyboard head pointer in AX 
        mov     di,ax           ;transfer to DI 
        mov     ax,1Eh          ;keyboard buffer start in AX 
        mov     es: word ptr [di],ax   ;transfer to head pointer 
        inc     di              ;bump pointer to... 
        inc     di              ;...keyboard tail pointer 
        mov     es: word ptr [di],ax   ;transfer to tail pointer 
        pop     di              ;restore DI 
        pop     es              ;restore ES 
        ret 
; 
; Turn off speaker. 
; 
stopnote: 
        push    dx              ;preserve DX 
        mov     dx,61h          ; 
        in      al,dx           ; 
        and     al,11111100xb   ;mask lower 2 bits 
        out     dx,al           ; 
        pop     dx              ;restore DX 
        ret                     ;return to caller 
; 
; ESC pressed, so exit. 
; 
exit: 
        call    stopnote        ;go silence the speaker 
        mov     al,10           ;linefeed in AL 
        int     29h             ;quick-print it 
        int     20h             ;exit to DOS 
; 
; Our 'print string' routine. 
; 
prt_str: 
        mov     ah,9            ;function 9 - print string 
        int     21h             ;call DOS services 
        ret                     ;return to caller 
; 
msg     db      13,10,'PIANO3' 
        db      13,10,'Keyboard number keys 1 through 8 play the notes.' 
        db      13,10,'ESC exits.  SPACE BAR toggles sustain -- now OFF$' 
onn     db      08,08,'N ',08,'$' 
off     db      08,'FF$' 
; 
;frequencies of notes 
; 
table   dw      262             ;middle C 
        dw      294             ;D 
        dw      330             ;E 
        dw      347             ;F 
        dw      392             ;G 
        dw      440             ;A 
        dw      494             ;B 
        dw      524             ;C 
end 
luat de aici.


Multumesc anticipat pentru raspunsuri/sugestii.


Old-School: If at first you don't succeed, try and try again.
New-School: If at first you don't succeed, destroy every evidence that you have ever tried.
Citate nostime:
  1. Mintile umane sunt ca parasutele... Functioneaza doar daca sunt deschise.
  2. Light travels faster than sound, that's why some people seem Bright ...... untill they Speak...

neagu_laurentiu
Membru++
Membru++
Posts: 919
Joined: 23 Jul 2007, 11:32

Re: Accesare speaker pe Win7

Post by neagu_laurentiu » 10 May 2011, 07:00

u0m3 wrote:Doar ca am gasit exemple doar pentru assembler-ul pentru DOS. Presupun ca se poate "traduce" si pentru MASM
Nu se poate. Principiile sistemului de operare Windows nu te lasa sa faci ce vrei ca in vremea lui MS-DOS. In plus ai citit in MSDN care sunt conditiile oferite de MS.
WDK e o alta poarta de intrare in inima Windows-ului si poate se gaseste ceva...

u0m3
Membru
Membru
Posts: 80
Joined: 21 Dec 2008, 19:11
Judet: Bucureşti
Contact:

Re: Accesare speaker pe Win7

Post by u0m3 » 10 May 2011, 10:52

neagu_laurentiu wrote:
u0m3 wrote:Doar ca am gasit exemple doar pentru assembler-ul pentru DOS. Presupun ca se poate "traduce" si pentru MASM
Nu se poate. Principiile sistemului de operare Windows nu te lasa sa faci ce vrei ca in vremea lui MS-DOS. In plus ai citit in MSDN care sunt conditiile oferite de MS.
WDK e o alta poarta de intrare in inima Windows-ului si poate se gaseste ceva...
De ce imi era frica tot nu scap... Driver development here I come. :?

Multumesc pentru clarificare.
Old-School: If at first you don't succeed, try and try again.
New-School: If at first you don't succeed, destroy every evidence that you have ever tried.
Citate nostime:
  1. Mintile umane sunt ca parasutele... Functioneaza doar daca sunt deschise.
  2. Light travels faster than sound, that's why some people seem Bright ...... untill they Speak...

neagu_laurentiu
Membru++
Membru++
Posts: 919
Joined: 23 Jul 2007, 11:32

Re: Accesare speaker pe Win7

Post by neagu_laurentiu » 10 May 2011, 12:16

Dar de ce nu te multumeste iesirea prin placa audio ?

User avatar
Marius Bancila
Fondator
Fondator
Posts: 2344
Joined: 11 Jul 2007, 11:45
Judet: Timiş
Location: Timisoara
Contact:

Re: Accesare speaker pe Win7

Post by Marius Bancila » 10 May 2011, 12:46

Cred ca asta spune cam tot ce e de spus:
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.
Marius Bancila
Fondator Codexpert, Microsoft MVP VC++
Site personal | Blog

u0m3
Membru
Membru
Posts: 80
Joined: 21 Dec 2008, 19:11
Judet: Bucureşti
Contact:

Re: Accesare speaker pe Win7

Post by u0m3 » 11 May 2011, 14:10

neagu_laurentiu wrote:Dar de ce nu te multumeste iesirea prin placa audio ?
Server-ele nu au sisteme audio conectate. :D
Doream sa fac un fel de sistem de avertizare pentru diferite scenarii, cum ar fi: temperatura prea mare a CPU, variatii prea mari de tensiune, etc.

Este proiect personal.

Cat despre afirmatia Microsoft: toate pc-urile pe care le am, au speaker, care piuie la BIOS POST. :D
Old-School: If at first you don't succeed, try and try again.
New-School: If at first you don't succeed, destroy every evidence that you have ever tried.
Citate nostime:
  1. Mintile umane sunt ca parasutele... Functioneaza doar daca sunt deschise.
  2. Light travels faster than sound, that's why some people seem Bright ...... untill they Speak...

User avatar
Silviu Ardelean
Senior
Senior
Posts: 1175
Joined: 12 Jul 2007, 09:22
Judet: Timiş
Location: Timisoara
Contact:

Re: Accesare speaker pe Win7

Post by Silviu Ardelean » 11 May 2011, 14:22

In principiu servele se afla in incinte special amenajate cu zgomot foarte ridicat iar beep-uitul unui speaker nu cred ca ar ajuta pe cineva. :)
Avertizezi prin alte mijloace... email, sms, etc.

User avatar
Marius Bancila
Fondator
Fondator
Posts: 2344
Joined: 11 Jul 2007, 11:45
Judet: Timiş
Location: Timisoara
Contact:

Re: Accesare speaker pe Win7

Post by Marius Bancila » 11 May 2011, 14:29

Ce intelegi tu prin server? Un PC obisnuit care ruleaza eventual Windows Server 200x si care e folosit pe post de computer pe care nu lucreaza nimeni, dar e tot timpul pornit pt. ca ruleaza cine stie ce aplicatie? Sau vorbim de servere de business dedicate, de genul celor la care se refera Silviu?
Marius Bancila
Fondator Codexpert, Microsoft MVP VC++
Site personal | Blog

u0m3
Membru
Membru
Posts: 80
Joined: 21 Dec 2008, 19:11
Judet: Bucureşti
Contact:

Re: Accesare speaker pe Win7

Post by u0m3 » 13 May 2011, 21:53

Marius Bancila wrote:Ce intelegi tu prin server? Un PC obisnuit care ruleaza eventual Windows Server 200x si care e folosit pe post de computer pe care nu lucreaza nimeni, dar e tot timpul pornit pt. ca ruleaza cine stie ce aplicatie? Sau vorbim de servere de business dedicate, de genul celor la care se refera Silviu?
Este un pc mai vechi care ruleaza un windows xp 64bit, pe care il folosesc ca Network Attached Storage (Windows File Sharing/Samba), server OpenVPN, si mai rulez ocazional niste masini virtuale cand nu imi mai permite RAM-ul de pe pc-ul personal.

Am incercat sa accesez speaker-ul ca metoda de avertizare, deoarece m-am trezit de cateva ori ca a murit subit placa de retea (cred ca are niste probleme driver-ul) si mi-a luat ceva timp sa inteleg ce si cum.
Old-School: If at first you don't succeed, try and try again.
New-School: If at first you don't succeed, destroy every evidence that you have ever tried.
Citate nostime:
  1. Mintile umane sunt ca parasutele... Functioneaza doar daca sunt deschise.
  2. Light travels faster than sound, that's why some people seem Bright ...... untill they Speak...

User avatar
Marius Bancila
Fondator
Fondator
Posts: 2344
Joined: 11 Jul 2007, 11:45
Judet: Timiş
Location: Timisoara
Contact:

Re: Accesare speaker pe Win7

Post by Marius Bancila » 13 May 2011, 21:59

Marius Bancila
Fondator Codexpert, Microsoft MVP VC++
Site personal | Blog

Post Reply