Skip to content

Commit

Permalink
Configure audio support at runtime - fixes #372
Browse files Browse the repository at this point in the history
Signed-off-by: Benn Snyder <benn.snyder@gmail.com>
  • Loading branch information
piedar committed Jul 19, 2014
1 parent fb2b056 commit 80f7423
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 124 deletions.
11 changes: 2 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ set (PROJECT_VER
set (PROJECT_APIVER
"${PROJECT_VER_MAJOR}.${PROJECT_VER_MINOR}")

OPTION(BUILD_AUDIO "Build audio support" OFF)
OPTION(BUILD_REDIST_PACKAGE "Build libfreenect in a legally-redistributable manner (only affects audio)" OFF)
OPTION(BUILD_REDIST_PACKAGE "Build libfreenect in a legally-redistributable manner (only affects audio)" ON)
OPTION(BUILD_EXAMPLES "Build example programs" ON)
OPTION(BUILD_FAKENECT "Build fakenect mock library" ON)
OPTION(BUILD_C_SYNC "Build c synchronous library" ON)
Expand Down Expand Up @@ -84,10 +83,6 @@ if(BIG_ENDIAN)
add_definitions(-DFN_BIGENDIAN)
endif()

if(BUILD_AUDIO)
add_definitions(-DBUILD_AUDIO)
endif()

if (WIN32)
set(MATH_LIB "")
else(WIN32)
Expand Down Expand Up @@ -217,11 +212,9 @@ IF ( BUILD_CPACK_TGZ OR BUILD_CPACK_DEB OR BUILD_CPACK_RPM )
include(CPack)

INSTALL(FILES "${CMAKE_BINARY_DIR}/lib/libfreenect.a" DESTINATION ${PROJECT_LIBRARY_INSTALL_DIR})
if (BUILD_AUDIO)
INSTALL(FILES "include/libfreenect_audio.h" DESTINATION ${PROJECT_INCLUDE_INSTALL_DIR})
endif()
INSTALL(FILES "include/libfreenect.h" DESTINATION ${PROJECT_INCLUDE_INSTALL_DIR})
INSTALL(FILES "include/libfreenect_registration.h" DESTINATION ${PROJECT_INCLUDE_INSTALL_DIR})
INSTALL(FILES "include/libfreenect_audio.h" DESTINATION ${PROJECT_INCLUDE_INSTALL_DIR})
INSTALL(FILES "APACHE20" DESTINATION "share/doc/${CPACK_PACKAGE_NAME}")
INSTALL(FILES "GPL2" DESTINATION "share/doc/${CPACK_PACKAGE_NAME}")
INSTALL(FILES "README.md" DESTINATION "share/doc/${CPACK_PACKAGE_NAME}")
Expand Down
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ It runs on Linux, OSX, and Windows and supports
- Motors
- Accelerometer
- LED

Audio is a work in progress.
- Audio


# Build Instructions
Expand All @@ -18,14 +17,20 @@ To build libfreenect, you'll need

- [libusb](http://libusb.info) >= 1.0.18
- [CMake](http://cmake.org) >= 2.6
- [python](http://python.org) == 2.* (only if BUILD_AUDIO or BUILD_PYTHON)
- [python](http://python.org) == 2.* (only if BUILD_PYTHON=ON or BUILD_REDIST_PACKAGE=OFF)

For the examples, you'll need

- OpenGL (included with OSX)
- glut (included with OSX)
- [pthreads-win32](http://sourceforge.net/projects/pthreads4w/) (Windows)

For audio support, you must have firmware to upload to the Kinect.
If you specify a non-redistributable package, firmware will be downloaded automatically:

cmake -L .. -DBUILD_REDIST_PACKAGE=OFF

Note that the downloaded firmware may not be legal to redistribute!

## <a name="fetch-build"></a>Fetch & Build

Expand All @@ -39,10 +44,6 @@ For the examples, you'll need
# if you don't have `make` or don't want color output
# cmake --build .

For some newer Kinect models, audio must be enabled for tilt and LED control:

cmake -L .. -DBUILD_AUDIO=ON

You can specify a build with debug symbols:

cmake -L .. -DCMAKE_BUILD_TYPE=debug
Expand Down
26 changes: 7 additions & 19 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@ add_executable(freenect-glview glview.c)
add_executable(freenect-regview regview.c)
add_executable(freenect-hiview hiview.c)
add_executable(freenect-chunkview chunkview.c)

if(BUILD_AUDIO)
add_executable(freenect-wavrecord wavrecord.c)
add_executable(freenect-micview micview.c)
endif()
add_executable(freenect-wavrecord wavrecord.c)
add_executable(freenect-micview micview.c)

if (BUILD_C_SYNC)
add_executable(freenect-glpclview glpclview.c)
Expand All @@ -44,10 +41,8 @@ if(APPLE)
target_link_libraries(freenect-regview freenect)
target_link_libraries(freenect-hiview freenect)
target_link_libraries(freenect-chunkview freenect)
if (BUILD_AUDIO)
target_link_libraries(freenect-wavrecord freenect)
target_link_libraries(freenect-micview freenect)
endif()
target_link_libraries(freenect-wavrecord freenect)
target_link_libraries(freenect-micview freenect)
if (BUILD_C_SYNC)
target_link_libraries(freenect-glpclview freenect_sync)
target_link_libraries(freenect-tiltdemo freenect_sync)
Expand All @@ -66,10 +61,8 @@ else()
target_link_libraries(freenect-regview freenect ${OPENGL_LIBRARIES} ${GLUT_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} ${MATH_LIB})
target_link_libraries(freenect-hiview freenect ${OPENGL_LIBRARIES} ${GLUT_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} ${MATH_LIB})
target_link_libraries(freenect-chunkview freenect ${OPENGL_LIBRARIES} ${GLUT_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} ${MATH_LIB})
if (BUILD_AUDIO)
target_link_libraries(freenect-wavrecord freenect ${OPENGL_LIBRARIES} ${GLUT_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} ${MATH_LIB})
target_link_libraries(freenect-micview freenect ${OPENGL_LIBRARIES} ${GLUT_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} ${MATH_LIB})
endif()
target_link_libraries(freenect-wavrecord freenect ${OPENGL_LIBRARIES} ${GLUT_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} ${MATH_LIB})
target_link_libraries(freenect-micview freenect ${OPENGL_LIBRARIES} ${GLUT_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} ${MATH_LIB})
if (BUILD_C_SYNC)
target_link_libraries(freenect-glpclview freenect_sync ${OPENGL_LIBRARIES} ${GLUT_LIBRARY}
${CMAKE_THREAD_LIBS_INIT} ${MATH_LIB})
Expand All @@ -78,15 +71,10 @@ else()
endif()
endif()

install (TARGETS freenect-glview freenect-regview freenect-hiview freenect-chunkview
install (TARGETS freenect-glview freenect-regview freenect-hiview freenect-chunkview freenect-wavrecord freenect-micview
DESTINATION bin)

if (BUILD_C_SYNC)
install (TARGETS freenect-glpclview freenect-tiltdemo
DESTINATION bin)
endif()

if (BUILD_AUDIO)
install (TARGETS freenect-wavrecord DESTINATION bin)
install (TARGETS freenect-micview DESTINATION bin)
endif()
33 changes: 14 additions & 19 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,19 @@ IF(WIN32)
set_source_files_properties(${SRC} PROPERTIES LANGUAGE CXX)
ENDIF(WIN32)

IF(BUILD_AUDIO)
include(FindPythonInterp)
LIST(APPEND SRC audio.c loader.c)
IF(BUILD_REDIST_PACKAGE)
# If this build is intended for a redistributable package, we can't include audios.bin, so we should include fwfetcher.py
# and the package should run "python fwfetcher.py $INSTALL_PREFIX/share" as a postinst hook
install (FILES "fwfetcher.py" DESTINATION "${CMAKE_INSTALL_PREFIX}/share")
ELSE(BUILD_REDIST_PACKAGE)
# If the install is local only, we can just run fwfetcher.py and install the audios.bin firmware to the system folder
add_custom_target(firmware ALL
COMMAND ${PYTHON_EXECUTABLE} "${CMAKE_CURRENT_SOURCE_DIR}/fwfetcher.py" "../audios.bin"
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/fwfetcher.py")
install (FILES "${CMAKE_CURRENT_BINARY_DIR}/../audios.bin" DESTINATION "${CMAKE_INSTALL_PREFIX}/share/libfreenect")
ENDIF()
# Audio
include(FindPythonInterp)
LIST(APPEND SRC audio.c loader.c)
IF(BUILD_REDIST_PACKAGE)
# If this build is intended for a redistributable package, we can't include audios.bin, so we should include fwfetcher.py
# and the package should run "python fwfetcher.py $INSTALL_PREFIX/share" as a postinst hook
install (FILES "fwfetcher.py" DESTINATION "${CMAKE_INSTALL_PREFIX}/share")
ELSE(BUILD_REDIST_PACKAGE)
# If the install is local only, we can just run fwfetcher.py and install the audios.bin firmware to the system folder
add_custom_target(firmware ALL
COMMAND ${PYTHON_EXECUTABLE} "${CMAKE_CURRENT_SOURCE_DIR}/fwfetcher.py" "../audios.bin"
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/fwfetcher.py")
install (FILES "${CMAKE_CURRENT_BINARY_DIR}/../audios.bin" DESTINATION "${CMAKE_INSTALL_PREFIX}/share/libfreenect")
ENDIF()

add_library (freenect SHARED ${SRC})
Expand All @@ -45,12 +44,8 @@ target_link_libraries (freenect ${LIBUSB_1_LIBRARIES})
target_link_libraries (freenectstatic ${LIBUSB_1_LIBRARIES})

# Install the header files
install (FILES "../include/libfreenect.h" "../include/libfreenect_registration.h"
install (FILES "../include/libfreenect.h" "../include/libfreenect_registration.h" "../include/libfreenect_audio.h"
DESTINATION ${PROJECT_INCLUDE_INSTALL_DIR})
if(BUILD_AUDIO)
install (FILES "../include/libfreenect_audio.h"
DESTINATION ${PROJECT_INCLUDE_INSTALL_DIR})
endif()

IF(UNIX AND NOT APPLE)
# Produce a pkg-config file for linking against the shared lib
Expand Down
3 changes: 0 additions & 3 deletions src/audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#include <string.h>
#include <stdlib.h>

#ifdef BUILD_AUDIO

static void prepare_iso_out_data(freenect_device* dev, uint8_t* buffer) {
audio_stream* stream = &dev->audio;
Expand Down Expand Up @@ -239,5 +238,3 @@ int freenect_stop_audio(freenect_device* dev) {

return ret;
}

#endif
30 changes: 9 additions & 21 deletions src/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@
#include "freenect_internal.h"
#include "registration.h"
#include "cameras.h"
#ifdef BUILD_AUDIO
#include "loader.h"
#endif


FREENECTAPI int freenect_init(freenect_context **ctx, freenect_usb_context *usb_ctx)
{
Expand All @@ -49,11 +48,7 @@ FREENECTAPI int freenect_init(freenect_context **ctx, freenect_usb_context *usb_
memset(*ctx, 0, sizeof(freenect_context));

(*ctx)->log_level = LL_WARNING;
(*ctx)->enabled_subdevices = (freenect_device_flags)(FREENECT_DEVICE_MOTOR | FREENECT_DEVICE_CAMERA
#ifdef BUILD_AUDIO
| FREENECT_DEVICE_AUDIO
#endif
);
(*ctx)->enabled_subdevices = (freenect_device_flags)(FREENECT_DEVICE_MOTOR | FREENECT_DEVICE_CAMERA | FREENECT_DEVICE_AUDIO);
res = fnusb_init(&(*ctx)->usb, usb_ctx);
if (res < 0) {
free(*ctx);
Expand Down Expand Up @@ -94,13 +89,11 @@ FREENECTAPI int freenect_process_events_timeout(freenect_context *ctx, struct ti
freenect_stop_video(dev);
freenect_stop_depth(dev);
}
#ifdef BUILD_AUDIO
if (dev->usb_audio.device_dead) {
FN_ERROR("USB audio marked dead, stopping streams\n");
res = -1; // Or something else to tell the user that the device just vanished.
freenect_stop_audio(dev);
}
#endif
dev = dev->next;
}
return res;
Expand Down Expand Up @@ -132,23 +125,18 @@ FREENECTAPI void freenect_free_device_attributes(struct freenect_device_attribut
return;
}

FREENECTAPI int freenect_supported_subdevices(void) {
#ifdef BUILD_AUDIO
FREENECTAPI int freenect_supported_subdevices(void)
{
return FREENECT_DEVICE_MOTOR | FREENECT_DEVICE_CAMERA | FREENECT_DEVICE_AUDIO;
#else
return FREENECT_DEVICE_MOTOR | FREENECT_DEVICE_CAMERA;
#endif
}

FREENECTAPI void freenect_select_subdevices(freenect_context *ctx, freenect_device_flags subdevs) {
ctx->enabled_subdevices = (freenect_device_flags)(subdevs & (FREENECT_DEVICE_MOTOR | FREENECT_DEVICE_CAMERA
#ifdef BUILD_AUDIO
| FREENECT_DEVICE_AUDIO
#endif
));
FREENECTAPI void freenect_select_subdevices(freenect_context *ctx, freenect_device_flags subdevs)
{
ctx->enabled_subdevices = (freenect_device_flags)(subdevs & (FREENECT_DEVICE_MOTOR | FREENECT_DEVICE_CAMERA | FREENECT_DEVICE_AUDIO));
}

FREENECTAPI freenect_device_flags freenect_enabled_subdevices(freenect_context *ctx) {
FREENECTAPI freenect_device_flags freenect_enabled_subdevices(freenect_context *ctx)
{
return ctx->enabled_subdevices;
}

Expand Down
11 changes: 2 additions & 9 deletions src/freenect_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@

#include "libfreenect.h"
#include "libfreenect_registration.h"

#ifdef BUILD_AUDIO
#include "libfreenect_audio.h"
#endif
#include "libfreenect_audio.h"

#ifdef __ELF__
#define FN_INTERNAL __attribute__ ((visibility ("hidden")))
Expand Down Expand Up @@ -174,7 +171,6 @@ typedef struct {
void *proc_buf;
} packet_stream;

#ifdef BUILD_AUDIO
typedef struct {
int running;

Expand Down Expand Up @@ -214,8 +210,6 @@ typedef struct {
freenect_sample_51 samples[6]; // Audio samples - 6 samples per transfer
} audio_out_block;

#endif

struct _freenect_device {
freenect_context *parent;
freenect_device *next;
Expand Down Expand Up @@ -244,7 +238,6 @@ struct _freenect_device {
// Registration
freenect_registration registration;

#ifdef BUILD_AUDIO
// Audio
fnusb_dev usb_audio;
fnusb_isoc_stream audio_out_isoc;
Expand All @@ -255,7 +248,7 @@ struct _freenect_device {

audio_stream audio;
uint32_t audio_tag;
#endif

// Motor
fnusb_dev usb_motor;
freenect_raw_tilt_state raw_state;
Expand Down
4 changes: 0 additions & 4 deletions src/loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
#include <stdio.h>
#include <stdlib.h>

#ifdef BUILD_AUDIO

static void dump_bl_cmd(freenect_context* ctx, bootloader_command cmd) {
int i;
Expand Down Expand Up @@ -418,6 +417,3 @@ FN_INTERNAL int upload_cemd_data(fnusb_dev* dev) {
FN_INFO("CEMD data uploaded successfully.\n");
return 0;
}

#endif

Loading

0 comments on commit 80f7423

Please sign in to comment.