-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsoundthread.c
More file actions
52 lines (44 loc) · 1.68 KB
/
soundthread.c
File metadata and controls
52 lines (44 loc) · 1.68 KB
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
/******************************************************************************
File: soundthread.c
Created: 2019-07-25
Updated: 2019-07-31
Author: Aaron Oman
Notice: Creative Commons Attribution 4.0 International License (CC-BY 4.0)
******************************************************************************/
//! \file soundthread.c
//! \brief Thread for sound playback
//!
//! Sound playback is triggered via the CHIP-8's sound timer.
//! When the timer reaches zero, a sound is played back.
//! The specifications seem loose on what this means exactly, so this emulator
//! plays back a tone of 440hz for 200ms.
//!
//! \param[in] context struct thread_args casted to void*
//! \return NULL
void *SoundThread(void *context) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpointer-arith"
struct thread_args *ctx = (struct thread_args *)context;
#pragma GCC diagnostic pop
struct sound *sound = SoundInit();
if (NULL == sound) {
fprintf(stderr, "Couldn't initialize sound");
return NULL;
}
struct timer *timer = TimerInit(200);
int playing = 0;
while (!ThreadSyncShouldShutdown(ctx->threadSync)) {
if (SystemSoundTriggered(ctx->sys)) {
TimerReset(timer);
SystemSoundSetTrigger(ctx->sys, 0);
playing = 1;
SoundPlay(sound);
}
if (playing && TimerHasElapsed(timer)) {
SoundStop(sound);
playing = 0;
}
}
SoundDeinit(sound);
return NULL;
}