This repository has been archived by the owner on Jun 15, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
horrible first step in producing audio with SDL_mixer from a nodejs a…
…ddon
- Loading branch information
Showing
5 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
node-sdlmixer experiment | ||
|
||
This is an experiment to get sdlmixer working together with nodejs. | ||
|
||
Note: since I am completely new to nodejs addon development, this | ||
is currently a very bad example of coding it. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#include <v8.h> | ||
#include "SDL.h" | ||
#include "SDL_mixer.h" | ||
|
||
using namespace v8; | ||
|
||
static int still_playing(void) | ||
{ | ||
return(Mix_Playing(0)); | ||
} | ||
|
||
|
||
extern "C" void | ||
init (Handle<Object> target) | ||
{ | ||
HandleScope scope; | ||
target->Set(String::New("hello"), String::New("world")); | ||
|
||
/* Initialize the SDL library */ | ||
if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) { | ||
fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); | ||
return ; | ||
} | ||
|
||
int audio_rate; | ||
Uint16 audio_format; | ||
int audio_channels; | ||
Mix_Chunk *wave = NULL; | ||
|
||
|
||
/* Initialize variables */ | ||
audio_rate = MIX_DEFAULT_FREQUENCY; | ||
audio_format = MIX_DEFAULT_FORMAT; | ||
audio_channels = 2; | ||
|
||
/* Open the audio device */ | ||
if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, 4096) < 0) { | ||
fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError()); | ||
} | ||
else { | ||
Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels); | ||
printf("Opened audio at %d Hz %d bit %s\n", audio_rate, | ||
(audio_format&0xFF), | ||
(audio_channels > 2) ? "surround" : | ||
(audio_channels > 1) ? "stereo" : "mono"); | ||
|
||
/* Load the requested wave file */ | ||
wave = Mix_LoadWAV("./wavs/alarm.wav"); | ||
|
||
/* Play and then exit */ | ||
Mix_PlayChannel(0, wave, 0); | ||
|
||
while (still_playing()) { | ||
SDL_Delay(1); | ||
|
||
} /* while still_playing() loop... */ | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
var sdlmixer = require("./build/default/node-sdlmixer"), | ||
sys = require("sys"), | ||
puts = sys.puts; | ||
|
||
puts(sdlmixer.hello); | ||
|
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from os import popen | ||
|
||
srcdir = '.' | ||
blddir = 'build' | ||
VERSION = '0.0.1' | ||
|
||
def set_options(opt): | ||
opt.tool_options('compiler_cxx') | ||
|
||
def configure(conf): | ||
conf.check_tool('compiler_cxx') | ||
conf.check_tool('node_addon') | ||
|
||
sdl_config = conf.find_program('sdl-config', var='SDL_CONFIG', mandatory=True) | ||
sdl_libs = popen("%s --libs" % sdl_config).readline().strip() | ||
sdl_cflags = popen("%s --cflags" % sdl_config).readline().strip() | ||
|
||
sdl_addpaths = [] | ||
sdl_addlibs = [] | ||
for item in sdl_libs.split(' '): | ||
# -L items are lib paths, -l are additional libraries | ||
if item.find("-L") == 0: | ||
sdl_addpaths.append(item[2:]) | ||
if item.find("-l") == 0: | ||
sdl_addlibs.append(item[2:]) | ||
|
||
sdl_addlibs.append("SDL_mixer") | ||
|
||
conf.env.append_value("LIBPATH_SDL", sdl_addpaths) | ||
conf.env.append_value("LIB_SDL", sdl_addlibs) | ||
|
||
conf.env.append_value("CPPFLAGS_SDL", sdl_cflags.split(' ')) | ||
|
||
def build(bld): | ||
obj = bld.new_task_gen('cxx', 'shlib', 'node_addon') | ||
obj.target = 'node-sdlmixer' | ||
obj.source = 'node-sdlmixer.cc' | ||
obj.uselib = "SDL" |