forked from ggerganov/kbd-audio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplay-full.cpp
90 lines (69 loc) · 2.41 KB
/
play-full.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
/*! \file play-full.cpp
* \brief Enter description here.
* \author Georgi Gerganov
*/
#include "audio_logger.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_audio.h>
#include <fstream>
bool g_terminate = false;
void cbPlayback(void * userdata, uint8_t * stream, int len) {
std::ifstream * fin = (std::ifstream *)(userdata);
if (fin->eof()) {
g_terminate = true;
return;
}
fflush(stdout);
fin->read((char *)(stream), len); // todo
if (fin->eof()) g_terminate = true;
}
int main(int argc, char ** argv) {
if (argc < 2) {
fprintf(stderr, "Usage: %s input.kbd\n", argv[0]);
return -127;
}
std::ifstream fin(argv[1], std::ios::binary);
if (fin.good() == false) {
fprintf(stderr, "Failed to open file '%s'\n", argv[1]);
return -1;
}
int bufferSize_frames = 1;
if (SDL_Init(SDL_INIT_AUDIO) < 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
return -1;
}
int nDevices = SDL_GetNumAudioDevices(SDL_FALSE);
printf("Found %d playback devices:\n", nDevices);
for (int i = 0; i < nDevices; i++) {
printf(" - Playback device #%d: '%s'\n", i, SDL_GetAudioDeviceName(i, SDL_FALSE));
}
SDL_AudioSpec playbackSpec;
SDL_zero(playbackSpec);
playbackSpec.freq = 24000;
playbackSpec.format = AUDIO_F32SYS;
playbackSpec.channels = 1;
playbackSpec.samples = bufferSize_frames*AudioLogger::kSamplesPerFrame;
playbackSpec.callback = cbPlayback;
playbackSpec.userdata = (void *)(&fin);
SDL_AudioSpec obtainedSpec;
SDL_zero(obtainedSpec);
auto deviceIdOut = SDL_OpenAudioDevice(NULL, SDL_FALSE, &playbackSpec, &obtainedSpec, 0);
if (!deviceIdOut) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open an audio device for playback: %s!\n", SDL_GetError());
SDL_Quit();
return -2;
}
int sampleSize_bytes = 4; // todo
printf("Opened playback device %d\n", deviceIdOut);
printf(" Frequency: %d\n", obtainedSpec.freq);
printf(" Format: %d (%d bytes)\n", obtainedSpec.format, sampleSize_bytes);
printf(" Channels: %d\n", obtainedSpec.channels);
printf(" Samples: %d\n", obtainedSpec.samples);
SDL_PauseAudioDevice(deviceIdOut, 0);
while(g_terminate == false) {
SDL_Delay(100);
}
fin.close();
SDL_CloseAudio();
return 0;
}