-
Notifications
You must be signed in to change notification settings - Fork 0
/
sound.cpp
76 lines (62 loc) · 1.67 KB
/
sound.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
#include "sound.h"
#include "./submodules/chip8asm/src/Logger.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_audio.h>
#define BEEP_AMPLITUDE 2800
#define BEEP_SAMPLE_RATE 44100
#define BEEP_FREQ 1500.0f
static void audioCallback(void *userData, Uint8* _buffer, int byteCount)
{
Sint16* buffer = (Sint16*)_buffer;
const int length = byteCount/2; // 2 bytes per sample
int &sampleI = *(int*)userData;
for (int i{}; i < length; ++i, ++sampleI)
{
const double time = (double)sampleI/BEEP_SAMPLE_RATE;
buffer[i] = BEEP_AMPLITUDE*sin(M_PI*2*time*BEEP_FREQ);
}
}
Beeper::Beeper()
{
m_couldInit = false;
m_sampleI = 0;
SDL_AudioSpec want;
want.freq = BEEP_SAMPLE_RATE;
want.format = AUDIO_S16SYS; // Signed 16-bit sample type
want.channels = 1;
want.samples = 2048;
want.callback = audioCallback; // SDL calls it to refill the buffer
want.userdata = (void*)&m_sampleI;
SDL_AudioSpec have;
if (SDL_OpenAudio(&want, &have))
{
Logger::err << "Failed to open audio: " << SDL_GetError() << Logger::End;
return;
}
if (want.format != have.format)
{
Logger::err << "Failed to get desired AudioSpec: " << SDL_GetError() << Logger::End;
return;
}
Logger::log << "Opened and set up audio device" << Logger::End;
m_couldInit = true;
}
void Beeper::startBeeping() const
{
if (!m_couldInit)
return;
SDL_PauseAudio(0);
}
void Beeper::stopBeeping() const
{
if (!m_couldInit)
return;
SDL_PauseAudio(1);
}
Beeper::~Beeper()
{
if (!m_couldInit)
return;
SDL_CloseAudio();
Logger::log << "Closed audio device" << Logger::End;
}