Skip to content
This repository has been archived by the owner on Jun 15, 2018. It is now read-only.

Commit

Permalink
horrible first step in producing audio with SDL_mixer from a nodejs a…
Browse files Browse the repository at this point in the history
…ddon
  • Loading branch information
japj committed May 8, 2011
1 parent 517d3be commit 2e1f22a
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README
@@ -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.


60 changes: 60 additions & 0 deletions node-sdlmixer.cc
@@ -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... */


}
}
6 changes: 6 additions & 0 deletions test.js
@@ -0,0 +1,6 @@
var sdlmixer = require("./build/default/node-sdlmixer"),
sys = require("sys"),
puts = sys.puts;

puts(sdlmixer.hello);

Binary file added wavs/alarm.wav
Binary file not shown.
38 changes: 38 additions & 0 deletions wscript
@@ -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"

0 comments on commit 2e1f22a

Please sign in to comment.