Files
sound-scope/main_buffer_manager_class.cpp
2026-03-27 10:26:47 -07:00

80 lines
1.9 KiB
C++

#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_timer.h>
#include <SDL2/SDL_ttf.h>
#include <stdio.h>
#include <stdlib.h>
#include <cmath>
#include <unistd.h>
#include <iostream>
#include <chrono>
#include <pulse/simple.h>
#include <pulse/error.h>
#include "scope.h"
// Pulse
static const pa_sample_spec my_pa_spec = {
.format = PA_SAMPLE_S16LE,
.rate = 44100,
.channels = 1
};
// audio buffer
main_buffer_manager_class::main_buffer_manager_class()
{
my_read_main_buffer = NULL;
}
main_buffer_manager_class::~main_buffer_manager_class()
{
if (my_read_main_buffer != NULL)
{
free(my_read_main_buffer);
}
}
Sint16 *main_buffer_manager_class::initial_preparation()
{
my_read_main_buffer = (Sint16 *)malloc ((sizeof(Sint16) * READ_BUFFER_SIZE));
if (my_read_main_buffer == NULL)
{
printf ("cannot allocate read buffer\n");
perror ("error is");
return NULL;
}
// Open pulse
pulse_s = pa_simple_new(NULL,"scope sound", PA_STREAM_RECORD, NULL, "record",
&my_pa_spec, NULL, NULL, &function_return);
if (pulse_s == NULL) {
printf("pa_simple_new() failed: %s\n", pa_strerror(function_return));
return NULL;
}
return my_read_main_buffer;
}
Sint16 *main_buffer_manager_class::read_main_buffer()
{
// grab the buffer from the audio device
printf("about to grab sound\n");
if (pa_simple_read(pulse_s, (Uint8 *)my_read_main_buffer,
(READ_BUFFER_SIZE * sizeof(Sint16)), &function_return) < 0) {
printf("sound read failed %s\n", pa_strerror(function_return));
return NULL;
}
printf("just grabbed the buffer from device\n");
// Turn the volume down
for (main_buffer_index = 0; main_buffer_index < READ_BUFFER_SIZE;
main_buffer_index += 1)
{
main_buffer_hold = (Sint16)(*(my_read_main_buffer + main_buffer_index) * MAIN_VOLUME);
*(my_read_main_buffer + main_buffer_index) = main_buffer_hold;
}
return my_read_main_buffer;
}