1

**SFML projects / Re: [MAC][Linux?]I published an SFML applet: ToneBoards**

« **on:**January 17, 2020, 11:33:14 pm »

I had a little time, and made a couple quick demos:

quick demo

quick cfg file drop + bass demo

...I still want to port to Linux(though not sure when it will happen).

...maybe I should just ploop down my code for the update here for all to use(won't do any good hidden away):

#include <SFML/Audio.hpp>

#include <SFML/Graphics.hpp>

#include "ResourcePath.hpp"

#include <cmath>

#include <iostream>

#include <string>

//const unsigned SAMPLES = 22050;//44100;//

const unsigned SAMPLE_RATE = 44100;//44112

const unsigned AMPLITUDE = 30000;

const double TWO_PI = 6.28318530717958647692;

const double increment = 440./44100;//middle A

const int numharmonics = 128;//1;//2;//4;//8;//16;//32;//64;//128;

const float EulerMasch = 0.6;//0.5772;//0.5772156649...//audible hum numharmonics=2

const float hoffset = 0.6;//0.5;//still audible hum numharmonics=2

const float bapprox = 1.65;

const double lgrow = -8.0/102.0;//-16.0/102.0;//-16.0/128.0;//-8.0/128.0;

const short lmid = 102;//128;

const double lopt = 1.0;//lopt>0

//y = sin(2π*M*x) => ƒ = M <=> λ = 1/M

//M = (440/44100)*2^(i/24)

int main(int, char const**){

//double jmult = 1./2; // 1

//double jmult = 1./4; // 2

//double jmult = 1./8; // 3

//double jmult = 1./16; // 4 div 5

double jmult = 1./40; //this!//this!//this!//this!//this!//this!

short wfund = 0;

std::cout << "jmult: " << jmult << "\n\n";

for (int i=0; i<256; i++) {

int duration = (int) (jmult*SAMPLE_RATE/exp2((double) (i-138)/24));//jmult/m;//relative to middle A

double dur = (jmult*SAMPLE_RATE/exp2((double) (i-138)/24));//relative to middle A

//if (duration < 22051) {

//if (duration < 44101) {

if (duration < 88201) {

jmult *= 2;

duration = (int) (jmult*SAMPLE_RATE/exp2((double) (i-138)/24));//jmult/m;

dur = (jmult*SAMPLE_RATE/exp2((double) (i-138)/24));

}

double cycles = 440.*exp2((double) (i-138)/24)*dur/44100.;//relative to middle A

//std::cout << "tone_" << i << "\'s lambda is " << ((int) 1/m) << " samples long\n";

std::cout << "tone_" << i << " lambda " << dur << " : " << duration << " : " << " cycles: " << cycles << " # " << i - 138 << "\n";

float mlogistic = 1.0+exp(lgrow*(i-lmid));

unsigned durationSAMPS = duration;

sf::Int16 *myrawsamples = new sf::Int16[durationSAMPS];

double x = 0;

for (unsigned j = 0; j < durationSAMPS; j++) {//build//raw//build//raw//build//raw//build//raw//build//raw

//myrawsamples[j] = AMPLITUDE * sin(x*TWO_PI);

//myrawsamples[j] = AMPLITUDE * sin(x*TWO_PI)*i/255; //fundamental*(i)/256 + harmonics*(256-i)/256

//myrawsamples[j] = AMPLITUDE * sin(x*TWO_PI)*wfund/138; //fundamental*(i)/256 + harmonics*(256-i)/256

//myrawsamples[j] = AMPLITUDE * sin(x*TWO_PI)*i/255;

//myrawsamples[j] = AMPLITUDE * sin(x*TWO_PI)*(255-255/(i+1))/255;

myrawsamples[j] = AMPLITUDE * sin(x*TWO_PI)/pow(mlogistic, lopt);//pow (double base, double exponent)

//y=255/(1+e^(-8/128(x-128)))

for(int h=1; h<numharmonics+1; h++){

//myrawsamples[j] += AMPLITUDE * sin(h*x*TWO_PI)/numharmonics;

//myrawsamples[j] += AMPLITUDE * sin(h*x*TWO_PI)*(numharmonics+1-h)/(numharmonics*(numharmonics+1)/2);

//harmonics += AMPLITUDE * sin(h*x*TWO_PI)*(numharmonics+1-h)/(numharmonics*(numharmonics+1)/2);

//myrawsamples[j] += AMPLITUDE * sin(h*x*TWO_PI)*(numharmonics+1-h)/(numharmonics*(numharmonics+1)/2)*(256-i)/256; //fundamental*(i)/256 + harmonics*(256-i)/256

//myrawsamples[j] += AMPLITUDE * sin(h*x*TWO_PI)/h/(log(h + hoffset)+EulerMasch)*(255-i)/255; //fundamental*(i)/256 + harmonics*(256-i)/256

//if (wfund < 138) {

//myrawsamples[j] += AMPLITUDE * sin(h*x*TWO_PI)/h/(log(h + hoffset)+EulerMasch)*(138-wfund)/138;//hseriesbest!

//myrawsamples[j] += AMPLITUDE * sin(h*x*TWO_PI)/numharmonics*(138-wfund)/138;

//myrawsamples[j] +=

// AMPLITUDE * sin(h*x*TWO_PI)*(numharmonics+1-h)/(numharmonics*(numharmonics+1)/2)*(138-wfund)/138;

//myrawsamples[j] += AMPLITUDE * sin(h*x*TWO_PI)/h/h/(-1/(h+hoffset)+1.65)*(138-wfund)/138;//y=-1/(x+0.6)+1.65

//}

//myrawsamples[j] += AMPLITUDE * sin(h*x*TWO_PI)/h/h/(-1/(h+hoffset)+1.65)*(255-i)/255;

//myrawsamples[j] += AMPLITUDE * sin(h*x*TWO_PI)/h/h/(-1/(h+hoffset)+bapprox)*(255/(i+1))/255;

myrawsamples[j] += AMPLITUDE * sin(h*x*TWO_PI)/h/h/(-1/(h+hoffset)+bapprox)*(1-1/pow(mlogistic, lopt));

//y=255/(1+e^(-8/128(x-128)))

}

x += increment*exp2((double) (i-138)/24);//mNOTESarray/12);//relative to middle A

//lowest note is 5.75 octaves below middle A

}

sf::SoundBuffer *Buffer = new sf::SoundBuffer();

if (!Buffer->loadFromSamples(myrawsamples, durationSAMPS, 1, SAMPLE_RATE)) {

std::cerr << "INITIALIZATION failed!" << std::endl;

return 1;

}

std::string outfilename = "tone_" + std::to_string(i) + "_.ogg";//outfiles/

std::cout << outfilename << " at " << resourcePath() << "\n\n";

Buffer->saveToFile(resourcePath() + outfilename);//___

if (wfund < 138) {

wfund++;

}

}

return EXIT_SUCCESS;

}

quick demo

quick cfg file drop + bass demo

...I still want to port to Linux(though not sure when it will happen).

...maybe I should just ploop down my code for the update here for all to use(won't do any good hidden away):

(click to show/hide)

#include <SFML/Audio.hpp>

#include <SFML/Graphics.hpp>

#include "ResourcePath.hpp"

#include <cmath>

#include <iostream>

#include <string>

//const unsigned SAMPLES = 22050;//44100;//

const unsigned SAMPLE_RATE = 44100;//44112

const unsigned AMPLITUDE = 30000;

const double TWO_PI = 6.28318530717958647692;

const double increment = 440./44100;//middle A

const int numharmonics = 128;//1;//2;//4;//8;//16;//32;//64;//128;

const float EulerMasch = 0.6;//0.5772;//0.5772156649...//audible hum numharmonics=2

const float hoffset = 0.6;//0.5;//still audible hum numharmonics=2

const float bapprox = 1.65;

const double lgrow = -8.0/102.0;//-16.0/102.0;//-16.0/128.0;//-8.0/128.0;

const short lmid = 102;//128;

const double lopt = 1.0;//lopt>0

//y = sin(2π*M*x) => ƒ = M <=> λ = 1/M

//M = (440/44100)*2^(i/24)

int main(int, char const**){

//double jmult = 1./2; // 1

//double jmult = 1./4; // 2

//double jmult = 1./8; // 3

//double jmult = 1./16; // 4 div 5

double jmult = 1./40; //this!//this!//this!//this!//this!//this!

short wfund = 0;

std::cout << "jmult: " << jmult << "\n\n";

for (int i=0; i<256; i++) {

int duration = (int) (jmult*SAMPLE_RATE/exp2((double) (i-138)/24));//jmult/m;//relative to middle A

double dur = (jmult*SAMPLE_RATE/exp2((double) (i-138)/24));//relative to middle A

//if (duration < 22051) {

//if (duration < 44101) {

if (duration < 88201) {

jmult *= 2;

duration = (int) (jmult*SAMPLE_RATE/exp2((double) (i-138)/24));//jmult/m;

dur = (jmult*SAMPLE_RATE/exp2((double) (i-138)/24));

}

double cycles = 440.*exp2((double) (i-138)/24)*dur/44100.;//relative to middle A

//std::cout << "tone_" << i << "\'s lambda is " << ((int) 1/m) << " samples long\n";

std::cout << "tone_" << i << " lambda " << dur << " : " << duration << " : " << " cycles: " << cycles << " # " << i - 138 << "\n";

float mlogistic = 1.0+exp(lgrow*(i-lmid));

unsigned durationSAMPS = duration;

sf::Int16 *myrawsamples = new sf::Int16[durationSAMPS];

double x = 0;

for (unsigned j = 0; j < durationSAMPS; j++) {//build//raw//build//raw//build//raw//build//raw//build//raw

//myrawsamples[j] = AMPLITUDE * sin(x*TWO_PI);

//myrawsamples[j] = AMPLITUDE * sin(x*TWO_PI)*i/255; //fundamental*(i)/256 + harmonics*(256-i)/256

//myrawsamples[j] = AMPLITUDE * sin(x*TWO_PI)*wfund/138; //fundamental*(i)/256 + harmonics*(256-i)/256

//myrawsamples[j] = AMPLITUDE * sin(x*TWO_PI)*i/255;

//myrawsamples[j] = AMPLITUDE * sin(x*TWO_PI)*(255-255/(i+1))/255;

myrawsamples[j] = AMPLITUDE * sin(x*TWO_PI)/pow(mlogistic, lopt);//pow (double base, double exponent)

//y=255/(1+e^(-8/128(x-128)))

for(int h=1; h<numharmonics+1; h++){

//myrawsamples[j] += AMPLITUDE * sin(h*x*TWO_PI)/numharmonics;

//myrawsamples[j] += AMPLITUDE * sin(h*x*TWO_PI)*(numharmonics+1-h)/(numharmonics*(numharmonics+1)/2);

//harmonics += AMPLITUDE * sin(h*x*TWO_PI)*(numharmonics+1-h)/(numharmonics*(numharmonics+1)/2);

//myrawsamples[j] += AMPLITUDE * sin(h*x*TWO_PI)*(numharmonics+1-h)/(numharmonics*(numharmonics+1)/2)*(256-i)/256; //fundamental*(i)/256 + harmonics*(256-i)/256

//myrawsamples[j] += AMPLITUDE * sin(h*x*TWO_PI)/h/(log(h + hoffset)+EulerMasch)*(255-i)/255; //fundamental*(i)/256 + harmonics*(256-i)/256

//if (wfund < 138) {

//myrawsamples[j] += AMPLITUDE * sin(h*x*TWO_PI)/h/(log(h + hoffset)+EulerMasch)*(138-wfund)/138;//hseriesbest!

//myrawsamples[j] += AMPLITUDE * sin(h*x*TWO_PI)/numharmonics*(138-wfund)/138;

//myrawsamples[j] +=

// AMPLITUDE * sin(h*x*TWO_PI)*(numharmonics+1-h)/(numharmonics*(numharmonics+1)/2)*(138-wfund)/138;

//myrawsamples[j] += AMPLITUDE * sin(h*x*TWO_PI)/h/h/(-1/(h+hoffset)+1.65)*(138-wfund)/138;//y=-1/(x+0.6)+1.65

//}

//myrawsamples[j] += AMPLITUDE * sin(h*x*TWO_PI)/h/h/(-1/(h+hoffset)+1.65)*(255-i)/255;

//myrawsamples[j] += AMPLITUDE * sin(h*x*TWO_PI)/h/h/(-1/(h+hoffset)+bapprox)*(255/(i+1))/255;

myrawsamples[j] += AMPLITUDE * sin(h*x*TWO_PI)/h/h/(-1/(h+hoffset)+bapprox)*(1-1/pow(mlogistic, lopt));

//y=255/(1+e^(-8/128(x-128)))

}

x += increment*exp2((double) (i-138)/24);//mNOTESarray/12);//relative to middle A

//lowest note is 5.75 octaves below middle A

}

sf::SoundBuffer *Buffer = new sf::SoundBuffer();

if (!Buffer->loadFromSamples(myrawsamples, durationSAMPS, 1, SAMPLE_RATE)) {

std::cerr << "INITIALIZATION failed!" << std::endl;

return 1;

}

std::string outfilename = "tone_" + std::to_string(i) + "_.ogg";//outfiles/

std::cout << outfilename << " at " << resourcePath() << "\n\n";

Buffer->saveToFile(resourcePath() + outfilename);//___

if (wfund < 138) {

wfund++;

}

}

return EXIT_SUCCESS;

}