Skip to content

Instantly share code, notes, and snippets.

@Chadtech
Last active August 29, 2015 14:13
Show Gist options
  • Save Chadtech/58660c96f156ffcbfe61 to your computer and use it in GitHub Desktop.
Save Chadtech/58660c96f156ffcbfe61 to your computer and use it in GitHub Desktop.
cppOperations = [
[ 'sine', '466.667', '88200']
[ 'volume', '0.5', '0', '88200']
[ 'fade in' '0', '88200' ]
[ 'ramp', '0', '30', '88170', '88200' ]
]
Nt.do cppOPerations
Nt = require './build/Release/NtCpp'
{buildFile} = require '../reallyOldNt/noitech'
buildFile 'pointerTest1.wav', [Nt.sineGenerate 466.667, 88200]
#include <nan.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
// For sine function
#include <math.h>
#include "./generate/sineGenerate.h"
using namespace v8;
using v8::String;
// Arguments are:
// frequency duration
NAN_METHOD(sineGenerate){
NanScope();
int sustain = args[1]->Uint32Value();
double frequency = args[0]->NumberValue();
frequency /= 44100;
short * audio = new short[sustain];
int confirmation = sineGenerate1(frequency, sustain, audio);
Local<Array> output = NanNew<Array>(sustain);
int sampleIndex = 0;
while (sampleIndex < sustain){
short sample = audio[sampleIndex];
output->Set(sampleIndex, NanNew(sample));
sampleIndex++;
}
NanReturnValue(output);
}
void Init(Handle<Object> exports){
exports->Set(NanNew("sineGenerate"),
NanNew<FunctionTemplate>(sineGenerate)->GetFunction());
}
NODE_MODULE(NtCpp, Init);
int sineGenerate1 (double frequency, const int sustain, short * audio){
double pi = 3.14159;
int maxAmplitude = 32767;
int outputIndex = 0;
while (outputIndex < sustain){
short sample = maxAmplitude * sin(pi * frequency * outputIndex * 2);
*(audio + outputIndex) = sample;
outputIndex++;
}
return outputIndex;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment