flat assembler
Message board for the users of flat assembler.

flat assembler > Main > PC speaker issue + code

Author
Thread Post new topic Reply to topic
Paul6253



Joined: 19 Oct 2003
Posts: 31
Location: NY
hi folks.

i wrote some code to play sounds in pc speaker:
one is ASM floppy bootsector, other for linux in C.
the code worked well in linux and off floppy boot

Now all of a sudden the code does not work in
linux the same way:
fails to produce tones,only a clicking sound.
I have not modified the code in any way,
even tried recompiling and tested on different
installations.

The bootsector code still works fine...so that rules out
hardware issues

odd...

any clues???

Code:


;x86 bootsector ,one sector
; Program entry point
; Currently we are at 0:7C00
;************************************************************
org 0
EntryPoint:

    jmp 0x07C0:AfterData
     
                    
    ; BIOS PARAMETER BLOCK
              oemID                           db "MS-DOS", 0, 0     ; 8 bytes
           bytesPerSector          dw 0200h
            sectorsPerCluster       db 01h
              nrReservedSectors       dw 0001h                        ; bootloader is spanning 1 sectors
          nrFats                          db 02h
              nrRootDirs                      dw 00E0h
            totalSectorsSmall       dw 0B40h
            mediaID                         db 0F0h
             sectorsPerFat           dw 0009h
            sectorsPerTrack         dw 0012h
            headsPerCylinder        dw 0002h
            hiddenSectors           dd 00000000h
                totalSectorsLarge       dd 00000000h
                driveNr                         db 00h                                  
    ;OPTIONAL FIELDS, NOT NEEDED ON
     ;FAT12, SKIPPED TO SAVE SIZE.
               flags                           db 00h
              signature                       db 29h
              volumeID                        dd 0FFFFFFFFh
               volumeLabel                     db "Pauls kernel"
         systemID                        db "FAT12   "                 

        ;my data
             BOOT_MSG db 'SHAKE YA BOOTY !',10,13,0
                 ACTIVITY db 'TURN THAT NOISE OFF!',0
        
TONE_TABLE dw    666,444,555,777,212,660,0
           



      


AfterData:
; update DS to be 7C0 instead of 0
push CS
pop DS

; update ES also
push CS
pop ES

; create stack
mov ax, 0x0000
mov ss, ax
mov sp, 0xFFFF

 
; display boot message...
lea si, [BOOT_MSG]
call Print
call Play

mov dx,10
DIE_LOOP:
lea si, [ACTIVITY]
call Print
mov cx, 0xffff
delay1:
mov bx, 0xfff
delay2:
dec bx
jnz delay2
dec cx
jnz delay1
dec dx
jnz DIE_LOOP

int 19h          ;reboot

;************************************************************
; Procedure print
; prints a zero terminated string pointed to by si
;************************************************************
Print:
push ax
mov ah, 14; BIOS code for screen display
cld
print_loop:
lodsb; moving the character to be displayed to al
or al, al; checking if the char is NULL
jz printdone
int 10h; Calling BIOS routine
JMP print_loop

printdone:
pop ax
ret
; End of print procedure...


;************************************************************
; Procedure Play
; plays a sick and demented melody 
;************************************************************

Play:

        pusha                    ;save regs
        lea si, [TONE_TABLE]    ;load sound data
GET_NEXT: 
        mov di,[si]              ;grab a tone
      call Sound              ;play it 
   call Delay
  

        
        add si,2
        cmp word [si],0
     jne GET_NEXT
        
    mov  dx,0                        ;turn off speaker
        call ChangeSpeaker
          
      
        


        ;restore regs and return
        popa
RET
;end Play            

        ;status in dx ,1= on 0 = off
ChangeSpeaker:
in al,0x61
cmp dx,1
jne off
or al,3
jmp on

off:
mov dx,3
not dx
and ax,dx
on:
out 0x61,al
RET
;end ChangeSpeaker



Sound: 

;hertz in di
mov  dx,1                        ;turn on speaker
call ChangeSpeaker

mov al,0xb6
out 0x43, al

                                ;  calculate tone
        mov    dx,14h           
        mov    ax,4F38h          ;  divisor of frequency
        div    di 
                          ;  play tone                 
        out    42h,al            ;  lower byte of frequency
        mov    al,ah            ;
        out    42h,al            ;  higher byte of frequency

RET
;end sound


Delay:
        mov cx, 0xffff
        delay3:
        mov bx, 0xfff
        delay4:
        dec bx
        jnz delay4
        dec cx
        jnz delay3
RET
;end Delay

          
            
            
            
            
; Make the file 512 bytes long
TIMES 510-($-$$) DB 0

; Add the boot signature
dw 0AA55h







    



Code:



#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/io.h>



#define ON  (1)
#define OFF (0)
/*------------------------------------------------
ChangeSpeaker - Turn speaker on or off.
*/

void ChangeSpeaker( int status )
{
int portval;
portval = inb( 0x61 );
if ( status==ON )portval|= 0x03;
else
  portval &= ~0x03;
outb(portval,0x61 );


}



 /*--ChangeSpeaker( )----------*/
void Sound( int hertz )
{
unsigned divisor = 1193180L / hertz;

ChangeSpeaker( ON );

outb( 0x43, 0xB6 );
outb( (divisor & 0xFF),0x42 );
outb( divisor >> 8 ,0x42);
}

void NoSound( void )
{
ChangeSpeaker( OFF );
}


int main( int argc ,char **argv)
{

int Playlist[5];
int i;



if( iopl(3) < 0 )// for requesting kernel io perms

                {
                fprintf(stderr,"Error iopl(): Are you root? \n");
                exit(-1);
                }

printf("enter a series of 5 tones ->  ");

do
{
  for (i=0;i < 5;i++ ) {
      scanf("%d",&Playlist[i]);
             if(Playlist[i] == 0){
                i = 6;
                break;
              }
      }

  if(i == 6) break;


   for (i=0;i < 5;i++ ) {
      Sound( Playlist[i]);
        sleep( 1 );

     }

}while (1);

NoSound( );

return(0);

}






    

_________________
Plez xcuce mi spelng
Post 16 Oct 2004, 01:18
View user's profile Send private message Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1171
Location: Overflow
Howdy folk,
are you sure this won't hurt anybody?

Code:
; create stack 
mov ax, 0x0000 
mov ss, ax 
mov sp, 0xFFFF 
    


what do you say using this instead?

Code:
; create stack 
push cs
pop ss
mov sp, 0xFFFF 
    


or if you need large stacks:

Code:
; create stack 
mov ax,cs
add ax,4096 ; + 64 kB @ offset side
mov ss,ax
mov sp, 0xFFFF 
    


Last edited by Matrix on 21 Oct 2004, 14:43; edited 1 time in total
Post 20 Oct 2004, 03:01
View user's profile Send private message Visit poster's website Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1171
Location: Overflow
I have made a simple PC speaker note player, Privalov, could you take a look at the times at the end? it is not working like this, my file is 256 bytes !

Code:
; Program: Simple Note Player by MATRIX
org 256
call setup_pit_timer

mov si,msg
call Print
mov bx,tonedata
call Play

call reset_pit_timer
int 20h

;************************************************************
; Procedure print
; prints a zero terminated string pointed to by si
;************************************************************

Print:
push ax
mov ah,14; BIOS code for screen display
cld
print_loop:lodsb; moving the character to be displayed to al
or al, al; checking if the char is NULL
jz printdone
int 10h; Calling BIOS routine
JMP print_loop
printdone:pop ax
ret
; End of print procedure...

;************************************************************
; Procedure Play
; plays a sick and demented melody
;************************************************************
Play:
pusha          ;save regs
localloop:
mov ax,[bx]    ;load sound data
add bx,2
or ax,ax
jz finished
jns notdelay
delay:
push bx
neg ax
mov cx,ax
call pit_tick_delay
pop bx
jmp localloop
notdelay: ; not delay, its a note
push bx
call sound  ;play it
pop bx
jmp localloop
finished:
call nosound
popa
ret
;end Play

nosound: ; Silences the speaker.
in al,0x61
and al,0xFC
out 0x61,al
ret

sound: ; AX = frequency Starts the speaker emiting a sound of a given frequency
mov bx,ax ; RETURNS:  AX,BX,DX = undefined
mov dx,0x12
mov ax,0x34DD ; ;mov ax,0x34DC ; which is more accurate?
div bx
mov bl,al
mov al,0xB6
out 0x43,al
mov al,bl
out 0x42,al
mov al,ah
out 0x42,al
in al,0x61
or al,3
out 0x61,al
ret

pit_tick_delay:  ; waits cx ticks, destroys: ax bx
;cli ;if you need accurate timing uncomment this
  call    read_pit_value
 mov     bx,ax
.below:
  call    read_pit_value
 cmp     ax,bx
   jl     .below
.above:
  call    read_pit_value
 cmp     ax,bx
   jg      .above
 loop    .below
;sti ;if you need accurate timing uncomment this
ret

reset_pit_timer:
 mov al,00110110b ; count down twice generator
jmp setupcommon
setup_pit_timer:  ; returns status in al
 mov al,00110100b ; rate generator
setupcommon:
cli ;if you need accurate timing comment this refer to above
 out 43h,al
 xor al,al
 out $40,al
 out $40,al
.waitlatch:
 mov al,11100010b ; get timer 0 status
  out 43h,al
  in al,40h
bt ax,6
jc .waitlatch
sti ;if you need accurate timing comment this refer to above
ret

read_pit_value:
;.waitlatch:                                  ; if you have early 8253s comment
; mov al,11100010b ; get timer 0 status       ; this part out
; out 43h,al                                  ; ( wais for latch to be valid )
; in al,40h                                   ;
;bt ax,6                                      ;
;jc .waitlatch                                ;
  mov al,11010010b
cli
  out 43h,al
  in al,40h
  mov ah,al
  in al,40h
sti
  xchg al,ah
ret

msg: db 'Playing sound',0
tonedata: ; negatives are delay
dw 440,-7,880,-4,440,-4,330,-15
dw 440,-7,880,-4,440,-4,330,-15
dw 550,-7,440,-4,330,-4,440,-15
dw 0 ; stop !

; Make the file 512 bytes long
;times 512+256-$ Db 0 ; file is org 256 based so

    


Last edited by Matrix on 20 Oct 2004, 08:24; edited 1 time in total
Post 20 Oct 2004, 04:06
View user's profile Send private message Visit poster's website Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1171
Location: Overflow
Here's the latest version, i have added some stuff:


Description: First
Download
Filename: pcspkplayer001.asm
Filesize: 3.06 KB
Downloaded: 603 Time(s)

Description: Latest
Download
Filename: pcspkplayer002.asm
Filesize: 5.08 KB
Downloaded: 578 Time(s)

Post 20 Oct 2004, 08:29
View user's profile Send private message Visit poster's website Reply with quote
Paul6253



Joined: 19 Oct 2003
Posts: 31
Location: NY
yeah I mean I still trying to figure why my linux code dont work no more...
Confused

_________________
Plez xcuce mi spelng
Post 22 Oct 2004, 15:59
View user's profile Send private message Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1171
Location: Overflow
I don't know what you mean,
my code should work without an operating system,
only a few modifcations whould be needed, since it is writing to ports.
You could substract the parts you need no?
Post 22 Oct 2004, 17:31
View user's profile Send private message Visit poster's website Reply with quote
oveja



Joined: 03 Mar 2004
Posts: 21
Location: Mexico
┬┐How play Midi File whit pcSpeaker? Twisted Evil
Post 21 Apr 2005, 17:48
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1171
Location: Overflow
Hy!
its not that simple task,
first you have to make a midi player, then decide a way to render on pc speaker, this will probably work only on dos, none multitask os.
you might laugh on this but some simple linux has the ability to emulate soundblaster on pc speaker, you can play mp3 on pc speaker.

i have a mod player somewhere able to play mod files on pcspeaker with fair quality (16KHz sampling rate) but with no source codes, just dos executable ( software )
Post 22 Apr 2005, 16:16
View user's profile Send private message Visit poster's website Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2335
Location: Usono (aka, USA)
http://www.ibiblio.org/pub/micro/pc-stuff/freedos/files/distributions/1.0/pkgs/

MIDIPLAX.LSM wrote:

Begin3
Title: MidiPlay
Version: 1.0
Entered-date: 03MAY2001
Description: play a MIDI file through a PC's internal speaker.
Keywords: midi, music, speaker
Author: James Allwright <J.R.Allwright_AT_westminster.ac.uk>
Maintained-by: James Allwright <J.R.Allwright_AT_westminster.ac.uk>
Primary-site: http://perun.hscs.wmin.ac.uk/~jra/MIDIplay/
Alternate-site: http://perun.hscs.wmin.ac.uk/~jra/MIDIplay/
Original-site: http://perun.hscs.wmin.ac.uk/~jra/MIDIplay/
Platforms: DOS
Copying-policy: freeware w/source
End


I think it used Pacific C plus PCC's weird assembly, so it may be a bit quirky to understand. But it does include a simple sample .MID file. Wink
Post 12 Jul 2007, 23:00
View user's profile Send private message Visit poster's website Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1904
WOW ! Does it work ?
Post 12 Jul 2007, 23:05
View user's profile Send private message Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2335
Location: Usono (aka, USA)
Uh, yes, of course, why would I post otherwise? Wink

Of course, it only plays mono-channel ones, so you won't hear a bunch of "instruments" at the same time (or whatever).

And, I know of at least one (slightly imperfect) .MOD / .STM / .S3M / .XM etc. player for DOS (even XTs) for soundcards and PC speaker (direct d/l): Galaxy Music Player.

And here's a .WAV player for DOS via PC speaker too: LxVox.

P.S. But if you want soundcard .MID players for DOS, try there: RealMIDI (w/ src) or GSplay.
Post 12 Jul 2007, 23:33
View user's profile Send private message Visit poster's website Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1904
rugxulo wrote:

(much)

Thanks.

> why would I post otherwise?

You tend to uncritically (?) advertise stuff Arrow

> And here's a .WAV player for DOS via PC speaker

I have my own ... written in FASM Idea

OTOH none of audio codecs has FASM source so I have to deal with all the silly C code ... at least there is a usable and well supported C compiler around by now Wink

_________________
Bug Nr.: 12345

Title: Hello World program compiles to 100 KB !!!

Status: Closed: NOT a Bug
Post 12 Jul 2007, 23:53
View user's profile Send private message 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.