All pastes #1515932 Raw Edit

Untitled

public text v1 · immutable
#1515932 ·published 2009-08-02 18:50 UTC
rendered paste body
; Simple ALSA playback system
; Author: Wesley Stessens (wesley@ubuntu.com)
;
; This ALSA playback system is meant to play only small chunks of PCM data!
; It doesn't use any buffering and is implemented in blocking-mode.
; Therefore it must run in a seperate thread.

extern snd_pcm_open, snd_pcm_hw_params_malloc, snd_pcm_hw_params_any, snd_pcm_hw_params_set_access
extern snd_pcm_hw_params_set_format, snd_pcm_hw_params_set_rate_near, snd_pcm_hw_params_set_channels
extern snd_pcm_hw_params, snd_pcm_hw_params_free
extern snd_pcm_prepare, snd_pcm_writei
extern snd_pcm_close

extern printf

SND_PCM_STREAM_PLAYBACK       equ 0
SND_PCM_FORMAT_S16_LE         equ 2
SND_PCM_ACCESS_RW_INTERLEAVED equ 3

section .data
    snd       incbin "raw_pcm_sample.raw"
    snd_len   equ ($ - snd) / 2
    device    db "default", 0

section .bss
    handle  resd 1
    params  resd 1
    rate    resd 1
    playing resb 1

section .text
initSound:
    push dword 0
    push dword SND_PCM_STREAM_PLAYBACK
    push device
    push handle
    call snd_pcm_open
    add esp, 16

    push params
    call snd_pcm_hw_params_malloc
    add esp, 4

    push dword [params]
    push dword [handle]
    call snd_pcm_hw_params_any
    add esp, 8

    push dword SND_PCM_ACCESS_RW_INTERLEAVED
    push dword [params]
    push dword [handle]
    call snd_pcm_hw_params_set_access
    add esp, 12

    push dword SND_PCM_FORMAT_S16_LE
    push dword [params]
    push dword [handle]
    call snd_pcm_hw_params_set_format
    add esp, 12

    mov dword [rate], 22050
    push dword 0
    push rate
    push dword [params]
    push dword [handle]
    call snd_pcm_hw_params_set_rate_near
    add esp, 16

    push dword 1
    push dword [params]
    push dword [handle]
    call snd_pcm_hw_params_set_channels
    add esp, 12

    push dword [params]
    push dword [handle]
    call snd_pcm_hw_params
    add esp, 8

    push dword [params]
    call snd_pcm_hw_params_free
    add esp, 4

    push dword [handle]
    call snd_pcm_prepare
    add esp, 4

    mov byte [playing], 0
    ret

playSound:
    cmp byte [playing], 1 ; Prevent multiple sounds from playing to eliminate errors
    je playSound
    mov byte [playing], 1
    push dword snd_len
    push snd
    push dword [handle]
    call snd_pcm_writei
    add esp, 12
    mov byte [playing], 0
    ret