169 lines
4.2 KiB
C++
169 lines
4.2 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 <cmath>
|
|
#include <unistd.h>
|
|
#include <iostream>
|
|
#include <sys/time.h>
|
|
#include <chrono>
|
|
#include <alsa/asoundlib.h>
|
|
#include "scope.h"
|
|
|
|
alsa_read_class::alsa_read_class()
|
|
{
|
|
}
|
|
|
|
INPUT_SOUND_DATA_TYPE *alsa_read_class::initialize_alsa(char *device_name)
|
|
{
|
|
rate = SAMPLE_RATE;
|
|
buffer_frames = ALSA_BUFFER_FRAMES;
|
|
printf("initialize alsa device\n");
|
|
format = SND_PCM_FORMAT_S16_LE;
|
|
|
|
// Mixer for low volume
|
|
// snd_mixer_open(&mixer_handle, 0);
|
|
// snd_mixer_attach(mixer_handle, card);
|
|
// snd_mixer_selem_register(mixer_handle, NULL, NULL);
|
|
// snd_mixer_load(mixer_handle);
|
|
// snd_mixer_selem_id_alloca(&sid);
|
|
// snd_mixer_selem_id_set_index(sid, 0);
|
|
// snd_mixer_selem_id_set_name(sid, selem_name);
|
|
// snd_mixer_elem_t* elem = snd_mixer_find_selem(mixer_handle, sid);
|
|
// snd_mixer_selem_get_capture_volume_range(elem, &min, &max);
|
|
// snd_mixer_selem_set_capture_volume_all(elem, 10);
|
|
// snd_mixer_close(mixer_handle);
|
|
|
|
// Open device
|
|
if ((err = snd_pcm_open (&capture_handle, device_name, SND_PCM_STREAM_CAPTURE, 0)) < 0)
|
|
{
|
|
fprintf (stderr, "cannot open audio device %s (%s)\n",
|
|
device_name,
|
|
snd_strerror (err));
|
|
return NULL;
|
|
}
|
|
|
|
fprintf(stdout,"opened interface\n");
|
|
|
|
// allocate the hw params
|
|
if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0)
|
|
{
|
|
fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n",
|
|
snd_strerror (err));
|
|
return NULL;
|
|
}
|
|
|
|
fprintf(stdout,"allocated hw param\n");
|
|
|
|
//Initialize hw params
|
|
if ((err = snd_pcm_hw_params_any (capture_handle, hw_params)) < 0)
|
|
{
|
|
fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n",
|
|
snd_strerror (err));
|
|
return NULL;
|
|
}
|
|
|
|
fprintf(stdout, "initialized hardware paramer structure\n");
|
|
|
|
// set access type
|
|
if ((err = snd_pcm_hw_params_set_access (capture_handle, hw_params,
|
|
SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
|
|
{
|
|
fprintf (stderr, "cannot set access type (%s)\n",
|
|
snd_strerror (err));
|
|
return NULL;
|
|
}
|
|
|
|
fprintf(stdout, "set access type\n");
|
|
|
|
// set format
|
|
if ((err = snd_pcm_hw_params_set_format (capture_handle, hw_params, format)) < 0)
|
|
{
|
|
fprintf (stderr, "cannot set sample format (%s)\n",
|
|
snd_strerror (err));
|
|
return NULL;
|
|
}
|
|
|
|
fprintf(stdout, "hw_params format\n");
|
|
|
|
//set rate
|
|
if ((err = snd_pcm_hw_params_set_rate_near (capture_handle, hw_params,
|
|
&rate, 0)) < 0)
|
|
{
|
|
fprintf (stderr, "cannot set sample rate (%s)\n",
|
|
snd_strerror (err));
|
|
return NULL;
|
|
}
|
|
|
|
fprintf (stdout, "rate set\n");
|
|
|
|
// set channels
|
|
if ((err = snd_pcm_hw_params_set_channels (capture_handle,
|
|
hw_params, 1)) < 0)
|
|
{
|
|
fprintf (stderr, "cannot set channel count (%s)\n",
|
|
snd_strerror (err));
|
|
return NULL;
|
|
}
|
|
|
|
fprintf (stdout, "channels set\n");
|
|
|
|
//set all parameters
|
|
if ((err = snd_pcm_hw_params (capture_handle, hw_params)) < 0)
|
|
{
|
|
fprintf (stderr, "cannot set parameters (%s)\n",
|
|
snd_strerror (err));
|
|
return NULL;
|
|
}
|
|
|
|
fprintf(stdout, "parameters set\n");
|
|
|
|
// free the parameter structure
|
|
snd_pcm_hw_params_free (hw_params);
|
|
|
|
fprintf(stdout, "parameters freed\n");
|
|
|
|
// prepare the device
|
|
if ((err = snd_pcm_prepare (capture_handle)) < 0)
|
|
{
|
|
fprintf (stderr, "cannot prepare audio interface(%s)\n",
|
|
snd_strerror (err));
|
|
return NULL;
|
|
}
|
|
|
|
fprintf(stdout, "device prepared\n");
|
|
|
|
// allocate buffer
|
|
alsa_input_data = (INPUT_SOUND_DATA_TYPE *) malloc (ALSA_BUFFER_FRAMES *
|
|
sizeof(INPUT_SOUND_DATA_TYPE) * 2);
|
|
if (alsa_input_data == NULL)
|
|
{
|
|
fprintf (stderr, "cannot cannot allocate buffer(%s)\n",
|
|
snd_strerror (err));
|
|
return NULL;
|
|
}
|
|
|
|
fprintf(stdout,"allocated buffer\n");
|
|
return alsa_input_data;
|
|
}
|
|
|
|
|
|
int alsa_read_class::read_alsa()
|
|
{
|
|
if ((err = snd_pcm_readi (capture_handle, alsa_input_data, buffer_frames))
|
|
!= buffer_frames)
|
|
{
|
|
fprintf (stderr, "did not read device, %d (%s)\n",
|
|
err, snd_strerror (err));
|
|
}
|
|
|
|
// Perform raw volume setting
|
|
for (i = 0; i < TOTAL_ALSA_BUFFER_SIZE_IN_WORDS; i += 1)
|
|
{
|
|
*(alsa_input_data + i) = *(alsa_input_data + i) * MAIN_VOLUME;
|
|
}
|
|
|
|
return err;
|
|
}
|