-
Notifications
You must be signed in to change notification settings - Fork 1
/
audio.cpp
121 lines (97 loc) · 3.6 KB
/
audio.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <exception>
#include <iostream>
#include "audio.h"
#define ALSA_BUFFER_SIZE 80000
Audio::Audio(AudioMode mode) {
volume = 0;
playbackBufferSize = 0;
// Open the PCM input/output depending on audio mode
if (mode == CAPTURE) {
setCaptureVolume(DEFAULT_CAPTURE_VOLUME);
int err = snd_pcm_open(&handle, "default", SND_PCM_STREAM_CAPTURE, 0);
if (err < 0) {
printf("Capture open error: %s\n", snd_strerror(err));
exit(EXIT_FAILURE);
}
}
else if (mode == PLAYBACK) {
setPlaybackVolume(DEFAULT_PLAYBACK_VOLUME);
int err = snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, 0);
if (err < 0) {
printf("Playback open error: %s\n", snd_strerror(err));
exit(EXIT_FAILURE);
}
}
else {
throw new std::invalid_argument("The audio mode specified is not recognized. ");
}
// Configure parameters of PCM output
int err = snd_pcm_set_params(handle,
// SND_PCM_FORMAT_FLOAT64_LE,
SND_PCM_FORMAT_FLOAT_LE,
SND_PCM_ACCESS_RW_INTERLEAVED,
NUM_CHANNELS,
SAMPLE_RATE,
1, // Allow software resampling
ALSA_BUFFER_SIZE); // 0.08 seconds per buffer
if (err < 0) {
printf("Playback open error: %s\n", snd_strerror(err));
exit(EXIT_FAILURE);
}
// Allocate this software's playback buffer to be the same size as the
// the hardware's playback buffers for efficient data transfers.
// ..get info on the hardware buffers:
unsigned long unusedBufferSize = 0;
snd_pcm_get_params(handle, &unusedBufferSize, &playbackBufferSize);
}
Audio::~Audio() {
threadObj.join();
}
void Audio::setPlaybackVolume(int newVolume) {
// Ensure volume is reasonable; If so, cache it for later getVolume() calls.
if (newVolume < 0 || newVolume > MAX_VOLUME) {
std::cout << "ERROR: Volume must be between 0 and 100.\n" << std::endl;
return;
}
volume = newVolume;
long min, max;
snd_mixer_t *handle;
snd_mixer_selem_id_t *sid;
const char *card = "default";
const char *selem_name = "PCM";
snd_mixer_open(&handle, 0);
snd_mixer_attach(handle, card);
snd_mixer_selem_register(handle, NULL, NULL);
snd_mixer_load(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(handle, sid);
snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
snd_mixer_selem_set_playback_volume_all(elem, volume * max / 100);
snd_mixer_close(handle);
}
void Audio::setCaptureVolume(int newVolume) {
// Ensure volume is reasonable; If so, cache it for later getVolume() calls.
if (newVolume < 0 || newVolume > MAX_VOLUME) {
std::cout << "ERROR: Volume must be between 0 and 100.\n" << std::endl;
return;
}
volume = newVolume;
long min, max;
snd_mixer_t *handle;
snd_mixer_selem_id_t *sid;
const char *card = "default";
const char *selem_name = "PGA";
snd_mixer_open(&handle, 0);
snd_mixer_attach(handle, card);
snd_mixer_selem_register(handle, NULL, NULL);
snd_mixer_load(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(handle, sid);
snd_mixer_selem_get_capture_volume_range(elem, &min, &max);
snd_mixer_selem_set_capture_volume_all(elem, volume * max / 100);
snd_mixer_close(handle);
}