Skip to content

Commit 6e00d27

Browse files
committed
[WIP] Switch to PortAudio
Switch to PortAudio for cross-platform and cross backend compatibility. Yet, there are two known limitations for current sound playback implementation: 1. The last period of sound is lost. 2. You cannot play sound twice or the device will crash thus whole system crashes.
1 parent b42df15 commit 6e00d27

File tree

6 files changed

+263
-171
lines changed

6 files changed

+263
-171
lines changed

.gitmodules

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
[submodule "cnfa"]
2-
path = cnfa
3-
url = https://github.com./cntools/cnfa
4-
shallow = true
51
[submodule "mini-gdbstub"]
62
path = mini-gdbstub
73
url = https://github.com./RinHizakura/mini-gdbstub
84
shallow = true
5+
[submodule "portaudio"]
6+
path = portaudio
7+
url = https://github.com./PortAudio/portaudio
8+
shallow = true

Makefile

+46-16
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ OBJS_EXTRA :=
1414
# command line option
1515
OPTS :=
1616

17-
LDFLAGS := -lm
17+
LDFLAGS :=
1818

1919
# virtio-blk
2020
ENABLE_VIRTIOBLK ?= 1
@@ -55,37 +55,67 @@ endif
5555

5656
# virtio-snd
5757
ENABLE_VIRTIOSND ?= 1
58-
ifneq ($(UNAME_S),$(filter $(UNAME_S),Linux))
58+
ifneq ($(UNAME_S),$(filter $(UNAME_S),Linux Darwin))
5959
ENABLE_VIRTIOSND := 0
6060
endif
6161

62-
# Check ALSA installation
62+
ENABLE_PULSEAUDIO ?= 1
6363
ifeq ($(UNAME_S),Linux)
64+
# Check ALSA installation
6465
ifeq (0, $(call check-alsa))
6566
$(warning No libasound installed. Check libasound in advance.)
6667
ENABLE_VIRTIOSND := 0
6768
endif
69+
70+
# Check PulseAudio installation
71+
ifeq (0, $(call check-pa))
72+
$(warning No PulseAudio installed.)
73+
ENABLE_PULSEAUDIO := 0
74+
endif
75+
endif
76+
ifeq ($(UNAME_S),Darwin)
77+
ifeq (0, $(call check-ca))
78+
$(warning No CoreAudio installed Check AudioToolbox in advance.)
79+
ENABLE_VIRTIOSND := 0
80+
endif
6881
endif
6982
$(call set-feature, VIRTIOSND)
7083
ifeq ($(call has, VIRTIOSND), 1)
7184
OBJS_EXTRA += virtio-snd.o
7285

73-
LDFLAGS += -lasound -lpthread
74-
CFLAGS += -Icnfa
75-
76-
cnfa/Makefile:
77-
git submodule update --init cnfa
78-
cnfa/os_generic: cnfa/Makefile
79-
$(MAKE) -C $(dir $<) os_generic.h
80-
CNFA_LIB := cnfa/CNFA_sf.h
81-
$(CNFA_LIB): cnfa/Makefile cnfa/os_generic
82-
$(MAKE) -C $(dir $<) CNFA_sf.h
83-
main.o: $(CNFA_LIB)
86+
PORTAUDIOLIB := portaudio/lib/.libs/libportaudio.a
87+
LDFLAGS += $(PORTAUDIOLIB)
8488

85-
# suppress warning when compiling CNFA
86-
virtio-snd.o: CFLAGS += -Wno-unused-parameter -Wno-sign-compare
89+
ifeq ($(UNAME_S),Linux)
90+
LDFLAGS += -lasound -lrt
91+
ifeq (1, $(ENABLE_PULSEAUDIO))
92+
LDFLAGS += -lpulse
93+
endif
94+
endif
95+
ifeq ($(UNAME_S),Darwin)
96+
LDFLAGS += -framework CoreServices -framework CoreFoundation -framework AudioUnit -framework AudioToolbox -framework CoreAudio
97+
endif
98+
99+
CFLAGS += -Iportaudio/include
100+
# PortAudio requires libm, yet we set -lm in the end of LDFLAGS
101+
# so that the other libraries will be benefited for no need to set
102+
# -lm separately.
103+
LDFLAGS += -lpthread
104+
105+
portaudio/Makefile:
106+
git submodule update --init portaudio
107+
$(PORTAUDIOLIB): portaudio/Makefile
108+
@cd $(dir $<) && ./configure
109+
@cd $(dir $<) && $(MAKE)
110+
$(OBJS): $(PORTAUDIOLIB)
111+
112+
# suppress warning when compiling PortAudio
113+
virtio-snd.o: CFLAGS += -Wno-unused-parameter
87114
endif
88115

116+
# Set libm as the last dependency so that no need to set -lm seperately.
117+
LDFLAGS += -lm
118+
89119
# .DEFAULT_GOAL should be set to all since the very first target is not all
90120
# after git submodule.
91121
.DEFAULT_GOAL := all

cnfa

-1
This file was deleted.

mk/check-libs.mk

+35
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,43 @@ int main(){\n\
1010
}\n'
1111
endef
1212

13+
# Create a mininal PulseAudio program
14+
define create-pa-prog
15+
echo '\
16+
#include <pulse/pulseaudio.h>\n\
17+
int main(){\n\
18+
pa_mainloop *m = NULL;\n\
19+
pa_mainloop_free(m);\n\
20+
return 0;\n\
21+
}\n'
22+
endef
23+
24+
# Create a mininal CoreAudio program
25+
define create-ca-prog
26+
echo '\
27+
#include <CoreAudio/CoreAudio.h>\n\
28+
#include <AudioToolbox/AudioQueue.h>
29+
int main(){\n\
30+
AudioQueueRef queue;\n\
31+
AudioQueueDispose(queue, TRUE);\n\
32+
return 0;\n\
33+
}\n'
34+
endef
35+
1336
# Check ALSA installation
1437
define check-alsa
1538
$(shell $(call create-alsa-prog) | $(CC) -x c -lasound -o /dev/null > /dev/null 2> /dev/null -
1639
&& echo $$?)
1740
endef
41+
42+
# Check PulseAudio installation
43+
define check-pa
44+
$(shell $(call create-pa-prog) | $(CC) -x c -lpulse -o /dev/null > /dev/null 2> /dev/null -
45+
&& echo $$?)
46+
endef
47+
48+
# Check CoreAudio installation
49+
define check-ca
50+
$(shell $(call create-ca-prog) | $(CC) -x c -framework AudioToolbox -o /dev/null > /dev/null 2> /dev/null -
51+
&& echo $$?)
52+
endef

portaudio

Submodule portaudio added at e97effb

0 commit comments

Comments
 (0)