update
This commit is contained in:
parent
625a2b1d12
commit
ff97a38a1e
13 changed files with 215 additions and 37 deletions
|
|
@ -3,30 +3,61 @@ project(
|
|||
mdelibs
|
||||
)
|
||||
|
||||
file(
|
||||
GLOB
|
||||
MDELib_SRC
|
||||
src/*.c
|
||||
)
|
||||
|
||||
add_library(
|
||||
MDELib
|
||||
SHARED
|
||||
${MDELib_SRC}
|
||||
)
|
||||
|
||||
target_include_directories(
|
||||
MDELib
|
||||
PUBLIC
|
||||
include
|
||||
)
|
||||
|
||||
include(GNUInstallDirs)
|
||||
install(
|
||||
TARGETS MDELib
|
||||
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
)
|
||||
macro(setup_lib dir name)
|
||||
file(
|
||||
GLOB
|
||||
MDE${name}_SRC
|
||||
src/${dir}/*.c
|
||||
)
|
||||
|
||||
add_library(
|
||||
MDE${name}
|
||||
SHARED
|
||||
${MDE${name}_SRC}
|
||||
)
|
||||
|
||||
target_include_directories(
|
||||
MDE${name}
|
||||
PUBLIC
|
||||
include
|
||||
)
|
||||
|
||||
install(
|
||||
TARGETS MDE${name}
|
||||
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
)
|
||||
endmacro()
|
||||
|
||||
setup_lib(core Core)
|
||||
setup_lib(audio Audio)
|
||||
|
||||
find_package(PkgConfig)
|
||||
pkg_check_modules(ALSA alsa)
|
||||
if(ALSA_FOUND)
|
||||
target_include_directories(
|
||||
MDEAudio
|
||||
PRIVATE
|
||||
${ALSA_INCLUDE_DIRS}
|
||||
)
|
||||
target_link_directories(
|
||||
MDEAudio
|
||||
PRIVATE
|
||||
${ALSA_LIBRARY_DIRS}
|
||||
)
|
||||
target_link_libraries(
|
||||
MDEAudio
|
||||
PRIVATE
|
||||
${ALSA_LIBRARIES} pthread
|
||||
)
|
||||
target_sources(
|
||||
MDEAudio
|
||||
PRIVATE
|
||||
src/audio/driver/alsa.c
|
||||
)
|
||||
endif()
|
||||
|
||||
install(
|
||||
DIRECTORY include/
|
||||
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
|
||||
|
|
|
|||
29
include/MDE/Audio/Audio.h
Normal file
29
include/MDE/Audio/Audio.h
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#ifndef __MDE_AUDIO_H__
|
||||
#define __MDE_AUDIO_H__
|
||||
|
||||
#include <MDE/MachDep.h>
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* always stereo, btw */
|
||||
#define MDEAudioRate 48000
|
||||
|
||||
/* S16, stereo. frames*2*2 == bytes */
|
||||
typedef void(*MDEAudioHandler)(void* handle, void* user, void* data, int frames);
|
||||
|
||||
/* driver functions */
|
||||
void* MDEAudioOpen(MDEAudioHandler handler, void* user);
|
||||
|
||||
void MDEAudioClose(void* handle);
|
||||
|
||||
void MDEAudioStart(void* handle);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
|
@ -3,9 +3,11 @@
|
|||
|
||||
#include <MDE/MachDep.h>
|
||||
|
||||
#include <MDE/File.h>
|
||||
#include <MDE/String.h>
|
||||
#include <MDE/Users.h>
|
||||
#include <MDE/Directory.h>
|
||||
#include <MDE/Core/File.h>
|
||||
#include <MDE/Core/String.h>
|
||||
#include <MDE/Core/Users.h>
|
||||
#include <MDE/Core/Directory.h>
|
||||
|
||||
#include <MDE/Audio/Audio.h>
|
||||
|
||||
#endif
|
||||
|
|
|
|||
123
src/audio/driver/alsa.c
Normal file
123
src/audio/driver/alsa.c
Normal file
|
|
@ -0,0 +1,123 @@
|
|||
#include <MDE/Audio/Audio.h>
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
#include <alsa/asoundlib.h>
|
||||
|
||||
typedef struct driver {
|
||||
MDEAudioHandler handler;
|
||||
void* user;
|
||||
snd_pcm_t* device;
|
||||
snd_pcm_hw_params_t* params;
|
||||
|
||||
int quit;
|
||||
pthread_t thread;
|
||||
pthread_mutex_t mutex;
|
||||
} driver_t;
|
||||
|
||||
static void* thread_routine(void* arg){
|
||||
driver_t* drv = arg;
|
||||
snd_pcm_uframes_t frame;
|
||||
void* data;
|
||||
|
||||
snd_pcm_hw_params_get_period_size(drv->params, &frame, 0);
|
||||
|
||||
data = malloc(frame * 2 * 2);
|
||||
|
||||
while(1){
|
||||
drv->handler(drv, drv->user, data, frame);
|
||||
if(snd_pcm_writei(drv->device, data, frame) == -EPIPE){
|
||||
snd_pcm_prepare(drv->device);
|
||||
}
|
||||
|
||||
pthread_mutex_lock(&drv->mutex);
|
||||
if(drv->quit){
|
||||
pthread_mutex_unlock(&drv->mutex);
|
||||
break;
|
||||
}
|
||||
pthread_mutex_unlock(&drv->mutex);
|
||||
}
|
||||
|
||||
free(data);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void* MDEAudioOpen(MDEAudioHandler handler, void* user){
|
||||
int rate = MDEAudioRate;
|
||||
driver_t* drv = malloc(sizeof(*drv));
|
||||
memset(drv, 0, sizeof(*drv));
|
||||
|
||||
drv->quit = 0;
|
||||
drv->handler = handler;
|
||||
drv->user = user;
|
||||
|
||||
if(snd_pcm_open(&drv->device, "default", SND_PCM_STREAM_PLAYBACK, 0) < 0){
|
||||
MDEAudioClose(drv);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
snd_pcm_hw_params_malloc(&drv->params);
|
||||
snd_pcm_hw_params_any(drv->device, drv->params);
|
||||
|
||||
if(snd_pcm_hw_params_set_access(drv->device, drv->params, SND_PCM_ACCESS_RW_INTERLEAVED) < 0){
|
||||
MDEAudioClose(drv);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(snd_pcm_hw_params_set_format(drv->device, drv->params, SND_PCM_FORMAT_S16) < 0){
|
||||
MDEAudioClose(drv);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(snd_pcm_hw_params_set_channels(drv->device, drv->params, 2) < 0){
|
||||
MDEAudioClose(drv);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(snd_pcm_hw_params_set_rate_near(drv->device, drv->params, &rate, 0) < 0){
|
||||
MDEAudioClose(drv);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(snd_pcm_hw_params_set_rate(drv->device, drv->params, rate, 0) < 0){
|
||||
MDEAudioClose(drv);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(snd_pcm_hw_params(drv->device, drv->params) < 0){
|
||||
MDEAudioClose(drv);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
pthread_mutex_init(&drv->mutex, NULL);
|
||||
|
||||
return drv;
|
||||
}
|
||||
|
||||
void MDEAudioClose(void* handle){
|
||||
driver_t* drv = handle;
|
||||
|
||||
if(drv->device != NULL){
|
||||
void* ret;
|
||||
|
||||
pthread_mutex_lock(&drv->mutex);
|
||||
drv->quit = 1;
|
||||
pthread_mutex_unlock(&drv->mutex);
|
||||
pthread_join(drv->thread, &ret);
|
||||
|
||||
pthread_mutex_destroy(&drv->mutex);
|
||||
|
||||
snd_pcm_drain(drv->device);
|
||||
snd_pcm_close(drv->device);
|
||||
snd_pcm_hw_params_free(drv->params);
|
||||
}
|
||||
|
||||
free(drv);
|
||||
}
|
||||
|
||||
void MDEAudioStart(void* handle){
|
||||
driver_t* drv = handle;
|
||||
|
||||
pthread_create(&drv->thread, NULL, thread_routine, handle);
|
||||
}
|
||||
0
src/audio/mixer.c
Normal file
0
src/audio/mixer.c
Normal file
|
|
@ -1,7 +1,5 @@
|
|||
#include <MDE/Directory.h>
|
||||
#include <MDE/Core/Directory.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <dirent.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
|
|
@ -1,6 +1,4 @@
|
|||
#include <MDE/File.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <MDE/Core/File.h>
|
||||
|
||||
void MDEFileCopy(const char* src, const char* dst) {
|
||||
char buffer[4096];
|
||||
|
|
@ -1,7 +1,4 @@
|
|||
#include <MDE/String.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <MDE/Core/String.h>
|
||||
|
||||
char* MDEStringDuplicate(const char* src) {
|
||||
char* s = malloc(strlen(src) + 1);
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
#include <MDE/Users.h>
|
||||
#include <MDE/String.h>
|
||||
#include <MDE/Core/Users.h>
|
||||
#include <MDE/Core/String.h>
|
||||
|
||||
#include <pwd.h>
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue