Skip to content

Commit e331ed7

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 e331ed7

File tree

6 files changed

+254
-170
lines changed

6 files changed

+254
-170
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

+39-15
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,61 @@ 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
6362
ifeq ($(UNAME_S),Linux)
63+
# Check ALSA installation
6464
ifeq (0, $(call check-alsa))
6565
$(warning No libasound installed. Check libasound in advance.)
6666
ENABLE_VIRTIOSND := 0
6767
endif
6868
endif
69+
ifeq ($(UNAME_S),Darwin)
70+
ifeq (0, $(call check-ca))
71+
$(warning No CoreAudio installed Check AudioToolbox in advance.)
72+
ENABLE_VIRTIOSND := 0
73+
endif
74+
endif
6975
$(call set-feature, VIRTIOSND)
7076
ifeq ($(call has, VIRTIOSND), 1)
7177
OBJS_EXTRA += virtio-snd.o
7278

73-
LDFLAGS += -lasound -lpthread
74-
CFLAGS += -Icnfa
79+
PORTAUDIOLIB := portaudio/lib/.libs/libportaudio.a
80+
LDFLAGS += $(PORTAUDIOLIB)
7581

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)
82+
ifeq ($(UNAME_S),Linux)
83+
LDFLAGS += -lasound -lrt
84+
# Check PulseAudio installation
85+
ifeq (0, $(call check-pa))
86+
LDFLAGS += -lpulse
87+
endif
88+
endif
89+
ifeq ($(UNAME_S),Darwin)
90+
LDFLAGS += -framework CoreServices -framework CoreFoundation -framework AudioUnit -framework AudioToolbox -framework CoreAudio
91+
endif
8492

85-
# suppress warning when compiling CNFA
86-
virtio-snd.o: CFLAGS += -Wno-unused-parameter -Wno-sign-compare
93+
CFLAGS += -Iportaudio/include
94+
# PortAudio requires libm, yet we set -lm in the end of LDFLAGS
95+
# so that the other libraries will be benefited for no need to set
96+
# -lm separately.
97+
LDFLAGS += -lpthread
98+
99+
portaudio/Makefile:
100+
git submodule update --init portaudio
101+
$(PORTAUDIOLIB): portaudio/Makefile
102+
@cd $(dir $<) && ./configure
103+
@cd $(dir $<) && $(MAKE)
104+
main.o: $(PORTAUDIOLIB)
105+
106+
# suppress warning when compiling PortAudio
107+
virtio-snd.o: CFLAGS += -Wno-unused-parameter
87108
endif
88109

110+
# Set libm as the last dependency so that no need to set -lm seperately.
111+
LDFLAGS += -lm
112+
89113
# .DEFAULT_GOAL should be set to all since the very first target is not all
90114
# after git submodule.
91115
.DEFAULT_GOAL := all

cnfa

-1
This file was deleted.

mk/check-libs.mk

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

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

portaudio

Submodule portaudio added at e97effb

0 commit comments

Comments
 (0)