flat assembler
Message board for the users of flat assembler.

flat assembler > OS Construction > PC Speaker

Author
Thread Post new topic Reply to topic
A$M



Joined: 29 Feb 2012
Posts: 94
Hello! I'm in doubt about using the PC speaker . I searched in internet and I found the code, but I did a test using Windows and I not hear any sound. I put the code in my operating system on a virtual machine but heard nothing. I thought to be a problem of the speaker, but i open an old game that uses the PC speaker, but in this game I could hear the sound. I have no idea what is the problem. Sad
Post 01 Mar 2012, 23:44
View user's profile Send private message Reply with quote
bubach



Joined: 17 Sep 2004
Posts: 340
Location: Trollhättan, Sweden
can you post your code, maybe we can find something wrong with it or compare to other sources and documents.
Post 02 Mar 2012, 00:01
View user's profile Send private message Reply with quote
smiddy



Joined: 31 Oct 2004
Posts: 559
Here's a snippet from Using Assembly Language and controlling the speaker:

Code:
   IN al,61h
   PUSH ax

   MOV dx,0Bh

Main:

   MOV bx,477     ; 1,193,180 / 2500 (assume this is timing to frequency)
   CALL WarbCom
   MOV cx, 2600h

Delay1:

   LOOP Delay1
   MOV bx, 36         1; 1,193,180 / 32767
   CALL WarbCom
   MOV cx,1300h

Delay2:

   LOOP Delay2
   DEC dx
   JNZ Main

   POP ax
   OUT 61h,al

--------------O/----------
                  O\

WarbCom:

   MOV al,10110110b
   OUT 43h,al
   MOV ax,bx
   OUT 42h,al
   MOV al,ah
   OUT 42h,al
   IN al, 61h
   OR al,00000011b
   OUT 61h,al

   RET

    


I haven't tested this. Here is an associate paragraph:

Code:
   This example, which shows how you can control the speaker directly, uses both 8253 timer and the 8255 PPI. The Warble subroutine (the one I typed in first) provides good sound for error routines. BOOP (one I didn't type in) provides a gentle sound when the wrong key is pressed.    


p. 392 Using Assembly Language 2nd Edition, by Allen L. Wyatt, (C) 1990
Post 02 Mar 2012, 00:17
View user's profile Send private message Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 518
Location: Munich
I don't know what OS you use but if it is windows, why not use better the beep function ?

http://msdn.microsoft.com/en-us/library/windows/desktop/ms679277%28v=vs.85%29.aspx

Quote:

Syntax

BOOL WINAPI Beep(
__in DWORD dwFreq,
__in DWORD dwDuration
);

Parameters

dwFreq [in]

The frequency of the sound, in hertz. This parameter must be in the range 37 through 32,767 (0x25 through 0x7FFF).
dwDuration [in]

The duration of the sound, in milliseconds.
[/code]
Post 02 Mar 2012, 11:46
View user's profile Send private message Send e-mail Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 16782
Location: In your JS exploiting you and your system
shutdownall wrote:
I don't know what OS you use but if it is windows, why not use better the beep function ?
Erm, in the OS Construction section it is probably safe to assume that the poster is not constructing Windows. Wink
Post 02 Mar 2012, 11:52
View user's profile Send private message Visit poster's website Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 518
Location: Munich
"I put the code in my operating system on a virtual machine but heard nothing."

Hmm - if he is able to construct an own OS with an own virtual machine (for x86 ?) - i assume he wouldn't ask this question at all. Razz

I think he maybe missed the wrong section here at the board. Wink
Post 02 Mar 2012, 12:10
View user's profile Send private message Send e-mail Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 16782
Location: In your JS exploiting you and your system
The remarks section on the Beep function page is interesting:
Quote:
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.
The window 95 help file has this remark:
Quote:
Windows 95:

The Beep function ignores the dwFreq and dwDuration parameters. On computers with a sound card, the function plays the default sound event. On computers without a sound card, the function plays the standard system beep.
Post 02 Mar 2012, 12:21
View user's profile Send private message Visit poster's website Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 518
Location: Munich
On my system (Intel Quad Core + XP Prof) both works fine. Razz

Code:
        invoke  Beep,500,400
        invoke  Beep,1000,200
        invoke  Beep,2000,100
        invoke  Beep,4000,50
        invoke  MessageBeep,MB_ICONEXCLAMATION
    
Post 02 Mar 2012, 16:58
View user's profile Send private message Send e-mail Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4209
Location: 2018
beep.inc wrote:
Code:
.lutin   dd music,0,@f-$-4 \
         ,.B,.4th ,.Dd,.4th ,.Gd,.4th ,.Dd,.8th ,.Gd,.8th \
         ,.B,.4th ,.E,.4th ,.Gd,.4th ,.E,.8th ,.Gd,.8th \
         ,.B,.4th ,.Dd,.4th ,.Gd,.4th ,.Dd,.8th ,.Gd,.8th \
         ,.Ad,.4th ,.Dd,.4th ,.G,.4th ,.Dd,.8th ,.Gd,.8th \
         ,.B,.4th ,.E,.4th ,.Gd,.4th ,.E,.8th ,.Gd,.8th \
         ,.Dd shl 1,.4th ,.Gd,.4th ,.B,.4th ,.E,.8th ,.Gd,.8th \
         ,.Cd shl 1,.4th ,.G,.4th ,.Ad,.4th ,.Dd,.8th ,.Gd,.8th \
         ,.Dd shl 1,.4th ,.Fd,.4th ,.B,.4th ,.E,.8th ,.Gd,.8th
         @@:
.C = 261
.Cd= 277
.D = 293
.Dd= 311
.E = 329
.F = 349
.Fd= 370
.G = 392
.Gd= 415
.A = 440
.Ad= 466
.B = 493

.0    dd .C/8,.Cd/8,.D/8,.Dd/8,.E/8,.F/8,.Fd/8,.G/8,.Gd/8,.A/8,.Ad/8,.B/8
.1    dd .C/4,.Cd/4,.D/4,.Dd/4,.E/4,.F/4,.Fd/4,.G/4,.Gd/4,.A/4,.Ad/4,.B/4
.2    dd .C/2,.Cd/2,.D/2,.Dd/2,.E/2,.F/2,.Fd/2,.G/2,.Gd/2,.A/2,.Ad/2,.B/2
.3    dd .C*1,.Cd*1,.D*1,.Dd*1,.E*1,.F*1,.Fd*1,.G*1,.Gd*1,.A*1,.Ad*1,.B*1
.4    dd .C*2,.Cd*2,.D*2,.Dd*2,.E*2,.F*2,.Fd*2,.G*2,.Gd*2,.A*2,.Ad*2,.B*2
.5    dd .C*4,.Cd*4,.D*4,.Dd*4,.E*4,.F*4,.Fd*4,.G*4,.Gd*4,.A*4,.Ad*4,.B*4
.6    dd .C*8,.Cd*8,.D*8,.Dd*8,.E*8,.F*8,.Fd*8,.G*8,.Gd*8,.A*8,.Ad*8,.B*8
.7    dd .C*16,.Cd*16,.D*16,.Dd*16,.E*16,.F*16,.Fd*16,.G*16,.Gd*16,.A*16,.Ad*16,.B*16
.8    dd .C*32,.Cd*32,.D*32,.Dd*32,.E*32,.F*32,.Fd*32,.G*32,.Gd*32,.A*32,.Ad*32,.B*32
.9    dd .C*64,.Cd*64,.D*64,.Dd*64,.E*64,.F*64,.Fd*64,.G*64,.Gd*64,.A*64,.Ad*64,.B*64
.10   dd .C*128,.Cd*128,.D*128,.Dd*128,.E*128,.F*128,.Fd*128,.G*128,.Gd*128,.A*128,.Ad*128,.B*128


.bpm=250
.4th = (60*int8.freq)/.bpm
.8th = .4th/2
.keyboard:
Null

;.do   dd 261.6
;.dod  dd 277.2
;.re   dd 293.7
;.red  dd 311.1
;.mi   dd 329.7
;.fa   dd 349.2
;.fad  dd 370.0
;.sol  dd 392.0
;.sold dd 415.3
;.la   dd 440.0
;.lad  dd 466.2
;.si   dd 493.9
beeper:
        dd beep,100,100

music:  dd speaker.loop,0
.call=0
.current=4
.size=8
.data=12
.freq=0
.time=4

melody: dd speaker.seq,0
.call=0
.current=4
.size=8
.data=12
.freq=0
.time=4

beep:   dd speaker.event,0
.call=0
.freq=4
.time=8

speaker:
.loop:
        cmp [.time],0
        jg .noloop
        mov eax,[esi+melody.current]
        add eax,8
        cmp eax,[esi+melody.size]
        jl @f
        mov eax,0
@@:
        mov [esi+melody.current],eax
        mov ebx,[esi+eax+melody.data+melody.freq]
        mov [.freq],ebx
        mov ebx,[esi+eax+melody.data+melody.time]
        mov [.time],ebx
        mov [.cnt],0
.noloop:
        call .tone
        ret

.seq:
        cmp [.time],0
        jg .noseq
        mov eax,[esi+melody.current]
        add eax,8
        cmp eax,[esi+melody.size]
        jge .endseq
        mov [esi+melody.current],eax
        mov ebx,[esi+eax+melody.data+melody.freq]
        mov [.freq],ebx
        mov ebx,[esi+eax+melody.data+melody.time]
        mov [.time],ebx
        mov [.cnt],0
.noseq:
        call .tone
        ret
.endseq:
        call .off
        ret

.event:
        cmp [.cnt],0
        jne @f
        mov eax,[esi+beep.freq]
        mov [.freq],eax
        mov eax,[esi+beep.time]
        mov [.time],eax
@@:
        call .tone
        ret
.tone:
        cmp [.time],0
        jle .stop
        mov eax,[int8.inc]
        sub [.time],eax
        jl .stop
        cmp [.cnt],0
        jne .end
        call .set
        inc [.cnt]
.end:
        ret
.stop:
        call .off
        ret
.set:
        push eax ebx edx
        mov ebx,[.freq] ; RETURNS:  AX,BX,DX = undefined
        mov edx,[.clk+2]
        mov eax,[.clk] ; ;mov ax,0x34DC ; which is more accurate?
        div bx
        mov bl,al
        mov al,0b6h
        out 43h,al
        mov al,bl
        out 42h,al
        mov al,ah
        out 42h,al
        call .on
        pop edx ebx eax
        ret
.on:
        in al,0x61
        or al,3
        out 0x61,al
        ret
.off:
        in al,0x61
        and al,not 3
        out 0x61,al
        ret
.clk    dd 1193180
.time dd 0
.cnt  dd 0
.freq dd ?



.do   dd 261.6
.dod  dd 277.2
.re   dd 293.7
.red  dd 311.1
.mi   dd 329.7
.fa   dd 349.2
.fad  dd 370.0
.sol  dd 392.0
.sold dd 415.3
.la   dd 440.0
.lad  dd 466.2
.si   dd 493.9

    


this code needs the timer to have a routine int8 with datas named ticks, inc, etc. the beep is quite easy to implement, but the next it more tricky cause you need the timer to control the duration. or use just a delay loop, but a delay loop sucks, better to use at least a timer relative loop. and better would be to set a timer event. because sound needs a really real time synchronisation to be cool.
Post 02 Mar 2012, 17:35
View user's profile Send private message Visit poster's website Reply with quote
smiddy



Joined: 31 Oct 2004
Posts: 559
Nice edfed! Smile
Post 02 Mar 2012, 17:47
View user's profile Send private message Reply with quote
A$M



Joined: 29 Feb 2012
Posts: 94
Ok... the sound include code is:

Code:
; AX - Note frequency

speaker_tone:
        pusha

        mov cx, ax                      ; Store note value for now

        mov al, 182
        out 43h, al
        mov ax, cx                      ; Set up frequency
        out 42h, al
        mov al, ah
        out 42h, al

        in al, 61h                      ; Switch PC speaker on
        or al, 03h
        out 61h, al

        popa
        ret


speaker_off:
        pusha

        in al, 61h
        and al, 0FCh
        out 61h, al

        popa
        ret
    


It's very simple...
Post 03 Mar 2012, 00:12
View user's profile Send private message Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
You need 3 things for beep
sound on
delay
sound off
Post 03 Mar 2012, 00:57
View user's profile Send private message Reply with quote
A$M



Joined: 29 Feb 2012
Posts: 94
Ok... I will try this... Thanks. Rolling Eyes
Very Happy
Post 03 Mar 2012, 12:39
View user's profile Send private message Reply with quote
A$M



Joined: 29 Feb 2012
Posts: 94
No. Using the delay function not solve the problem, Dex4u.
Post 03 Mar 2012, 12:54
View user's profile Send private message Reply with quote
smiddy



Joined: 31 Oct 2004
Posts: 559
How long was your delay?
Post 03 Mar 2012, 16:00
View user's profile Send private message Reply with quote
A$M



Joined: 29 Feb 2012
Posts: 94
I tried 10.000.000 µs (microseconds).
Post 03 Mar 2012, 19:33
View user's profile Send private message Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
This very old code used to work, so if it does not its the PC.
Code:
org 0x100
use16
start:
CBA1:
      MOV      CX,10
      MOV      [W],10
CBA2: 
      PUSH     CX
TT4:
      ADD      [W],1000
      MOV      AX,[W]
      SAR      AX,3
      MOV      BX,40
      MUL      BX
      MOV      BX,10000
      MUL      BX
      MOV      [Hz],DX
      CALL     Sound
;      MOV      [ms1],1
;      CALL     DeLay
      MOV      [ms1],11
      CALL     DeLay
      POP      CX
      LOOP     CBA2

      CALL     NoSound
      XOR      AH,AH
      INT      16h
      
      CALL     NoSound
      IN       AL,60H
      CMP      AL,1
      JNE      CBA1

      INT      20h

                                                       
 ;----------------------------------------------------;
 ; Sound                                              ;
 ;----------------------------------------------------;
Sound:
        mov   bx,[Hz]
        mov   ax,0x34dd
        mov   dx,0x0012
        cmp   dx,bx
        jnc   Done1
        div   bx
        mov   bx,ax
        in    al,0x61
        test  al,3
        jnz   A99
        or    al,3
        out   0x61,al
        mov   al,0xb6
        out   0x43,al
A99:
        mov   al,bl
        out   0x42,al
        mov   al,bh
        out   0x42,al
Done1:
        ret

 ;----------------------------------------------------;
 ; NoSound                                            ;
 ;----------------------------------------------------;
NoSound:
        in    al,0x61
        and   al,11111100b
        out   0x61,al
        ret

 ;----------------------------------------------------;
 ; DeLay                                              ;
 ;----------------------------------------------------;
DeLay:                   
        mov   cx,[ms1]
        jcxz  A2                
A1:
        call  DeLaYoneMS
        loop  A1                
A2:
        ret   
 ;----------------------------------------------------;
 ; DeLaYoneMS                                         ;
 ;----------------------------------------------------;
DeLaYoneMS:            
        push  ecx                                    
        mov   cx,[OneMS]                
B1:
        loop  B1               
        pop   ecx                
        ret
                                   
OneMS    dw      40000
Hz       dw      0
ms1      dw      20
W        dw      0                 
    

Note: its for dos.
I know you said that the game worked, but it could be using PC sound, maybe sound blaster 16 etc.
Post 03 Mar 2012, 20:54
View user's profile Send private message Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1904
> know you said that the game worked

in DOS ??? Failed to reveal how tested the game and the code ... maybe the game uses PC speaker only if it doesn't find SB. Also, it's a bad idea to test DOS or raw code inside NTVDM.

> but it could be using PC sound, maybe sound blaster 16 etc.

Right ... if NOT run in DOS.

BTW, some PC's (but not those with ISA SB 16) simply have NO PC SPEAKER at all (and don't connect the PIT to the soundcard either) so PC speaker code will NOT WORK ... and there is no way to detect this Smile
Post 04 Mar 2012, 07:40
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4209
Location: 2018
would be really cool to have a fasm function for sound playing.
a sort of channel, using a full segment as buffer, like vga card, would be very very very nice.

i'd like to have this kind of code able to play real sounds.
and lets play math music! things that no synth can do all over the world, some randomly minded asm code will do easy.
Post 04 Mar 2012, 10:47
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2019, Tomasz Grysztar.

Powered by rwasa.