Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Audio Sample Analasis  (Read 2809 times)

0 Members and 1 Guest are viewing this topic.

lordseanington

  • Jr. Member
  • **
  • Posts: 86
  • Coffee is the blood of a coder.
    • View Profile
Audio Sample Analasis
« on: September 26, 2015, 07:13:07 pm »
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. 
Code: [Select]
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.

Code: [Select]
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.
« Last Edit: September 26, 2015, 07:18:21 pm by lordseanington »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10394
    • View Profile
    • development blog
    • Email
Audio Sample Analasis
« Reply #1 on: September 26, 2015, 08:36:51 pm »
I'm not exactly sure what you want to extract. An audio sample is nothing else than the value of the amplitude at a given moment. Think of a waveform on a graph where the x axis is time and the y axis is the amplitude.

If you want to extract some specific frequency or similar you'll most likely want to read up on fast Fourier transformation. It's not exactly a trivial topic, so don't expect anyone to just explain it on a forum. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://dev.my-gate.net/

lordseanington

  • Jr. Member
  • **
  • Posts: 86
  • Coffee is the blood of a coder.
    • View Profile
Re: Audio Sample Analasis
« Reply #2 on: September 26, 2015, 08:55:06 pm »
I am wanting to get the frequency of the sample. And I wasn't really expecting anyone to explain it i just didn't quite know where to look for the awnser.
Thank you

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10394
    • View Profile
    • development blog
    • Email
Audio Sample Analasis
« Reply #3 on: September 26, 2015, 11:56:21 pm »
The frequency of what sample exactly?

If you talk about the encoded sample and you know what the frequency was when you generated the sample, then it will be the same when decoding it.

Otherwise, if you don't want to into Fourier transformations, you need to know something about the signal. Like that it is a single sinus wave. If so you then can scan for the highest values and count the samples inbetween. Combined with the sample rate you now have all the needed information to calculate the frequency: frequency = samplerate / number of samples between peaks.

If your signal is more complex you can try to scan for the repeating part in different ways.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://dev.my-gate.net/

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6278
  • Thor Developer
    • View Profile
    • Bromeon
Re: Audio Sample Analasis
« Reply #4 on: September 27, 2015, 09:07:16 am »
I am wanting to get the frequency of the sample.
A single sample doesn't have a frequency. Frequency is the inverse wavelength, and a wave consists of many samples. As mentioned, FFT lets you compute the spectrum.

You should probably read a bit more about signal theory, especially the relation between time and frequency domain.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

lordseanington

  • Jr. Member
  • **
  • Posts: 86
  • Coffee is the blood of a coder.
    • View Profile
Re: Audio Sample Analasis
« Reply #5 on: September 28, 2015, 06:59:08 pm »
I figured I would need to. Thank You for your help and I will be back with more questions.