/src/Core/objects/AudioSpec.xs

http://github.com/PerlGameDev/SDL · Unknown · 154 lines · 125 code · 29 blank · 0 comment · 0 complexity · b41966f11e5bb8f9975c78905532b3b9 MD5 · raw file

  1. #include "EXTERN.h"
  2. #include "perl.h"
  3. #include "XSUB.h"
  4. #include "ppport.h"
  5. #include "helper.h"
  6. #include "defines.h"
  7. #ifndef aTHX_
  8. #define aTHX_
  9. #endif
  10. #ifdef USE_THREADS
  11. #define HAVE_TLS_CONTEXT
  12. #endif
  13. #include <SDL.h>
  14. #include <SDL_audio.h>
  15. void
  16. audio_callback ( void* data, Uint8 *stream, int len )
  17. {
  18. ENTER_TLS_CONTEXT;
  19. dSP;
  20. char* string = (char*)stream;
  21. SV* sv = newSVpv("a",1);
  22. SvCUR_set(sv,len * sizeof(Uint8));
  23. SvLEN_set(sv,len * sizeof(Uint8));
  24. void* old = SvPVX(sv);
  25. SvPV_set(sv,string);
  26. ENTER;
  27. SAVETMPS;
  28. PUSHMARK(SP);
  29. XPUSHs(sv_2mortal(newSViv(sizeof(Uint8))));
  30. XPUSHs(sv_2mortal(newSViv(len)));
  31. XPUSHs(sv_2mortal(newRV_inc(sv)));
  32. PUTBACK;
  33. call_pv(data,G_VOID|G_DISCARD);
  34. SvPV_set(sv,old);
  35. SvCUR_set(sv,1);
  36. SvLEN_set(sv,1);
  37. sv_2mortal(sv);
  38. FREETMPS;
  39. LEAVE;
  40. LEAVE_TLS_CONTEXT;
  41. }
  42. MODULE = SDL::AudioSpec PACKAGE = SDL::AudioSpec PREFIX = audiospec_
  43. =for documentation
  44. SDL_AudioSpec -- Audio specification
  45. /* The calculated values in this structure are calculated by SDL_OpenAudio() */
  46. typedef struct SDL_AudioSpec {
  47. int freq; /* DSP frequency -- samples per second */
  48. Uint16 format; /* Audio data format */
  49. Uint8 channels; /* Number of channels: 1 mono, 2 stereo */
  50. Uint8 silence; /* Audio buffer silence value (calculated) */
  51. Uint16 samples; /* Audio buffer size in samples (power of 2) */
  52. Uint16 padding; /* Necessary for some compile environments */
  53. Uint32 size; /* Audio buffer size in bytes (calculated) */
  54. /* This function is called when the audio device needs more data.
  55. 'stream' is a pointer to the audio data buffer
  56. 'len' is the length of that buffer in bytes.
  57. Once the callback returns, the buffer will no longer be valid.
  58. Stereo samples are stored in a LRLRLR ordering.
  59. */
  60. void (SDLCALL *callback)(void *userdata, Uint8 *stream, int len);
  61. void *userdata;
  62. } SDL_AudioSpec;
  63. =cut
  64. SDL_AudioSpec *
  65. audiospec_new (CLASS)
  66. char* CLASS
  67. CODE:
  68. RETVAL = safemalloc(sizeof(SDL_AudioSpec));
  69. OUTPUT:
  70. RETVAL
  71. int
  72. audiospec_freq ( audiospec, ... )
  73. SDL_AudioSpec *audiospec
  74. CODE:
  75. if (items > 1 ) audiospec->freq = SvIV(ST(1));
  76. RETVAL = audiospec->freq;
  77. OUTPUT:
  78. RETVAL
  79. Uint16
  80. audiospec_format ( audiospec, ... )
  81. SDL_AudioSpec *audiospec
  82. CODE:
  83. if (items > 1 ) audiospec->format = SvIV(ST(1));
  84. RETVAL = audiospec->format;
  85. OUTPUT:
  86. RETVAL
  87. Uint8
  88. audiospec_channels ( audiospec, ... )
  89. SDL_AudioSpec *audiospec
  90. CODE:
  91. if (items > 1 ) audiospec->channels = SvIV(ST(1));
  92. RETVAL = audiospec->channels;
  93. OUTPUT:
  94. RETVAL
  95. Uint16
  96. audiospec_samples ( audiospec, ... )
  97. SDL_AudioSpec *audiospec
  98. CODE:
  99. if (items > 1 ) audiospec->samples = SvIV(ST(1));
  100. RETVAL = audiospec->samples;
  101. OUTPUT:
  102. RETVAL
  103. #ifdef USE_THREADS
  104. void
  105. audiospec_callback( audiospec, cb )
  106. SDL_AudioSpec *audiospec
  107. char* cb
  108. CODE:
  109. /* the audio callback will happen in a different thread. */
  110. GET_TLS_CONTEXT;
  111. audiospec->userdata = cb;
  112. audiospec->callback = audio_callback;
  113. #else
  114. void
  115. audiospec_callback( audiospec, cb )
  116. SDL_AudioSpec *audiospec
  117. char* cb
  118. CODE:
  119. warn("Perl need to be compiled with 'useithreads' for SDL::AudioSpec::callback( cb )");
  120. #endif
  121. void
  122. audiospec_DESTROY(bag)
  123. SV *bag
  124. CODE:
  125. objDESTROY(bag, safefree);