Not sure if I should make new thread with this, but I am having issue with playing sound. It seems that no sound is playing out of my speakers.
This is my callback. This callback is being called by SDL, so I am not forgetting an SDL_PauseAudio(0) or anything like that.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 | static void SDLAudioCallBack(void* userData, uint8_t* stream, int len) {
int16_t* currSample = (int16_t*)stream;
uint32_t tone = 256;
uint32_t period = 48000 / tone;
uint32_t halfPeriod = period / 2;
for (size_t i = 0; i < (len / sizeof(*currSample)); i+=2) {
int16_t halfSample = ((i / halfPeriod) % 2) ? 1 : -1;
currSample[i] = halfSample * 3000; //left channel
currSample[i+1] = halfSample * 3000; //right channel
}
}
|
This is my audio spec
| SDL_AudioSpec desiredAudio = {};
desiredAudio.channels = 2;
desiredAudio.samples = 4096;
desiredAudio.freq = SOUND_FREQ;
desiredAudio.format = AUDIO_S16LSB;
desiredAudio.callback = SDLAudioCallBack;
if (SDL_OpenAudio(&desiredAudio, NULL) < 0) {
printSDLErrorAndExit();
}
|
Any help would be greatly appreciated! Thanks!
Also, in addition, it seems that _rdtsc() or __rdtsc() does not seem to be in x86intrin.h on Mac or Linux Mint for whatever reason (perhaps I am compiling with the wrong flags? I am compiling in C using clang, not g++. Maybe it's not in clang's std library?). As a workaround, I found that this implimentation seems to work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 | static inline uint64_t
__rdtsc(void)
{
uint32_t eax = 0, edx;
__asm__ __volatile__("cpuid;"
"rdtsc;"
: "+a" (eax), "=d" (edx)
:
: "%rcx", "%rbx", "memory");
__asm__ __volatile__("xorl %%eax, %%eax;"
"cpuid;"
:
:
: "%rax", "%rbx", "%rcx", "%rdx", "memory");
return (((uint64_t)edx << 32) | eax);
}
|
Just as a suggestion: If it turns out that these functions really are missing on these platforms and this implementation is good enough, would you want to update the tutorial with this implementation?
Thanks!