2
0

SDL_diskaudio.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #ifdef SDL_AUDIO_DRIVER_DISK
  20. // Output raw audio data to a file.
  21. #include "../SDL_sysaudio.h"
  22. #include "SDL_diskaudio.h"
  23. #define DISKDEFAULT_OUTFILE "sdlaudio.raw"
  24. #define DISKDEFAULT_INFILE "sdlaudio-in.raw"
  25. static bool DISKAUDIO_WaitDevice(SDL_AudioDevice *device)
  26. {
  27. SDL_Delay(device->hidden->io_delay);
  28. return true;
  29. }
  30. static bool DISKAUDIO_PlayDevice(SDL_AudioDevice *device, const Uint8 *buffer, int buffer_size)
  31. {
  32. const int written = (int)SDL_WriteIO(device->hidden->io, buffer, (size_t)buffer_size);
  33. if (written != buffer_size) { // If we couldn't write, assume fatal error for now
  34. return false;
  35. }
  36. #ifdef DEBUG_AUDIO
  37. SDL_Log("DISKAUDIO: Wrote %d bytes of audio data", (int) written);
  38. #endif
  39. return true;
  40. }
  41. static Uint8 *DISKAUDIO_GetDeviceBuf(SDL_AudioDevice *device, int *buffer_size)
  42. {
  43. return device->hidden->mixbuf;
  44. }
  45. static int DISKAUDIO_RecordDevice(SDL_AudioDevice *device, void *buffer, int buflen)
  46. {
  47. struct SDL_PrivateAudioData *h = device->hidden;
  48. const int origbuflen = buflen;
  49. if (h->io) {
  50. const int br = (int)SDL_ReadIO(h->io, buffer, (size_t)buflen);
  51. buflen -= br;
  52. buffer = ((Uint8 *)buffer) + br;
  53. if (buflen > 0) { // EOF (or error, but whatever).
  54. SDL_CloseIO(h->io);
  55. h->io = NULL;
  56. }
  57. }
  58. // if we ran out of file, just write silence.
  59. SDL_memset(buffer, device->silence_value, buflen);
  60. return origbuflen;
  61. }
  62. static void DISKAUDIO_FlushRecording(SDL_AudioDevice *device)
  63. {
  64. // no op...we don't advance the file pointer or anything.
  65. }
  66. static void DISKAUDIO_CloseDevice(SDL_AudioDevice *device)
  67. {
  68. if (device->hidden) {
  69. if (device->hidden->io) {
  70. SDL_CloseIO(device->hidden->io);
  71. }
  72. SDL_free(device->hidden->mixbuf);
  73. SDL_free(device->hidden);
  74. device->hidden = NULL;
  75. }
  76. }
  77. static const char *get_filename(const bool recording)
  78. {
  79. const char *devname = SDL_GetHint(recording ? SDL_HINT_AUDIO_DISK_INPUT_FILE : SDL_HINT_AUDIO_DISK_OUTPUT_FILE);
  80. if (!devname) {
  81. devname = recording ? DISKDEFAULT_INFILE : DISKDEFAULT_OUTFILE;
  82. }
  83. return devname;
  84. }
  85. static bool DISKAUDIO_OpenDevice(SDL_AudioDevice *device)
  86. {
  87. bool recording = device->recording;
  88. const char *fname = get_filename(recording);
  89. device->hidden = (struct SDL_PrivateAudioData *) SDL_calloc(1, sizeof(*device->hidden));
  90. if (!device->hidden) {
  91. return false;
  92. }
  93. device->hidden->io_delay = ((device->sample_frames * 1000) / device->spec.freq);
  94. const char *hint = SDL_GetHint(SDL_HINT_AUDIO_DISK_TIMESCALE);
  95. if (hint) {
  96. double scale = SDL_atof(hint);
  97. if (scale >= 0.0) {
  98. device->hidden->io_delay = (Uint32)SDL_round(device->hidden->io_delay * scale);
  99. }
  100. }
  101. // Open the "audio device"
  102. device->hidden->io = SDL_IOFromFile(fname, recording ? "rb" : "wb");
  103. if (!device->hidden->io) {
  104. return false;
  105. }
  106. // Allocate mixing buffer
  107. if (!recording) {
  108. device->hidden->mixbuf = (Uint8 *)SDL_malloc(device->buffer_size);
  109. if (!device->hidden->mixbuf) {
  110. return false;
  111. }
  112. SDL_memset(device->hidden->mixbuf, device->silence_value, device->buffer_size);
  113. }
  114. SDL_LogCritical(SDL_LOG_CATEGORY_AUDIO, "You are using the SDL disk i/o audio driver!");
  115. SDL_LogCritical(SDL_LOG_CATEGORY_AUDIO, " %s file [%s].\n", recording ? "Reading from" : "Writing to", fname);
  116. return true; // We're ready to rock and roll. :-)
  117. }
  118. static void DISKAUDIO_DetectDevices(SDL_AudioDevice **default_playback, SDL_AudioDevice **default_recording)
  119. {
  120. *default_playback = SDL_AddAudioDevice(false, DEFAULT_PLAYBACK_DEVNAME, NULL, (void *)0x1);
  121. *default_recording = SDL_AddAudioDevice(true, DEFAULT_RECORDING_DEVNAME, NULL, (void *)0x2);
  122. }
  123. static bool DISKAUDIO_Init(SDL_AudioDriverImpl *impl)
  124. {
  125. impl->OpenDevice = DISKAUDIO_OpenDevice;
  126. impl->WaitDevice = DISKAUDIO_WaitDevice;
  127. impl->WaitRecordingDevice = DISKAUDIO_WaitDevice;
  128. impl->PlayDevice = DISKAUDIO_PlayDevice;
  129. impl->GetDeviceBuf = DISKAUDIO_GetDeviceBuf;
  130. impl->RecordDevice = DISKAUDIO_RecordDevice;
  131. impl->FlushRecording = DISKAUDIO_FlushRecording;
  132. impl->CloseDevice = DISKAUDIO_CloseDevice;
  133. impl->DetectDevices = DISKAUDIO_DetectDevices;
  134. impl->HasRecordingSupport = true;
  135. return true;
  136. }
  137. AudioBootStrap DISKAUDIO_bootstrap = {
  138. "disk", "direct-to-disk audio", DISKAUDIO_Init, true
  139. };
  140. #endif // SDL_AUDIO_DRIVER_DISK