Added audio stream conversion functions:
authorSam Lantinga <slouken@libsdl.org>
Wed, 18 Oct 2017 15:54:05 -0700
changeset 11631150b925ddfbb
parent 11630 ad4b5d85edee
child 11632 385312bd6594
Added audio stream conversion functions:
SDL_NewAudioStream
SDL_AudioStreamPut
SDL_AudioStreamGet
SDL_AudioStreamAvailable
SDL_AudioStreamClear
SDL_FreeAudioStream
WhatsNew.txt
include/SDL_audio.h
src/audio/SDL_audio_c.h
src/audio/SDL_audiocvt.c
src/dynapi/SDL_dynapi_overrides.h
src/dynapi/SDL_dynapi_procs.h
     1.1 --- a/WhatsNew.txt	Wed Oct 18 08:52:04 2017 -0700
     1.2 +++ b/WhatsNew.txt	Wed Oct 18 15:54:05 2017 -0700
     1.3 @@ -6,6 +6,13 @@
     1.4  ---------------------------------------------------------------------------
     1.5  
     1.6  General:
     1.7 +* Added audio stream conversion functions:
     1.8 +	SDL_NewAudioStream
     1.9 +	SDL_AudioStreamPut
    1.10 +	SDL_AudioStreamGet
    1.11 +	SDL_AudioStreamAvailable
    1.12 +	SDL_AudioStreamClear
    1.13 +	SDL_FreeAudioStream
    1.14  * Added functions to query and set the SDL memory allocation functions:
    1.15  	SDL_GetMemoryFunctions()
    1.16  	SDL_SetMemoryFunctions()
     2.1 --- a/include/SDL_audio.h	Wed Oct 18 08:52:04 2017 -0700
     2.2 +++ b/include/SDL_audio.h	Wed Oct 18 15:54:05 2017 -0700
     2.3 @@ -477,6 +477,106 @@
     2.4   */
     2.5  extern DECLSPEC int SDLCALL SDL_ConvertAudio(SDL_AudioCVT * cvt);
     2.6  
     2.7 +/* SDL_AudioStream is a new audio conversion interface.
     2.8 +   The benefits vs SDL_AudioCVT:
     2.9 +    - it can handle resampling data in chunks without generating
    2.10 +      artifacts, when it doesn't have the complete buffer available.
    2.11 +    - it can handle incoming data in any variable size.
    2.12 +    - You push data as you have it, and pull it when you need it
    2.13 + */
    2.14 +/* this is opaque to the outside world. */
    2.15 +struct _SDL_AudioStream;
    2.16 +typedef struct _SDL_AudioStream SDL_AudioStream;
    2.17 +
    2.18 +/**
    2.19 + *  Create a new audio stream
    2.20 + *
    2.21 + *  \param src_format The format of the source audio
    2.22 + *  \param src_channels The number of channels of the source audio
    2.23 + *  \param src_rate The sampling rate of the source audio
    2.24 + *  \param dst_format The format of the desired audio output
    2.25 + *  \param dst_channels The number of channels of the desired audio output
    2.26 + *  \param dst_rate The sampling rate of the desired audio output
    2.27 + *  \return 0 on success, or -1 on error.
    2.28 + *
    2.29 + *  \sa SDL_AudioStreamPut
    2.30 + *  \sa SDL_AudioStreamGet
    2.31 + *  \sa SDL_AudioStreamAvailable
    2.32 + *  \sa SDL_AudioStreamClear
    2.33 + *  \sa SDL_FreeAudioStream
    2.34 + */
    2.35 +extern DECLSPEC SDL_AudioStream * SDLCALL SDL_NewAudioStream(const SDL_AudioFormat src_format,
    2.36 +                                           const Uint8 src_channels,
    2.37 +                                           const int src_rate,
    2.38 +                                           const SDL_AudioFormat dst_format,
    2.39 +                                           const Uint8 dst_channels,
    2.40 +                                           const int dst_rate);
    2.41 +
    2.42 +/**
    2.43 + *  Add data to be converted/resampled to the stream
    2.44 + *
    2.45 + *  \param stream The stream the audio data is being added to
    2.46 + *  \param buf A pointer to the audio data to add
    2.47 + *  \param int The number of bytes to write to the stream
    2.48 + *  \return 0 on success, or -1 on error.
    2.49 + *
    2.50 + *  \sa SDL_NewAudioStream
    2.51 + *  \sa SDL_AudioStreamGet
    2.52 + *  \sa SDL_AudioStreamAvailable
    2.53 + *  \sa SDL_AudioStreamClear
    2.54 + *  \sa SDL_FreeAudioStream
    2.55 + */
    2.56 +extern DECLSPEC int SDLCALL SDL_AudioStreamPut(SDL_AudioStream *stream, const void *buf, int len);
    2.57 +
    2.58 +/**
    2.59 + *  Get converted/resampled data from the stream
    2.60 + *
    2.61 + *  \param stream The stream the audio is being requested from
    2.62 + *  \param buf A buffer to fill with audio data
    2.63 + *  \param len The maximum number of bytes to fill
    2.64 + *  \return The number of bytes read from the stream, or -1 on error
    2.65 + *
    2.66 + *  \sa SDL_NewAudioStream
    2.67 + *  \sa SDL_AudioStreamPut
    2.68 + *  \sa SDL_AudioStreamAvailable
    2.69 + *  \sa SDL_AudioStreamClear
    2.70 + *  \sa SDL_FreeAudioStream
    2.71 + */
    2.72 +extern DECLSPEC int SDLCALL SDL_AudioStreamGet(SDL_AudioStream *stream, void *buf, int len);
    2.73 +
    2.74 +/**
    2.75 + * Get the number of converted/resampled bytes available
    2.76 + *
    2.77 + *  \sa SDL_NewAudioStream
    2.78 + *  \sa SDL_AudioStreamPut
    2.79 + *  \sa SDL_AudioStreamGet
    2.80 + *  \sa SDL_AudioStreamClear
    2.81 + *  \sa SDL_FreeAudioStream
    2.82 + */
    2.83 +extern DECLSPEC int SDLCALL SDL_AudioStreamAvailable(SDL_AudioStream *stream);
    2.84 +
    2.85 +/**
    2.86 + *  Clear any pending data in the stream without converting it
    2.87 + *
    2.88 + *  \sa SDL_NewAudioStream
    2.89 + *  \sa SDL_AudioStreamPut
    2.90 + *  \sa SDL_AudioStreamGet
    2.91 + *  \sa SDL_AudioStreamAvailable
    2.92 + *  \sa SDL_FreeAudioStream
    2.93 + */
    2.94 +extern DECLSPEC void SDLCALL SDL_AudioStreamClear(SDL_AudioStream *stream);
    2.95 +
    2.96 +/**
    2.97 + * Free an audio stream
    2.98 + *
    2.99 + *  \sa SDL_NewAudioStream
   2.100 + *  \sa SDL_AudioStreamPut
   2.101 + *  \sa SDL_AudioStreamGet
   2.102 + *  \sa SDL_AudioStreamAvailable
   2.103 + *  \sa SDL_AudioStreamClear
   2.104 + */
   2.105 +extern DECLSPEC void SDLCALL SDL_FreeAudioStream(SDL_AudioStream *stream);
   2.106 +
   2.107  #define SDL_MIX_MAXVOLUME 128
   2.108  /**
   2.109   *  This takes two audio buffers of the playing audio format and mixes
   2.110 @@ -532,7 +632,7 @@
   2.111   *  \param dev The device ID to which we will queue audio.
   2.112   *  \param data The data to queue to the device for later playback.
   2.113   *  \param len The number of bytes (not samples!) to which (data) points.
   2.114 - *  \return zero on success, -1 on error.
   2.115 + *  \return 0 on success, or -1 on error.
   2.116   *
   2.117   *  \sa SDL_GetQueuedAudioSize
   2.118   *  \sa SDL_ClearQueuedAudio
     3.1 --- a/src/audio/SDL_audio_c.h	Wed Oct 18 08:52:04 2017 -0700
     3.2 +++ b/src/audio/SDL_audio_c.h	Wed Oct 18 15:54:05 2017 -0700
     3.3 @@ -74,46 +74,6 @@
     3.4  extern int SDL_PrepareResampleFilter(void);
     3.5  extern void SDL_FreeResampleFilter(void);
     3.6  
     3.7 -
     3.8 -/* SDL_AudioStream is a new audio conversion interface. It
     3.9 -    might eventually become a public API.
    3.10 -   The benefits vs SDL_AudioCVT:
    3.11 -    - it can handle resampling data in chunks without generating
    3.12 -      artifacts, when it doesn't have the complete buffer available.
    3.13 -    - it can handle incoming data in any variable size.
    3.14 -    - You push data as you have it, and pull it when you need it
    3.15 -
    3.16 -    (Note that currently this converts as data is put into the stream, so
    3.17 -    you need to push more than a handful of bytes if you want decent
    3.18 -    resampling. This can be changed later.)
    3.19 - */
    3.20 -
    3.21 -/* this is opaque to the outside world. */
    3.22 -typedef struct SDL_AudioStream SDL_AudioStream;
    3.23 -
    3.24 -/* create a new stream */
    3.25 -extern SDL_AudioStream *SDL_NewAudioStream(const SDL_AudioFormat src_format,
    3.26 -                                           const Uint8 src_channels,
    3.27 -                                           const int src_rate,
    3.28 -                                           const SDL_AudioFormat dst_format,
    3.29 -                                           const Uint8 dst_channels,
    3.30 -                                           const int dst_rate);
    3.31 -
    3.32 -/* add data to be converted/resampled to the stream */
    3.33 -extern int SDL_AudioStreamPut(SDL_AudioStream *stream, const void *buf, const Uint32 len);
    3.34 -
    3.35 -/* get converted/resampled data from the stream */
    3.36 -extern int SDL_AudioStreamGet(SDL_AudioStream *stream, void *buf, const Uint32 len);
    3.37 -
    3.38 -/* clear any pending data in the stream without converting it. */
    3.39 -extern void SDL_AudioStreamClear(SDL_AudioStream *stream);
    3.40 -
    3.41 -/* number of converted/resampled bytes available */
    3.42 -extern int SDL_AudioStreamAvailable(SDL_AudioStream *stream);
    3.43 -
    3.44 -/* dispose of a stream */
    3.45 -extern void SDL_FreeAudioStream(SDL_AudioStream *stream);
    3.46 -
    3.47  #endif /* SDL_audio_c_h_ */
    3.48  
    3.49  /* vi: set ts=4 sw=4 expandtab: */
     4.1 --- a/src/audio/SDL_audiocvt.c	Wed Oct 18 08:52:04 2017 -0700
     4.2 +++ b/src/audio/SDL_audiocvt.c	Wed Oct 18 15:54:05 2017 -0700
     4.3 @@ -1077,7 +1077,7 @@
     4.4  typedef void (*SDL_ResetAudioStreamResamplerFunc)(SDL_AudioStream *stream);
     4.5  typedef void (*SDL_CleanupAudioStreamResamplerFunc)(SDL_AudioStream *stream);
     4.6  
     4.7 -struct SDL_AudioStream
     4.8 +struct _SDL_AudioStream
     4.9  {
    4.10      SDL_AudioCVT cvt_before_resampling;
    4.11      SDL_AudioCVT cvt_after_resampling;
    4.12 @@ -1349,9 +1349,9 @@
    4.13  }
    4.14  
    4.15  int
    4.16 -SDL_AudioStreamPut(SDL_AudioStream *stream, const void *buf, const Uint32 _buflen)
    4.17 +SDL_AudioStreamPut(SDL_AudioStream *stream, const void *buf, int len)
    4.18  {
    4.19 -    int buflen = (int) _buflen;
    4.20 +    int buflen = len;
    4.21      int workbuflen;
    4.22      Uint8 *workbuf;
    4.23      Uint8 *resamplebuf = NULL;
    4.24 @@ -1495,34 +1495,19 @@
    4.25      return buflen ? SDL_WriteToDataQueue(stream->queue, resamplebuf, buflen) : 0;
    4.26  }
    4.27  
    4.28 -void
    4.29 -SDL_AudioStreamClear(SDL_AudioStream *stream)
    4.30 -{
    4.31 -    if (!stream) {
    4.32 -        SDL_InvalidParamError("stream");
    4.33 -    } else {
    4.34 -        SDL_ClearDataQueue(stream->queue, stream->packetlen * 2);
    4.35 -        if (stream->reset_resampler_func) {
    4.36 -            stream->reset_resampler_func(stream);
    4.37 -        }
    4.38 -        stream->first_run = SDL_TRUE;
    4.39 -    }
    4.40 -}
    4.41 -
    4.42 -
    4.43  /* get converted/resampled data from the stream */
    4.44  int
    4.45 -SDL_AudioStreamGet(SDL_AudioStream *stream, void *buf, const Uint32 len)
    4.46 +SDL_AudioStreamGet(SDL_AudioStream *stream, void *buf, int len)
    4.47  {
    4.48      #if DEBUG_AUDIOSTREAM
    4.49 -    printf("AUDIOSTREAM: want to get %u converted bytes\n", (unsigned int) len);
    4.50 +    printf("AUDIOSTREAM: want to get %d converted bytes\n", len);
    4.51      #endif
    4.52  
    4.53      if (!stream) {
    4.54          return SDL_InvalidParamError("stream");
    4.55      } else if (!buf) {
    4.56          return SDL_InvalidParamError("buf");
    4.57 -    } else if (len == 0) {
    4.58 +    } else if (len <= 0) {
    4.59          return 0;  /* nothing to do. */
    4.60      } else if ((len % stream->dst_sample_frame_size) != 0) {
    4.61          return SDL_SetError("Can't request partial sample frames");
    4.62 @@ -1538,6 +1523,20 @@
    4.63      return stream ? (int) SDL_CountDataQueue(stream->queue) : 0;
    4.64  }
    4.65  
    4.66 +void
    4.67 +SDL_AudioStreamClear(SDL_AudioStream *stream)
    4.68 +{
    4.69 +    if (!stream) {
    4.70 +        SDL_InvalidParamError("stream");
    4.71 +    } else {
    4.72 +        SDL_ClearDataQueue(stream->queue, stream->packetlen * 2);
    4.73 +        if (stream->reset_resampler_func) {
    4.74 +            stream->reset_resampler_func(stream);
    4.75 +        }
    4.76 +        stream->first_run = SDL_TRUE;
    4.77 +    }
    4.78 +}
    4.79 +
    4.80  /* dispose of a stream */
    4.81  void
    4.82  SDL_FreeAudioStream(SDL_AudioStream *stream)
     5.1 --- a/src/dynapi/SDL_dynapi_overrides.h	Wed Oct 18 08:52:04 2017 -0700
     5.2 +++ b/src/dynapi/SDL_dynapi_overrides.h	Wed Oct 18 15:54:05 2017 -0700
     5.3 @@ -640,3 +640,9 @@
     5.4  #define SDL_GetMemoryFunctions SDL_GetMemoryFunctions_REAL
     5.5  #define SDL_SetMemoryFunctions SDL_SetMemoryFunctions_REAL
     5.6  #define SDL_GetNumAllocations SDL_GetNumAllocations_REAL
     5.7 +#define SDL_NewAudioStream SDL_NewAudioStream_REAL
     5.8 +#define SDL_AudioStreamPut SDL_AudioStreamPut_REAL
     5.9 +#define SDL_AudioStreamGet SDL_AudioStreamGet_REAL
    5.10 +#define SDL_AudioStreamClear SDL_AudioStreamClear_REAL
    5.11 +#define SDL_AudioStreamAvailable SDL_AudioStreamAvailable_REAL
    5.12 +#define SDL_FreeAudioStream SDL_FreeAudioStream_REAL
     6.1 --- a/src/dynapi/SDL_dynapi_procs.h	Wed Oct 18 08:52:04 2017 -0700
     6.2 +++ b/src/dynapi/SDL_dynapi_procs.h	Wed Oct 18 15:54:05 2017 -0700
     6.3 @@ -674,3 +674,9 @@
     6.4  SDL_DYNAPI_PROC(void,SDL_GetMemoryFunctions,(SDL_malloc_func *a, SDL_calloc_func *b, SDL_realloc_func *c, SDL_free_func *d),(a,b,c,d),)
     6.5  SDL_DYNAPI_PROC(int,SDL_SetMemoryFunctions,(SDL_malloc_func a, SDL_calloc_func b, SDL_realloc_func c, SDL_free_func d),(a,b,c,d),return)
     6.6  SDL_DYNAPI_PROC(int,SDL_GetNumAllocations,(void),(),return)
     6.7 +SDL_DYNAPI_PROC(SDL_AudioStream*,SDL_NewAudioStream,(const SDL_AudioFormat a, const Uint8 b, const int c, const SDL_AudioFormat d, const Uint8 e, const int f),(a,b,c,d,e,f),return)
     6.8 +SDL_DYNAPI_PROC(int,SDL_AudioStreamPut,(SDL_AudioStream *a, const void *b, int c),(a,b,c),return)
     6.9 +SDL_DYNAPI_PROC(int,SDL_AudioStreamGet,(SDL_AudioStream *a, void *b, int c),(a,b,c),return)
    6.10 +SDL_DYNAPI_PROC(void,SDL_AudioStreamClear,(SDL_AudioStream *a),(a),)
    6.11 +SDL_DYNAPI_PROC(int,SDL_AudioStreamAvailable,(SDL_AudioStream *a),(a),return)
    6.12 +SDL_DYNAPI_PROC(void,SDL_FreeAudioStream,(SDL_AudioStream *a),(a),)