I have made a data encoder that encodes data into audio using frequency modulation. I am having a main issue though. I directly load the file into the sound buffer and get its samples and im trying to decode it. I have a set amplitude, a set of data, a constant offset and i multiply the data by a multiplayer to make it easier to notice.
void EncodeData(std::string data, int speed, int multiplyer, int offset, unsigned int AMPLITUDE, int mode, std::string filename)
{
int seconds=(ceil(data.size()/(double)speed));
std::vector<sf::Int16> encoded;
const double TWO_PI = 6.28318;
double FREQUENCY= 0;
double increment = 0;
double x = 0;
double switchtime=ceil((44100*seconds)/(double)data.size());
std::cout<<44100*seconds <<" switchtime: " <<switchtime <<" datasize: " <<data.size() <<" samplecount: " <<seconds*44100<<"\n";
_getch();
double x2;
for(int sample=0; sample<44100*seconds; sample++)
{
FREQUENCY=((int)(data.at((int)floor(sample/switchtime)))*multiplyer)+offset;
increment=(FREQUENCY)/44100;
if(sample/switchtime==3.0)
x2=switchtime;
sf::Int16 Sample = AMPLITUDE* (sin(TWO_PI*x));
encoded.push_back(Sample);
x+=(increment);
}
sf::SoundBuffer buffer;
buffer.loadFromSamples(&encoded[0],encoded.size(),1,44100);
}
I am trying to get the data from the sound samples. I have this so far.
bool DecodeFile(std::string filename, int speed, int multiplyer, int offset, int AMPLITUDE, std::string &output)
{
sf::SoundBuffer sb;
sb.loadFromFile(filename);
int rate=sb.getSampleRate();
int seconds=sb.getDuration().asSeconds();
int datasize=seconds*speed;
double switchtime=(44100*seconds)/datasize;
const sf::Int16* samples = sb.getSamples();
std::vector<char> data;
data.resize((int)(ceil(sb.getSampleCount()/switchtime)));
for(unsigned int counter=0; counter<data.size(); counter++)
data[counter]=' ';
//???????????????????????????
// Here is where im stuck
//??????????????????????????
}
I already know that i need to loop through everything but my main issue is the formula to get the Data. Altogether though i might be totally wrong on everything i did.