Skip to content

Commit

Permalink
using ofSoundPlayer istead of Tonic in Box2D
Browse files Browse the repository at this point in the history
This change reduces a strange sound artifact
  • Loading branch information
galsasson committed Jan 9, 2014
1 parent f7f91ae commit 94b6507
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ void CloudsVisualSystemExampleBox2D::selfSetupGui(){
customGui->setName("EXAMPLE BOX 2D");
customGui->setWidgetFontSize(OFX_UI_FONT_SMALL);

customGui->addToggle("DISPLAY", &bDrawToScreen);

customGui->addToggle("GAVITY CONTROL MODE", &bGravityMod);

customGui->addToggle("CIRCLES", &bCircles);
Expand Down Expand Up @@ -65,12 +63,10 @@ void CloudsVisualSystemExampleBox2D::selfSetupGui(){
customGui->addMinimalSlider("S3", 0.0, 255, &rectHSB.g, length, dim)->setShowValue(true);
customGui->addMinimalSlider("B3", 0.0, 255, &rectHSB.b, length, dim)->setShowValue(true);
customGui->setWidgetPosition(OFX_UI_WIDGET_POSITION_DOWN);

#ifdef TONIC_WAVES

customGui->addSpacer("SOUNDS");
customGui->addSlider("MAX FREQ", 0, 300, &noteMax);
customGui->addSlider("MIN FREQ", 0, 300, &noteMin);
#endif

customGui->addSlider("Volume", 0, 1, &masterVolume);

ofAddListener(customGui->newGUIEvent, this, &CloudsVisualSystemExampleBox2D::selfGuiEvent);
guis.push_back(customGui);
Expand Down Expand Up @@ -116,20 +112,6 @@ void CloudsVisualSystemExampleBox2D::selfGuiEvent(ofxUIEventArgs &e)
rects.clear();
}
}
#ifdef TONIC_WAVES
else if (e.widget->getName() == "MAX FREQ") {
for (int i=0; i<WAVE_GEN_NUM; i++)
{
noteControl[i].max(noteMax);
}
}
else if (e.widget->getName() == "MIN FREQ") {
for (int i=0; i<WAVE_GEN_NUM; i++)
{
noteControl[i].min(noteMin);
}
}
#endif
}

//Use system gui for global or logical settings, for exmpl
Expand Down Expand Up @@ -161,6 +143,9 @@ void CloudsVisualSystemExampleBox2D::selfSetup()
box2d.setGravity(0, 7);
box2d.createBounds();
box2d.setFPS(60);
ofAddListener(box2d.contactStartEvents,
this,
&CloudsVisualSystemExampleBox2D::contactStart);

prevScreenSize = ofVec2f(ofGetWidth(), ofGetHeight());

Expand All @@ -185,65 +170,31 @@ void CloudsVisualSystemExampleBox2D::selfSetup()
rectSizeDev = 10;
rectSizeMean = 20;

triggerIndex = 0;

circleHSB = ofFloatColor(100, 100, 100);
circleLineHSB = ofFloatColor(100, 100, 100);
rectHSB = ofFloatColor(100, 100, 100);

#ifdef TONIC_WAVES
noteMin = 0;
noteMax = 50;
#endif

// setup sound synth
#ifdef TONIC_WAVES
vector<float> scale;
scale.push_back(0);
scale.push_back(4);
scale.push_back(7);
scale.push_back(11);

for (int i=0; i<GENERATOR_NUM; i++)
masterVolume = 1;
lastSampleTime = 0;
for (int i=0; i<7; i++)
{
noteControl[i] = ControlRandom().min(noteMin).max(noteMax).trigger(collisionTrigger[i]);
ControlSnapToScale scaleSnapper = ControlSnapToScale().setScale(scale);
scaleSnapper.input(20 + noteControl[i]);

Generator collisionGen = SineWave().freq(ControlMidiToFreq().input(scaleSnapper)) * 0.05 * ADSR(0.01, 0.1, 0, 0).trigger(collisionTrigger[i]);

output = output + collisionGen;
ostringstream fn;
fn << GetCloudsDataPath() << "sound/triggers/drip" << i+1 << ".aif";
samplePlayer[i].loadSound(fn.str());
samplePlayer[i].setMultiPlay(true);
samplePlayer[i].setSpeed(0.5);
}
#else
string strDir = GetCloudsDataPath()+"sound/triggers/";
ofDirectory sdir(strDir);

// load the samples
for (int i=0; i<7; i++)
for (int i=0; i<8; i++)
{
char filename[32];
std::sprintf(filename, "cardboard%d.aif", i+1);
string strAbsPath1 = sdir.getAbsolutePath() + "/" + filename;
boxSamples[i] = loadAudioFile(strAbsPath1);

std::sprintf(filename, "drip%d.aif", i+1);
string strAbsPath2 = sdir.getAbsolutePath() + "/" + filename;
circleSamples[i] = loadAudioFile(strAbsPath2);
ostringstream fn;
fn << GetCloudsDataPath() << "sound/triggers/cardboard" << i+1 << ".aif";
samplePlayer[i+7].loadSound(fn.str());
samplePlayer[i+7].setMultiPlay(true);
}

// allocate the generators
for (int i=0; i<GENERATOR_NUM; i++)
{
Generator gen = BufferPlayer().setBuffer(circleSamples[i%7]).trigger(circleTrigger[i]) * circleVolume[i];
output = output + gen;
}
for (int i=0; i<GENERATOR_NUM; i++)
{
Generator gen = BufferPlayer().setBuffer(boxSamples[i%7]).trigger(boxTrigger[i]) * boxVolume[i];
output = output + gen;
}
#endif
synth.setOutputGen(output);
}

// selfPresetLoaded is called whenever a new preset is triggered
Expand Down Expand Up @@ -277,12 +228,6 @@ void CloudsVisualSystemExampleBox2D::selfBegin()
if (!bRandomPlatforms) {
removeRandomPlatform();
}

ofAddListener(box2d.contactStartEvents,
this,
&CloudsVisualSystemExampleBox2D::contactStart);

ofAddListener(GetCloudsAudioEvents()->diageticAudioRequested, this, &CloudsVisualSystemExampleBox2D::audioRequested);
}

//do things like ofRotate/ofTranslate here
Expand Down Expand Up @@ -310,10 +255,10 @@ void CloudsVisualSystemExampleBox2D::selfUpdate(){
// handle random platform
if (bRandomPlatforms) {
randomPlatformCounter--;
if (randomPlatformCounter == 0) {
if (randomPlatformCounter==0) {
addRandomPlatform();
}
if (randomPlatformCounter < -400) {
if (randomPlatformCounter<-400) {
removeRandomPlatform();
randomPlatformCounter = 150;
}
Expand Down Expand Up @@ -422,7 +367,8 @@ void CloudsVisualSystemExampleBox2D::selfDrawBackground()
// this is called when your system is no longer drawing.
// Right after this selfUpdate() and selfDraw() won't be called any more
void CloudsVisualSystemExampleBox2D::selfEnd(){
ofRemoveListener(GetCloudsAudioEvents()->diageticAudioRequested, this, &CloudsVisualSystemExampleBox2D::audioRequested);


}
// this is called when you should clear all the memory and delet anything you made in setup
void CloudsVisualSystemExampleBox2D::selfExit(){
Expand Down Expand Up @@ -524,9 +470,8 @@ void CloudsVisualSystemExampleBox2D::addCircle(ofVec2f pos, ofVec2f vel, float r
circle.setup(box2d.getWorld(), pos.x, pos.y, rad);
circle.setVelocity(vel);
circle.setRotation(ofRandom(180));

circle.body->SetUserData((void*)0);

// circle.setData((void*)1234);
circles.push_back(circle);
}

Expand All @@ -538,9 +483,7 @@ void CloudsVisualSystemExampleBox2D::addRect(ofVec2f pos, ofVec2f vel, ofVec2f s
rect.setup(box2d.getWorld(), pos.x, pos.y, size.x, size.y);
rect.setVelocity(vel);
rect.setRotation(ofRandom(180));

rect.body->SetUserData((void*)1);

rects.push_back(rect);
}

Expand Down Expand Up @@ -631,38 +574,37 @@ void CloudsVisualSystemExampleBox2D::reinitBounds()

void CloudsVisualSystemExampleBox2D::contactStart(ofxBox2dContactArgs &e)
{
b2Vec2 aVel = e.a->GetBody()->GetLinearVelocity();
b2Vec2 bVel = e.b->GetBody()->GetLinearVelocity();
float aVel = e.a->GetBody()->GetLinearVelocity().Length();
float bVel = e.b->GetBody()->GetLinearVelocity().Length();

int type;
if (aVel > bVel) {
type = (int)e.a->GetBody()->GetUserData();
}
else {
type = (int)e.b->GetBody()->GetUserData();
}

float maxVel = max(aVel.Length(), bVel.Length());
if (maxVel > 8)
int sIndex = type;

float maxVel = max(aVel, bVel);
if (maxVel > 3)
{
int type=0;
if (aVel.Length() > bVel.Length()) {
type = (int)e.a->GetBody()->GetUserData();
int index = sIndex*7 + (int)ofRandom(7);
float vol = maxVel / 70;
vol = (vol<1) ? pow(vol, 2) : 1;

if (ofGetElapsedTimeMillis() > lastSampleTime+15) {
lastSampleTime = ofGetElapsedTimeMillis();
}
else {
type = (int)e.b->GetBody()->GetUserData();
vol *= 0.01;
}

float vol = maxVel / 200;
vol = (vol>1) ? 0.05 : pow(vol, 2);

if (triggerIndex >= GENERATOR_NUM) {
triggerIndex = 0;
}
samplePlayer[index].setVolume(vol * masterVolume);
samplePlayer[index].play();

if (type == 0) {
circleVolume[triggerIndex].value(vol);
circleTrigger[triggerIndex].trigger();
}
else {
boxVolume[triggerIndex].value(vol);
boxTrigger[triggerIndex].trigger();
}
triggerIndex++;
}

}

float CloudsVisualSystemExampleBox2D::getGaussian() {
Expand All @@ -673,8 +615,3 @@ float CloudsVisualSystemExampleBox2D::getGaussian() {

return (sqrt (-2.0 * log(x1)) * cos(2.0 * PI * x2)) / 2;
}

void CloudsVisualSystemExampleBox2D::audioRequested(ofAudioEventArgs& args)
{
synth.fillBufferOfFloats(args.buffer, args.bufferSize, args.nChannels);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,8 @@
#include "ofMain.h"
#include "CloudsVisualSystem.h"
#include "ofxBox2d.h"
#include "CloudsAudioEvents.h"
#include "ofSoundPlayer.h"
#include "CloudsGlobal.h"
#include "ofxTonic.h"

using namespace Tonic;

#define GENERATOR_NUM 60
class DummyApp;

//TODO: rename this to your own visual system
class CloudsVisualSystemExampleBox2D : public CloudsVisualSystem {
Expand Down Expand Up @@ -162,24 +156,7 @@ class CloudsVisualSystemExampleBox2D : public CloudsVisualSystem {
float rectSizeDev;
float rectSizeMean;

// Sound
ofxTonicSynth synth;
SampleTable boxSamples[7];
SampleTable circleSamples[7];
#ifdef TONIC_WAVES
ControlTrigger collisionTrigger[GENERATOR_NUM];
ControlRandom noteControl[GENERATOR_NUM];
float noteMax;
float noteMin;
#else
ControlTrigger boxTrigger[GENERATOR_NUM];
ControlParameter boxVolume[GENERATOR_NUM];

ControlTrigger circleTrigger[GENERATOR_NUM];
ControlParameter circleVolume[GENERATOR_NUM];
#endif
int triggerIndex;
Generator output;

void audioRequested(ofAudioEventArgs& args);
float masterVolume;
ofSoundPlayer samplePlayer[16];
unsigned long long lastSampleTime;
};
8 changes: 8 additions & 0 deletions VSExampleBox2D/ExampleBox2D.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
2A316F28187F545700662636 /* ofxSystemTextbox.mm in Sources */ = {isa = PBXBuildFile; fileRef = 2A316F27187F545600662636 /* ofxSystemTextbox.mm */; };
2A316F29187F545700662636 /* ofxSystemTextbox.mm in Sources */ = {isa = PBXBuildFile; fileRef = 2A316F27187F545600662636 /* ofxSystemTextbox.mm */; };
2A4F33BF1863B0C30070C0ED /* CloudsMixer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2A4F33AF1863B0C20070C0ED /* CloudsMixer.cpp */; };
2A4F33C01863B0C30070C0ED /* CloudsMixer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2A4F33AF1863B0C20070C0ED /* CloudsMixer.cpp */; };
2A4F33D11863BE9B0070C0ED /* CloudsAudioEvents.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2A4F33D01863BE9B0070C0ED /* CloudsAudioEvents.cpp */; };
Expand Down Expand Up @@ -452,6 +454,8 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
2A316F26187F545600662636 /* ofxSystemTextbox.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ofxSystemTextbox.h; sourceTree = "<group>"; };
2A316F27187F545600662636 /* ofxSystemTextbox.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = ofxSystemTextbox.mm; sourceTree = "<group>"; };
2A4F33AE1863B0C20070C0ED /* CloudsAudioEvents.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CloudsAudioEvents.h; sourceTree = "<group>"; };
2A4F33AF1863B0C20070C0ED /* CloudsMixer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CloudsMixer.cpp; sourceTree = "<group>"; };
2A4F33B01863B0C20070C0ED /* CloudsMixer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CloudsMixer.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1471,6 +1475,8 @@
E72FF11817E688FE0007780D /* VisualSystemsLibrary */ = {
isa = PBXGroup;
children = (
2A316F26187F545600662636 /* ofxSystemTextbox.h */,
2A316F27187F545600662636 /* ofxSystemTextbox.mm */,
E72FF11A17E688FE0007780D /* CloudsRGBDCamera.cpp */,
E72FF11B17E688FE0007780D /* CloudsRGBDCamera.h */,
E72FF11C17E688FE0007780D /* CloudsRGBDVideoPlayer.cpp */,
Expand Down Expand Up @@ -2139,6 +2145,7 @@
2ACA69201871EDB60029603B /* Effect.cpp in Sources */,
85732C4018307C80002B53D9 /* ofxDamperBehavior.cpp in Sources */,
2ACA68F81871EDB60029603B /* ControlDelay.cpp in Sources */,
2A316F29187F545700662636 /* ofxSystemTextbox.mm in Sources */,
85732C4118307C80002B53D9 /* b2CircleContact.cpp in Sources */,
85732C4218307C80002B53D9 /* b2PolygonAndCircleContact.cpp in Sources */,
85732C4318307C80002B53D9 /* ofxDistorterBehavior.cpp in Sources */,
Expand Down Expand Up @@ -2438,6 +2445,7 @@
2ACA69431871EDB60029603B /* TableLookupOsc.cpp in Sources */,
E7CF78C61753FECA006B7D41 /* ofxEasing.cpp in Sources */,
E7CF78C71753FECA006B7D41 /* ofxEasingBack.cpp in Sources */,
2A316F28187F545700662636 /* ofxSystemTextbox.mm in Sources */,
2ACA690D1871EDB60029603B /* ControlSnapToScale.cpp in Sources */,
E7CF78C81753FECA006B7D41 /* ofxEasingBounce.cpp in Sources */,
E7CF78C91753FECA006B7D41 /* ofxEasingCirc.cpp in Sources */,
Expand Down
4 changes: 3 additions & 1 deletion VSExampleBox2D/src/testApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

//--------------------------------------------------------------
void testApp::setup(){
mixer.setup(2, 44100, 512, 2);
ofSetVerticalSync(true);

mixer.setup();
mixer.setDiageticVolume(1);

exampleBox2D.setup();
Expand Down

0 comments on commit 94b6507

Please sign in to comment.