Simple audio playback using ALSA

August 13th, 2009 Leave a comment Go to comments

Utilize ALSA to play an unbuffered raw WAV/PCM sample using x86 Linux assembler (NASM)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
; 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
VN:F [1.6.3_896]
Rating: +1 (from 1 vote)
Share and Enjoy:
  • Print this article!
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • LinkedIn
  • StumbleUpon
  • Twitter
  1. No comments yet.
  1. No trackbacks yet.