汇编播放音频文件TASM 16位 [英] Assembly playing audio file TASM 16 bit

查看:198
本文介绍了汇编播放音频文件TASM 16位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在为学校项目编写游戏,但在播放音频文件时遇到问题. 我可以播放文件,但是问题是程序随后冻结并停止响应用户

Hello i'm writing a game for school project and i'm having problems to play an audio file. I was able to play the file but the problem is that then the program freezes and stop responding to the user

音乐文件代码

proc read Near ; Read next sample
    push bx
    push cx
    push dx
    mov ah, 3Fh
    mov bx, [filehandle]
    mov cx, 1
    lea dx, [Buffer]
    int 21h
    jc errorcode
    mov al, [Buffer]
    xor ah, ah
    mov bx, 54
    mul bx
    ; mov ax, dx ; Result is in DX because we need to div by 65536 which is all of AX
    shr ax, 8
    pop dx
    pop cx
    pop bx
    ret
endp
proc br Near; Print line break
    push dx
    push ax
    mov dl, 10
    mov ah, 2
    int 21h
    mov dl, 13
    mov ah, 2
    int 21h
    pop ax
    pop dx
    ret
endp
proc PlayMusic Near
    mov ah, 3Dh
    xor al, al
    lea dx, [filename]
    int 21h
    mov [filehandle], ax
    call br
    mov al, 90h
    out 43h, al
in al, 61h
    or al, 3
    out 61h, al
    cli
    mov ax, 0
totalloop:
    call read ; Read file
    out 42h, al ; Send data
    mov bx, ax
    mov cx, [delay]
portloop:
    loop portloop
    mov ax, bx
    out 42h, ax ; Send data
    mov cx, [delay]
rloop:
    loop rloop
    call read
    jmp totalloop
    ret
errorcode:
    ; Close
    sti
    mov al, 86h
    out 43h, al
    mov ah, 3Eh
    mov bx, [filehandle]
    int 21h
    pop dx
    pop cx
    pop bx
    ret
endp PlayMusic

,在我调用此proc之后,我正在调用int 16h以从用户读取密钥,但是音乐文件阻止了IO 有人知道我需要做什么? 对不起,我英语不好 而且我在组装方面有点新,所以我不太好

and after i call this proc i'm calling int 16h to read a key from the user but the music file blocks the IO someone have an idea what I need to do? sorry for my bad English by the way and i'm kind of new in assembly so i'm not so good

推荐答案

您想要做的是并行化"您的游戏代码(不使用线程).如何执行此操作的选项很少:

What you want to do is "parallelize" your game code (without using threads). There are few options on how to do this:

  1. 插入代码

这是最简单的实现方法.因此,您应该具有以下游戏架构:

This is the easiest way to implement. So you should have your game architecture like this:

loop:handle input
     handle game logic
     render frame
     play sound (20-40ms)
     jmp loop

这种体系结构将使游戏可以并行"运行其所有任务.如果您的任务比较慢,则可以使音乐交错更多

This architecture will allow the game to run all its task in "parallel". If your tasks are slower you can interleave the music a bit more

loop:handle input
     play sound (~10ms)
     handle game logic
     play sound (~10ms)
     render frame
     play sound (~10ms)
     jmp loop

如果任何任务真的很慢,那么您需要将声音直接插入其中,否则会产生断断续续的声音.这是一个示例(无声音):

if any task is really slow then you need to interleave the sound into it directly otherwise you would have choppy sound. Here is an example of this (without sound):

这是有声音的(但不是游戏):

And this is with sound (but not a game):

您需要使键盘测试不阻塞.摘自我的No Signal演示,它是这样完成的:

You need to make your keyboard test non blocking. Taken from my No Signal demo it is done like this:

    mov ah,1    ; non blocking key test
    int 16h
    jz keyr0
    sub ax,ax   ; blocking key test
    int 16h
    ; here handle the key press...
keyr0:          ; here continues your code

  • 使用PIT作为生成器播放声音

    我没有使用它,但是可以将 IIRC PIT 配置为使用扬声器播放恒定频率.因此,您无需设置扬声器的开/关状态,而只需设置每帧左右的频率即可....这将在后台播放,在这段时间内, CPU 可以执行其他操作...

    I did not use this but IIRC the PIT can be configured to play a constant frequency with the speaker. So instead of setting Speaker on/off you just set its frequency per each frame or so ... This will play in the background during which time the CPU can do other stuff...

    使用PIT作为计时器在后台播放声音

    要获得更好的计时并在与游戏代码并行的背景下播放声音,您可以将播放声音移至

    For much better timing and playing the sound in the background parallel to your game code you can move the sound playing to the PIT ISR entirely which can operate at 1193180.0/16bit_divider Hz so set the PIT to target sound sampling frequency. Set the ISR for it and fetch/play single bit in it....

    如果您使用 PWM 和采样频率的倍数,则可以将Speaker变成 DAC 甚至 PCM *.wav文件.

    If you use PWM and multiple of sampling frequency you can turn Speaker into DAC playing even PCM *.wav files.

    在这种情况下,您根本不会在游戏循环中插入声音代码.

    In this case you do not have the sound code interleaved in the game loop at all.

    不要忘记在退出前将坑频率设置回18.2 Hz ,因为 MS-DOS 和游戏使用它来进行计时.只需更改 PIT 频率即可加快许多游戏的速度.同样,当您更改PIT频率和ISR时,最好在新ISR中以18.2Hz的频率调用原始ISR.

    DO NOT FORGET TO SET PIT FREQUENCY BACK TO 18.2 Hz BEFORE EXIT because MS-DOS and games use it for timing. Many games can be speed-up by changing the PIT frequency alone. Also when you change PIT frequency and ISR it is a good idea to call the original ISR with 18.2Hz frequency from your new ISR.

    有关PC MS-DOS和游戏的更多信息,请参见:

    For more info about PC MS-DOS and games see:

    • PCGPE 1.0
    • WPCGPE 1.0 该格式应为*.hlp (您需要Win7 +的Windows6.1-KB917607补丁,因为他们不再支持该补丁)
    • PCGPE 1.0
    • WPCGPE 1.0 this should be *.hlp format (you need Windows6.1-KB917607 patch for Win7+ as they stopped supporting it)

    这是从我所知道的程序集开始的最佳选择:

    This is the best for starting with assembly I know of:

    • Assembler a ZX Spectrum 1
    • Assembler a ZX Spectrum 2

    但是它是捷克语中的 Z80 CPU ,我不知道任何翻译....

    but it is in Czech for Z80 CPU and I do not know of any translations ....

    这篇关于汇编播放音频文件TASM 16位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆