Skip to content

Switch to PortAudio #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ ForEachMacros:
- hlist_for_each_entry
- rb_list_foreach
- rb_list_foreach_safe
StatementAttributeLikeMacros:
- IIF
8 changes: 4 additions & 4 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[submodule "cnfa"]
path = cnfa
url = https://github.com/cntools/cnfa
shallow = true
[submodule "mini-gdbstub"]
path = mini-gdbstub
url = https://github.com/RinHizakura/mini-gdbstub
shallow = true
[submodule "portaudio"]
path = portaudio
url = https://github.com/PortAudio/portaudio
shallow = true
54 changes: 39 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ OBJS_EXTRA :=
# command line option
OPTS :=

LDFLAGS := -lm
LDFLAGS :=

# virtio-blk
ENABLE_VIRTIOBLK ?= 1
Expand Down Expand Up @@ -55,37 +55,61 @@ endif

# virtio-snd
ENABLE_VIRTIOSND ?= 1
ifneq ($(UNAME_S),$(filter $(UNAME_S),Linux))
ifneq ($(UNAME_S),$(filter $(UNAME_S),Linux Darwin))
ENABLE_VIRTIOSND := 0
endif

# Check ALSA installation
ifeq ($(UNAME_S),Linux)
# Check ALSA installation
ifeq (0, $(call check-alsa))
$(warning No libasound installed. Check libasound in advance.)
ENABLE_VIRTIOSND := 0
endif
endif
ifeq ($(UNAME_S),Darwin)
ifeq (0, $(call check-coreaudio))
$(warning No CoreAudio installed Check AudioToolbox in advance.)
ENABLE_VIRTIOSND := 0
endif
endif
$(call set-feature, VIRTIOSND)
ifeq ($(call has, VIRTIOSND), 1)
OBJS_EXTRA += virtio-snd.o

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

cnfa/Makefile:
git submodule update --init cnfa
cnfa/os_generic: cnfa/Makefile
$(MAKE) -C $(dir $<) os_generic.h
CNFA_LIB := cnfa/CNFA_sf.h
$(CNFA_LIB): cnfa/Makefile cnfa/os_generic
$(MAKE) -C $(dir $<) CNFA_sf.h
main.o: $(CNFA_LIB)
ifeq ($(UNAME_S),Linux)
LDFLAGS += -lasound -lrt
# Check PulseAudio installation
ifeq (0, $(call check-pa))
LDFLAGS += -lpulse
endif
endif
ifeq ($(UNAME_S),Darwin)
LDFLAGS += -framework CoreServices -framework CoreFoundation -framework AudioUnit -framework AudioToolbox -framework CoreAudio
endif

# suppress warning when compiling CNFA
virtio-snd.o: CFLAGS += -Wno-unused-parameter -Wno-sign-compare
CFLAGS += -Iportaudio/include
# PortAudio requires libm, yet we set -lm in the end of LDFLAGS
# so that the other libraries will be benefited for no need to set
# -lm separately.
LDFLAGS += -lpthread

portaudio/Makefile:
git submodule update --init portaudio
$(PORTAUDIOLIB): portaudio/Makefile
cd $(dir $<) && ./configure
$(MAKE) -C $(dir $<)
main.o: $(PORTAUDIOLIB)

# suppress warning when compiling PortAudio
virtio-snd.o: CFLAGS += -Wno-unused-parameter
endif

# Set libm as the last dependency so that no need to set -lm seperately.
LDFLAGS += -lm

# .DEFAULT_GOAL should be set to all since the very first target is not all
# after git submodule.
.DEFAULT_GOAL := all
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ A minimalist RISC-V system emulator capable of running Linux the kernel and corr
- Three types of I/O support using VirtIO standard:
- virtio-blk acquires disk image from the host.
- virtio-net is mapped as TAP interface.
- virtio-snd uses ALSA for sound operation with the following limitations:
- The emulator will hang if PulseAudio is enabled on host.
- virtio-snd uses PortAudio for sound operation with the following limitations:
- The emulator will crash after playing sound twice.
- The playback may plays with repeating artifact.

## Prerequisites
Expand Down
1 change: 0 additions & 1 deletion cnfa
Submodule cnfa deleted from 60bcdd
16 changes: 16 additions & 0 deletions common.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,19 @@ static inline int ilog2(int x)
#else /* unsupported compilers */
#define PACKED(name)
#endif

/* Pattern Matching for C macros.
* https://github.com/pfultz2/Cloak/wiki/C-Preprocessor-tricks,-tips,-and-idioms
*/

/* In Visual Studio, __VA_ARGS__ is treated as a separate parameter. */
#define FIX_VC_BUG(x) x

/* catenate */
#define PRIMITIVE_CAT(a, ...) FIX_VC_BUG(a##__VA_ARGS__)

#define IIF(c) PRIMITIVE_CAT(IIF_, c)
/* run the 2nd parameter */
#define IIF_0(t, ...) __VA_ARGS__
/* run the 1st parameter */
#define IIF_1(t, ...) t
1 change: 1 addition & 0 deletions configs/linux.config
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,7 @@ CONFIG_DUMMY_CONSOLE_ROWS=25
CONFIG_SOUND=y
CONFIG_SND=y
CONFIG_SND_VIRTIO=y
CONFIG_SND_HRTIMER=y

#
# HID support
Expand Down
33 changes: 33 additions & 0 deletions mk/check-libs.mk
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,41 @@ int main(){\n\
}\n'
endef

# Create a mininal PulseAudio program
define create-pa-prog
echo '\
#include <pulse/mainloop.h>\n\
int main(){\n\
pa_mainloop *m;\n\
return 0;\n\
}\n'
endef

# Create a mininal CoreAudio program
define create-ca-prog
echo '\
#include <CoreAudio/CoreAudio.h>\n\
#include <AudioToolbox/AudioQueue.h>
int main(){\n\
AudioQueueRef queue;\n\
AudioQueueDispose(queue, TRUE);\n\
return 0;\n\
}\n'
endef

# Check ALSA installation
define check-alsa
$(shell $(call create-alsa-prog) | $(CC) -x c -lasound -o /dev/null > /dev/null 2> /dev/null -
&& echo $$?)
endef

# Check PulseAudio installation
define check-pa
$(shell $(call create-pa-prog) | $(CC) -x c -lpulse -o /dev/null - && echo 0 || echo 1)
endef

# Check CoreAudio installation
define check-coreaudio
$(shell $(call create-ca-prog) | $(CC) -x c -framework AudioToolbox -o /dev/null > /dev/null 2> /dev/null -
&& echo $$?)
endef
1 change: 1 addition & 0 deletions portaudio
Submodule portaudio added at e97eff
Loading
Loading