| Summary: | SDL_OpenAudioDevice with NULL obtained variable behaves as though requested samples changes | ||
|---|---|---|---|
| Product: | SDL | Reporter: | Gabriel Marcano <gabemarcano> |
| Component: | audio | Assignee: | Ryan C. Gordon <icculus> |
| Status: | RESOLVED INVALID | QA Contact: | Sam Lantinga <slouken> |
| Severity: | normal | ||
| Priority: | P2 | CC: | gabemarcano |
| Version: | 2.0.3 | ||
| Hardware: | x86_64 | ||
| OS: | Linux | ||
| Attachments: | main.c, Sample file showcasing issue | ||
It seems I misread the documentation. I assumed the length from the callback was the number of samples requested by SDL, but in reality it is the number of bytes in the buffer. In my case, each mono sample is actually of two bytes. After fixing my code to take this difference into account, sound works as it should. It still seems that the PulseAudio code cuts samples in half internally, but SDL behaves consistent with its documentation (again, the callback receives the number of bytes in the buffer instead of the number of samples). I apologize for the noise. Keep up the good work! |
Created attachment 2228 [details] main.c, Sample file showcasing issue I am using libSDL2-2.0.3 on Gentoo Linux. While trying to play a simple square wave with SDL, I realized that for some reason the number of samples I was requesting in the wanted SDL_AudioSpec struct for SDL_OpenAudioDevice() was being cut in half without telling me, since the obtained pointer was set to NULL (the documentation states that SDL should convert things on its own in the background if conversion is needed). Some more things I've noticed: When the callback function was called by SDL, the length variable passed to it still stated the original buffer size I asked for, but when the sound plays back I did not hear a clean square wave. It was only when I modified my generation algorithm to only work on the first half of the buffer that the clean square wave worked. I compiled SDL with debugging symbols and stepped though. I found that for opening a pulseaudio device (which is what I have), the samples get halved (in src/audio/pulseaudio/SDL_pulseaudio.c:413). Maybe this is related somehow? I tried commenting this out and recompiling SDL, but then the callback function was getting a length size of double my requested sample size, so there may be more code doing more doubling/halving elsewhere. I'm attaching a simple program that exhibits the issue. The function generate_samples just generates the given number of samples for a 16 bit native byte ordering PCM square wave, and stores the samples in a global array. The function keeps track of the current position in the wave period, so that it can begin, when called again, where it left off. The function fill_audio is the callback registered with SDL. There are two generate_samples lines (one should be commented out). The one with just len shows the default SDL behavior, and the one with len/2 shows my workaround. The default behavior should cause for the square wave to sound, well, like crap. With my workaround (by only using the first half of the buffer), causes a nice square wave to sound. I compiled the program using GCC-5.1 using C99. I can provide more information if necessary.