Skip to content

Commit

Permalink
a few udpates for the mac
Browse files Browse the repository at this point in the history
1. initialize mutex and localize use of trylock/unlock
2. fix callback arguments in simpletest.js
3. add basics.js as the skeleton for a simple streamer of system output
(it should go elsewhere, but i suspect we'll be testing with this for a
while)...
  • Loading branch information
mrose17 committed May 8, 2013
1 parent 5c6032f commit 0cf9381
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .gitignore
@@ -1,8 +1,9 @@
build/
Debug/
Release/
ipch/
Test/
node_modules/
NodeCoreAudio.suo
NodeCoreAudio.opensdf
NodeCoreAudio.sdf
NodeCoreAudio.sdf
13 changes: 6 additions & 7 deletions NodeCoreAudio/AudioEngine.cpp
Expand Up @@ -230,8 +230,6 @@ void Audio::AudioEngine::processAudioCallback( uv_work_t* request, int status )

pEngine->GetIsolate()->Exit();

uv_mutex_unlock( &pEngine->m_mutex );

} // end AudioEngine::processAudioCallback()


Expand Down Expand Up @@ -380,6 +378,7 @@ void* Audio::AudioEngine::runAudioLoop( void* data ){

assert( pEngine->m_pPaStream );

uv_mutex_init( &pEngine->m_mutex );
while( true ) {
error = Pa_ReadStream( pEngine->m_pPaStream, pEngine->m_cachedInputSampleBlock, pEngine->m_uSamplesPerBuffer );

Expand All @@ -388,11 +387,11 @@ void* Audio::AudioEngine::runAudioLoop( void* data ){
uv_work_t* request = new uv_work_t;
request->data = pEngine;

uv_mutex_trylock( &pEngine->m_mutex );

int statusInt = 0;
processAudioCallback( request, statusInt );
uv_mutex_lock( &pEngine->m_mutex ); //wait, until js call is done
if (!uv_mutex_trylock( &pEngine->m_mutex )) {
processAudioCallback( request, statusInt );
uv_mutex_unlock( &pEngine->m_mutex );
}

if( pEngine->m_uNumCachedOutputSamples > 0 ) {
error = Pa_WriteStream( pEngine->m_pPaStream, pEngine->m_cachedOutputSampleBlock, pEngine->m_uNumCachedOutputSamples );
Expand Down Expand Up @@ -617,4 +616,4 @@ Audio::AudioEngine::~AudioEngine() {

if( err != paNoError )
fprintf( stderr, "PortAudio error: %s\n", Pa_GetErrorText( err ) );
} // end AudioEngine::~AudioEngine()
} // end AudioEngine::~AudioEngine()
82 changes: 82 additions & 0 deletions basics.js
@@ -0,0 +1,82 @@
// just the basics

var coreaudio = require('./node-core-audio')
, http = require('http')
, portfinder = require('portfinder')
;


process.on('uncaughtException', function(err) {
console.log('process: ' + err.message);
console.error(err);
});


var clients = [];

portfinder.getPort({ port: 9999 }, function(err, portno) {
if (err) return console.log('portfinder.getPort 9999: ' + err.message);

var addr = function(socket) {
var sockaddr = socket.address ();

return (sockaddr.address + ':' + sockaddr.port);
};

var remove = function(socket) {
var i;

for (i = 0; i < clients.length; i++) {
if (clients[i] === socket) {
clients = clients.splice(i, 1);
return;
}
}
};

http.createServer(function(request, response) {
if (request.method !== 'GET') {
response.writeHead(405, { Allow: 'GET' });
return response.end();
}

// ignore any body....
request.on('end', function() {
response.writeHead(200, { 'Content-Type': 'audio', 'Content-Transfer-Encoding': 'binary' });
response.write(new Buffer([ ]));
clients.push(response.socket);
}).on('close', function() {
console.log('client close: ' + addr(request.socket));
remove(request.socket);
});
}).on('clientError', function(err, socket) {
console.log('client error: ' + err.message + '[' + addr(socket) + ']');
remove(socket);
}).on('listening', function() {
console.log('listening on port ' + portno);
}).listen(portno);
});


var startaudio = function() {
var engine, i, j;

engine = coreaudio.createNewAudioEngine();
engine.addAudioCallback( audioCallback );

console.log('isActive: ' + (engine.isActive() ? 'true' : 'false'));
console.log('options');
console.log(engine.getOptions ());
console.log('devices');
j = engine.getNumDevices();
for (i = 0; i < j; i++) console.log('#' + i + ': ' + engine.getDeviceName(i));
};

var audioCallback = function(inputBuffer) {
// TBD: loop through clients[] and send the buffer to each
// howeer, there's a few questions for mike (-;

return inputBuffer;
};

setTimeout (function() { try { startaudio(); } catch(ex) { console.log('startaudio: ' + ex.message); } }, 0);
2 changes: 1 addition & 1 deletion node-core-audio.js
Expand Up @@ -332,4 +332,4 @@ function interleave( inputBuffer, outputBuffer, numSamplesPerBuffer, numChannels
} // end for each sample position
} // end for each channel

} // end interleave()
} // end interleave()
4 changes: 2 additions & 2 deletions simpletest.js
Expand Up @@ -15,7 +15,7 @@ var options = {
outputChannels: 2
}

var audioCallback = function( numSamples, inputBuffer ) {
var audioCallback = function( inputBuffer ) {
console.log("sweet");
return inputBuffer;
}
Expand All @@ -27,4 +27,4 @@ setTimeout( function(){
var engine = coreAudio.createNewAudioEngine();

engine.addAudioCallback( audioCallback );
}, 0);
}, 0);

0 comments on commit 0cf9381

Please sign in to comment.