persist phase to stop disjointed sine waves
This commit is contained in:
parent
c1375ae0c9
commit
c8e40e9b80
|
|
@ -11,9 +11,9 @@
|
||||||
#include "../sounddevice.h"
|
#include "../sounddevice.h"
|
||||||
|
|
||||||
#define FORMAT PA_SAMPLE_S16LE
|
#define FORMAT PA_SAMPLE_S16LE
|
||||||
#define SAMPLE_RATE (48000)
|
#define SAMPLE_RATE (44100)
|
||||||
#define AMPLITUDE .5
|
#define AMPLITUDE 1
|
||||||
#define DURATION 4.0
|
#define DURATION 1.0
|
||||||
|
|
||||||
#ifndef M_PI
|
#ifndef M_PI
|
||||||
#define M_PI (3.14159265)
|
#define M_PI (3.14159265)
|
||||||
|
|
@ -49,20 +49,24 @@ void gear_sound_stream(pa_stream *s, size_t length, void *userdata) {
|
||||||
void engine_sound_stream(pa_stream *s, size_t length, void *userdata) {
|
void engine_sound_stream(pa_stream *s, size_t length, void *userdata) {
|
||||||
|
|
||||||
SoundData* data = (SoundData*)userdata;
|
SoundData* data = (SoundData*)userdata;
|
||||||
|
double freq = data->curr_frequency;
|
||||||
size_t num_samples = length / sizeof(int16_t);
|
size_t num_samples = length / sizeof(int16_t);
|
||||||
|
num_samples = (size_t) (DURATION * SAMPLE_RATE);
|
||||||
int16_t buffer[num_samples];
|
int16_t buffer[num_samples];
|
||||||
|
|
||||||
|
|
||||||
for (size_t i = 0; i < num_samples; i++) {
|
for (size_t i = 0; i < num_samples; i++) {
|
||||||
static double t = 0.0;
|
double t = data->phase;
|
||||||
double sample = AMPLITUDE * 32767.0 * sin(2.0 * M_PI * data->curr_frequency * t);
|
double sample = AMPLITUDE * 32767.0 * sin(2.0 * M_PI * freq * t);
|
||||||
|
|
||||||
|
|
||||||
buffer[i] = (int16_t)sample;
|
buffer[i] = (int16_t)sample;
|
||||||
|
|
||||||
t += 1.0 / SAMPLE_RATE;
|
t += 1.0 / SAMPLE_RATE;
|
||||||
if (t >= DURATION) {
|
if (t >= DURATION) {
|
||||||
t = 0.0;
|
t = -DURATION;
|
||||||
}
|
}
|
||||||
|
data->phase = t;
|
||||||
}
|
}
|
||||||
pa_stream_write(s, buffer, length, NULL, 0LL, PA_SEEK_RELATIVE);
|
pa_stream_write(s, buffer, length, NULL, 0LL, PA_SEEK_RELATIVE);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -131,7 +131,7 @@ int sounddev_init(SoundDevice* sounddevice, const char* devname, int volume, int
|
||||||
//sounddevice->sounddata.table_size = 48000/(100/60);
|
//sounddevice->sounddata.table_size = 48000/(100/60);
|
||||||
sounddevice->sounddata.curr_frequency = 0;
|
sounddevice->sounddata.curr_frequency = 0;
|
||||||
sounddevice->sounddata.curr_duration = 0;
|
sounddevice->sounddata.curr_duration = 0;
|
||||||
|
sounddevice->sounddata.phase = 0;
|
||||||
|
|
||||||
|
|
||||||
const char* streamname= "Engine";
|
const char* streamname= "Engine";
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ typedef struct
|
||||||
double duration;
|
double duration;
|
||||||
int curr_frequency;
|
int curr_frequency;
|
||||||
double curr_duration;
|
double curr_duration;
|
||||||
|
double phase;
|
||||||
}
|
}
|
||||||
SoundData;
|
SoundData;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue