#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"

audio_buffer_manager_class::audio_buffer_manager_class()
  {
  my_formatted_audio_buffer = NULL;
  }

audio_buffer_manager_class::~audio_buffer_manager_class()
  {
  if (my_formatted_audio_buffer != NULL)
    {
    free(my_formatted_audio_buffer);
    }
  }

int audio_buffer_manager_class::initialize_audio(int horizontal_size,
  int vertical_size, Sint16 *main_buffer)
  {
  my_formatted_audio_buffer = (int *)malloc ((sizeof(int) * AUDIO_BUFFER_SIZE));
  if (my_formatted_audio_buffer == NULL)
    {
    printf ("cannot allocate formatted audio audio buffer\n");
    perror ("error is");
    return 1;
    }
  my_audio_horizontal_size = horizontal_size;
  my_audio_vertical_size = vertical_size; 
  my_main_buffer = main_buffer;
  return 0;
  }

int *audio_buffer_manager_class::process_audio()
  {
  my_read_audio_index = 0;

  // Transform Sint16 to int for each element
  for (my_read_audio_index = 0; my_read_audio_index < my_audio_horizontal_size;
    my_read_audio_index += 1)
     {
     audio_holding = *(my_main_buffer + my_read_audio_index);
     *(my_formatted_audio_buffer + my_read_audio_index) = (int)audio_holding;
     }

  return my_formatted_audio_buffer;
  }
    
