Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sound Help

Status
Not open for further replies.

Gotejjeken

Programmer
Dec 1, 2006
2
0
0
US
Are there any reliable sound routines I could use to make my keyboard sound like a 'piano'? Our instructor gave us one however it doesn't seem to be working correctly.
 
> Are there any reliable sound routines I could use to make my keyboard sound like a 'piano'?
Well that really depends on your Operating system, sound card, assembler etc etc etc.

> Our instructor gave us one however it doesn't seem to be working correctly.
Back in my day, I would have showed my example program and results to the instructor.
Since you have provided to us neither
- your library
- your attempt
its really hard to say what you should do.

--
 
INCLUDE PCMAC.INC
.MODEL SMALL ; Small memory model
.586 ; Pentium Instruction Set
.STACK 100h ; Stack area, saves 256 bytes

.DATA

;--------------Other Messages----------------
Explan1 DB 'This program is designed to play various key frequencies. $'
Explan2 DB 'a,s,d,f,j,k, and l are normal notes while w, r, i, and p $'
Explan3 DB 'are sharps/flats. The n key is octave up and the m key is $'
Explan4 DB 'octave down, while the enter key is used to exit. $'
;-----------Input Setup----------------------
Char_Array DB 'a', 's', 'd', 'f', 'j', 'k', 'l', 'i', 'p', 'r', 'w'
Freq_Array DW 440, 494, 550, 588, 660, 698, 800, 310, 370, 278, 466
Freq_Array2 DW 880, 988, 1100, 1174, 1320, 1396, 1600, 622, 740, 554, 932
Freq_Array3 DW 1760, 1976, 2200, 2348, 2640, 2792, 3200, 1244, 1480, 1108, 1864
Newline DB 13,10, '$'
;----------------Variables-------------------
Octave DB 1
Save DW 0
divider DD 1e294h

.CODE

Main PROC
;----------Display Explanation---------------
mov ax, @DATA
mov ds, ax
_PutStr Explan1
_PutStr Newline
_PutStr Explan2
_PutStr Newline
_PutStr Explan3
_PutStr Newline
_PutStr Explan4
CharLoop:
mov bx, 0
_BIOSCh
cmp al, 'n' ; increase octave
je HandleLS
cmp al, 'm' ; decrease octave
je HandleRS
cmp ah, 1Ch ; was char an enter?
je Ext
CArrayLoop:
cmp al, [Char_Array+bx] ; does the keypress match a letter?
je FArrayH
inc bx ; if not move on
cmp bx, 11 ; end of array?
je CharLoop
jmp CArrayLoop
FArrayH:
cmp Octave, 1
jl HandleLower
je FArray1
cmp Octave, 2
je FArray2
cmp Octave, 3
je FArray3
jg HandleUpper
HandleLower:
mov Octave, 1 ; if n key is pressed too many times bound it
jmp FArray1
HandleUpper:
mov Octave, 3 ; if m key is pressed too many times bound it
jmp FArray3
FArray1:
mov Save, ds
mov ax, [Freq_Array+bx]
call SndSpeak
jmp RestoreLoop
FArray2:
mov Save, ds
mov ax, [Freq_Array2+bx]
call SndSpeak
jmp RestoreLoop
FArray3:
mov Save, ds
mov ax, [Freq_Array3+bx]
call SndSpeak
jmp RestoreLoop
HandleLS:
inc Octave
jmp CharLoop
HandleRS:
dec Octave
jmp CharLoop
RestoreLoop:
mov ds, Save
jmp CharLoop
;----------Close Program-----------
Ext:
mov ax, 4c00h
int 21h
;----------------------------------
Main ENDP


SndSpeak PROC

mov bx, ax
mov eax, divider
div bx
mov cx, ax
cli ;no interrupts
mov al, 10110110b ;get ready byte
out 043h, al
mov al, cl ;frequency divisor low byte
out 042h, al
mov al, ch ;frequncy divisor high byte
out 042h, al

; Switch speaker on

in al, 061h ;get port 61h contents
or al, 00000011b ;enable speaker
out 061h, al

; Wait CX clock ticks

sti ;must allow clock interrupt

mov cx, 12 ; change to number in other students code
mov cx, 6 ;About 1 second (1/18.2*t=num) t=time num=number of clock ticks
mov ax, 040h ;point DS to BIOS
mov es, ax
mov si, 06ch

push ds ;save the data segment
mov ds, ax

waitlp: mov ax, [si] ;get low word of tick counter

wait1: cmp ax, [si] ;wait for it to change
je wait1
loop waitlp ;count CX changes

pop ds ;restore the data segment

; Switch speaker off

in al, 61h ;get port contents
and al, 11111100b ;disable speaker
out 61h, al


ret

sndspeak ENDP
END Main

There is my code, the tones seem to play but changing the octave does nothing, and repeated key presses of the same note in the same octave sound differently o_O.
 
Doesn't a word-sized div use dx and ax to contain the initial number to be divided? I can't remember. I'm absolutely certain it doesn't use eax because if it did, it wouldn't have worked before the 386.
 
DX:AX for 16 bit DIV operation.

AX contains quotient, DX contains remainder.
 
... in which case your sound is all wrong because you've put the large part of the number you want to divide in the top half of eax, when it ought to have been in dx. Since dx is otherwise unused in your code, it will contain whatever was left after the key-reading bit or whatever the remainder was in the last division, which will, of course, change even if you press the same key again...
Good luck! (and you might find, if you like sound and assembler, that it's fun to play with soundblaster and adlib card things; any modern PC will emulate them)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top