// Remaining16kToneDemo.cpp : Defines the entry point for the console application.
//
#define _USE_MATH_DEFINES
#include <cmath>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <conio.h>
#include <windows.h>
#include <SFML/Audio.hpp>
using namespace std;
//globals
//configs
int SampleRate = 22050;
double ToneFrequency = 1000.1;
double ToneTime = 1.25;
int SamplesZeroAtEnd = 20;
//used as static, or accessed by functions
int CurrentSample = 0;
unsigned int SamplesNeeded =(unsigned int)(ToneTime * (double)SampleRate);
vector<sf::Int16> SentanceSamples;
//function headers
void RunSequence();
void PlaySentence();
int NumberKey(char value);//returns value, -1 if canceled
int main()
{
sf::Sound SoundPlayer;
sf::SoundBuffer SentenceSound;
bool Running = false;
//main loop
for(unsigned char rx_char = 0x00; (rx_char != 27);)//escape exits
{
//display commands
system("cls");
cout << "<Numbers>\t\t: Set Value\n";
cout << "<Space>\t\t\t: Play or Stop (If Still Playing)\n";
cout << "'S'\t\t\t: Calls stop()\n";
cout << "'G'\t\t\t: Calls getStatus()\n";
cout << "<Esc>\t\t\t: Exits Program\n";
cout << "\n";
cout << "Samples Zero at the End\t: " << SamplesZeroAtEnd << "\n";
//wait for key here
rx_char = _getch();
switch(rx_char)
{
case 27://escape
break;
case 'S':
case 's':
SoundPlayer.stop();
break;
case 'G':
case 'g':
SoundPlayer.getStatus();
break;
case ' '://<space>
//clear 'Running' if has stopped
if(SoundPlayer.getStatus() == sf::SoundSource::Status::Stopped)Running = false;
if(Running == false)
{
//play sentence HERE!!!
RunSequence();//generates audio samples, stored in "SentenceSamples"
//SentenceSamples is a "vector<sf::Int16>"
if(SentanceSamples.size() >= 100)//skip if very few samples
{
SentenceSound.loadFromSamples(&SentanceSamples[0], SentanceSamples.size(), 1, SampleRate);
SoundPlayer.setBuffer(SentenceSound);
SoundPlayer.setLoop(false);
SoundPlayer.play();
Running = true;
}
}
else//Running?
{
//stop playing
SoundPlayer.stop();
Running = false;
}
break;
default:
//numbers
if(rx_char >= '0' && rx_char <= '9')
{
SamplesZeroAtEnd = NumberKey(rx_char);
}
}
}
return 0;
}
//functions
void RunSequence()
{
SentanceSamples.resize(SamplesNeeded);
unsigned int peak = ((1 << 15) - 1);
double RadiansPerSample = 2.0 * M_PI * ToneFrequency / SampleRate;
for(unsigned int i = 0;i < SamplesNeeded;i++)
{
SentanceSamples[i] = (unsigned int)round(sin((double)CurrentSample * RadiansPerSample) * (double)peak);
CurrentSample++;
}
//change last sample to zero
for(int i = 1;i <= SamplesZeroAtEnd;i++)
{
SentanceSamples[SentanceSamples.size() - i] = 0;
}
}
int NumberKey(char value)
{
int number = value - '0';
cout << value;
for(char rx_char = 0;;)
{
rx_char = _getch();
if(rx_char >= '0' && rx_char <= '9')
{
cout << rx_char;
number = number * 10 + rx_char - '0';
}
else if(rx_char == 13)//enter
{
if(number > SamplesNeeded)return SamplesNeeded;
return number;
}
else if(rx_char == 8)//backspace
{
if(number)
{
cout << "\b \b";
number = number / 10;
}
}
}
}