Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * \file kit_acodec.h
- * \brief Header file for KIT SDL2's Audio Codec module
- */
- #ifndef _KIT_ACODEC_H
- #define _KIT_ACODEC_H
- #ifndef _KIT_SDL2_ACODEC_H
- #define _KIT_SDL2_ACODEC_H
- //todo: add acodecQOARead at some point
- #include "./kit_core.h" //includes SDL2/SDL.h
- #ifdef __cplusplus
- extern "C" {
- #endif
- /* ++++++++++++++++++++ */
- /* +kit_sdl2_acodecPCM+ */
- /* ++++++++++++++++++++ */
- //using the extension ".kpm" is preferred
- //when saving kit_acodecPCM to a file
- #define KPCM_MAGIC (0x4D43506B) //= "kPCM"
- /**
- * \brief Stereo Uint8 samples
- */
- typedef struct kit_acodecPCM_U_8S {
- Uint8 l; ///< \brief Left audio channel
- Uint8 r; ///< \brief Right audio channel
- } kit_acodecPCM_U_8S;
- /**
- * \brief Stereo Sint16 samples
- */
- typedef struct kit_acodecPCM_I16S {
- Sint16 l; ///< \brief Left audio channel
- Sint16 r; ///< \brief Right audio channel
- } kit_acodecPCM_I16S;
- /**
- * \brief Stereo Sint32 samples
- */
- typedef struct kit_acodecPCM_I32S {
- Sint32 l; ///< \brief Left audio channel
- Sint32 r; ///< \brief Right audio channel
- } kit_acodecPCM_I32S;
- /**
- * \brief Stereo float samples
- */
- typedef struct kit_acodecPCM_F32S {
- float l; ///< \brief Left audio channel
- float r; ///< \brief Right audio channel
- } kit_acodecPCM_F32S;
- /**
- * \brief A union of supported sample formats
- */
- typedef union kit_acodecPCMSamples {
- void* data; ///< \brief Generic pointer
- Uint8* u_8; ///< \brief Mono Uint8 Samples
- Sint16* i16; ///< \brief Mono Sint16 Samples
- Sint32* i32; ///< \brief Mono Sint32 Samples
- float* f32; ///< \brief Mono float Samples
- kit_acodecPCM_U_8S* u_8s; ///< \brief Stereo Uint8 samples
- kit_acodecPCM_I16S* i16s; ///< \brief Stereo Sint16 samples
- kit_acodecPCM_I32S* i32s; ///< \brief Stereo Sint32 samples
- kit_acodecPCM_F32S* f32s; ///< \brief Stereo float samples
- } kit_acodecPCMSamples;
- /**
- * \brief The struct that contains info about a PCM audio stream. \n
- * When saved as a file (usually as ".kpm"), the header's size will be 72 (0x48).
- */
- typedef struct kit_acodecPCM {
- Uint32 magic; ///< \brief (0x00) = 0x4D43506B = "kPCM" (no null terminator)
- SDL_AudioFormat format; ///< \brief (0x04) The data format of the stream
- Uint16 headerSize; ///< \brief (0x06) = sizeof(kit_acodecPCM)
- Uint64 dataSize; ///< \brief (0x08) The size of the PCM buffer, in bytes
- Uint64 loopStart; ///< \brief (0x10) Which sample to loop back to
- Uint64 loopEnd; ///< \brief (0x18) Which sample to restart the loop on
- Uint64 numSamples; ///< \brief (0x20) The number of sample frames in the stream
- Uint32 sampleRate; ///< \brief (0x28) The stream's sample rate, in Hz
- Uint32 bitRate; ///< \brief (0x2C) The audio's bit rate per second
- Uint16 loopCount; ///< \brief (0x30) # of times to loop audio (65535 for infinite loop)
- Uint16 channels; ///< \brief (0x32) # of interlaced channels in the stream (L&R for stereo)
- Uint8 bitRemainder; ///< \brief (0x34) = bitsPerSample%8
- Uint8 userflags; ///< \brief (0x35) User-defined (is just padding otherwise)
- Uint16 uservalue; ///< \brief (0x36) User-defined (is just padding otherwise)
- //while userdata and data are technically included in a .kpm file,
- //they should appear as 0 within that file
- void* userdata; ///< \brief (0x38) User-defined pointer
- union {
- void* data; ///< \brief = (void*)pcm_struct_pointer + pcm_struct_pointer->headerSize
- Uint8* u_8; ///< \brief Mono Uint8 Samples
- Sint16* i16; ///< \brief Mono Sint16 Samples
- Sint32* i32; ///< \brief Mono Sint32 Samples
- float* f32; ///< \brief Mono float Samples
- kit_acodecPCM_U_8S* u_8s; ///< \brief Stereo Uint8 samples
- kit_acodecPCM_I16S* i16s; ///< \brief Stereo Sint16 samples
- kit_acodecPCM_I32S* i32s; ///< \brief Stereo Sint32 samples
- kit_acodecPCM_F32S* f32s; ///< \brief Stereo float samples
- }; ///< \brief (0x40) Sample data (PCM data should be contiguous with the struct itself)
- } kit_acodecPCM;
- /**
- * Destroy a kit_acodecPCM stream
- * \param[in,out] pcm_p A pointer to the kit_acodecPCM* to be destroyed (before being set to NULL)
- * \return 0 on success, or -1 on error (call SDL_GetError() for more info)
- *
- * \sa kit_acodecPCMCreate
- * \sa kit_acodecPCMCopy
- */
- extern int kit_acodecPCMDestroy(kit_acodecPCM** pcm_p);
- /**
- * Destroy a kit_acodecPCM stream
- * \param[in] format The data type of the stream
- * \param[in] channels The number of interlaced channels in the stream
- * \param[in] sampleRate The desired sample rate, in hertz
- * \param[in] numSamples The number of sample frames in the stream
- * \return A pointer to the newly-created pcm stream, or NULL on error (call SDL_GetError() for more info)
- *
- * \sa kit_acodecPCMDestroy
- * \sa kit_acodecPCMCopy
- */
- extern kit_acodecPCM* kit_acodecPCMCreate(SDL_AudioFormat format, Uint16 channels,
- Uint32 sampleRate, Uint64 numSamples);
- /**
- * Create a duplicate of a kit_acodecPCM stream
- * \param[in] pcm A pointer to the kit_acodecPCM stream to be copied
- * \return A pointer to a newly-copied kit_acodecPCM stream,
- * or NULL on error (call SDL_GetError() for more info)
- *
- * \sa kit_acodecPCMDestroy
- * \sa kit_acodecPCMCreate
- */
- extern kit_acodecPCM* kit_acodecPCMCopy(kit_acodecPCM* pcm);
- /**
- * Read kit_acodecPCM data from a pcm file (usually with a ".kpm" file extension)
- * \param[in] filePath The name of the file to read from
- * \return A pointer to a kit_acodecPCM struct that contains the audio data,
- * or NULL on error (call SDL_GetError() for more info)
- *
- * \sa kit_acodecPCMWrite
- */
- extern kit_acodecPCM* kit_acodecPCMRead(const char* filePath);
- /**
- * Save kit_acodecPCM data to a pcm file (usually with a ".kpm" file extension)
- * \param[in] pcm A pointer to a kit_acodecPCM struct that contains the audio data
- * \param[in] filePath The name of the file to write to
- * \return 0 on success, or -1 on error (call SDL_GetError() for more info)
- *
- * \sa kit_acodecPCMRead
- */
- extern int kit_acodecPCMWrite(kit_acodecPCM* pcm, const char* filePath);
- /**
- * Change the number of sample frames in a kit_acodecPCM stream
- * \param[in,out] pcm_p A pointer to the kit_acodecPCM* to be resized
- * \param[in] numSamples The amount of sample frames to resize the stream to
- * \return 0 on success, or -1 on error (call SDL_GetError() for more info)
- */
- extern int kit_acodecPCMSetNumSamples(kit_acodecPCM** pcm_p, Uint64 numSamples);
- /**
- * Create a copy of a kit_acodecPCM stream, converted to either mono or stereo
- * \param[in] pcm A pointer to the input kit_acodecPCM stream
- * \param[in] toStereo Whether to convert to stereo, or convert to mono
- * \return A pointer to a kit_acodecPCM struct that contains the converted stream,
- * or NULL on error (call SDL_GetError() for more info)
- *
- * \remark Accepted formats are AUDIO_U8, AUDIO_S16, AUDIO_S32, and AUDIO_F32. \n
- * \remark (Trying to use a stream with > 2 channels is not allowed!)
- */
- extern kit_acodecPCM* kit_acodecPCMConvertStereo(kit_acodecPCM* pcm, SDL_bool toStereo);
- /**
- * Create a copy of a kit_acodecPCM stream, under a new sample format
- * \param[in] pcm A pointer to the input kit_acodecPCM stream
- * \param[in] format The sample format to convert to
- * \return A pointer to a kit_acodecPCM struct that contains the converted stream,
- * or NULL on error (call SDL_GetError() for more info)
- *
- * \remark Accepted formats are AUDIO_U8, AUDIO_S16, AUDIO_S32, and AUDIO_F32.
- * \remark (Also, conversions from AUDIO_S32 will have a quantization error of up to 64!)
- */
- extern kit_acodecPCM* kit_acodecPCMConvertFormat(kit_acodecPCM* pcm, SDL_AudioFormat format);
- /**
- * Create a copy of a kit_acodecPCM stream, under a new sample rate
- * \param[in] pcm A pointer to the input kit_acodecPCM stream
- * \param[in] sampleRate The sample rate to convert to
- * \param[in] linearInterpolation A boolean of whether to use linear interpolation
- * during sample rate conversion (nearest-neighbor is used otherwise)
- * \return A pointer to a kit_acodecPCM struct that contains the converted stream,
- * or NULL on error (call SDL_GetError() for more info)
- *
- * \remark Accepted formats are AUDIO_U8, AUDIO_S16, AUDIO_S32, and AUDIO_F32.
- */
- extern kit_acodecPCM* kit_acodecPCMResample(kit_acodecPCM* pcm, Uint32 sampleRate,
- SDL_bool linearInterpolation);
- /* -------------------- */
- /* -kit_sdl2_acodecPCM- */
- /* -------------------- */
- /* ++++++++++++++++++++ */
- /* +kit_sdl2_acodecWAV+ */
- /* ++++++++++++++++++++ */
- /**
- * Load kit_acodecPCM data from a ".wav" file
- * \param[in] filePath The name of the wav file to load
- * \return A pointer to a kit_acodecPCM struct that contains the audio data,
- * or NULL on error (call SDL_GetError() for more info)
- *
- * \remark Supported sample formats are: Uint8, Sint16, Sint32, and float
- * \sa kit_acodecPCM
- * \sa kit_acodecWAVWrite
- */
- extern kit_acodecPCM* kit_acodecWAVRead(const char* filePath);
- /**
- * Save kit_acodecPCM data to a ".wav" file
- * \param[in] pcm A pointer to a kit_acodecPCM struct that contains the audio data
- * \param[in] filePath The name of the wav file to write to
- * \return 0 on success, or -1 on failure; call SDL_GetError() for details
- *
- * \sa kit_acodecPCM
- * \sa kit_acodecWAVRead
- */
- extern int kit_acodecWAVWrite(kit_acodecPCM* pcm, const char* filePath);
- /* -------------------- */
- /* -kit_sdl2_acodecWAV- */
- /* -------------------- */
- /* ++++++++++++++++++++ */
- /* +kit_sdl2_acodecFDM+ */
- /* ++++++++++++++++++++ */
- #define FDM_MAGIC_NUMBER 0x4D44466B
- typedef struct kit_acodecFDMHeader { //(".kfd")
- union {
- char s[4]; //="kFDM"
- Uint32 n; //=0x4D44466B
- } /*-----*/ magic;
- Uint16 headerSize; //headerSize includes headerSize itself, and magic
- Uint16 type; //type 0 = static delta, static history
- Uint64 dataSize;
- Uint64 numSamples;
- Uint64 loopStart;
- Uint64 loopEnd;
- float delta; //% of change, relative to HALF range; 0.0 -> 2.0
- Uint8 loopCount; //255 (-1) for inf. loop
- Uint8 remainder; //=numSamples%8
- Uint8 historyLen; //number of samples in filter; 0 -> 63
- Uint8 channels;
- } kit_acodecFDMHeader;
- extern kit_acodecPCM* kit_acodecFDMRead(const char* filePath);
- extern int kit_acodecFDMWrite(const char* fileName, kit_acodecPCM* pcm,
- Uint8 historyLen, Uint8 iterations);
- /* -------------------- */
- /* -kit_sdl2_acodecFDM- */
- /* -------------------- */
- /* +++++++++++++++++ */
- /* +kit_sdl2_acodec+ */
- /* +++++++++++++++++ */
- extern const SDL_bool kit_acodecIsDebug;
- /**
- * Load kit_acodecPCM data from a valid audio file
- * \param[in] filePath The name of the file to load from
- * \param[in] format The target data type to convert to (0 to keep original data type)
- * \param[in] sampleRate The target sample rate to resample to (0 to keep original sample rate)
- * \param[in] linearInterpolation 'Use linear interpolation if resampling?' (nearest-neighbor otherwise)
- * \return A pointer to a kit_acodecPCM struct that contains the audio data,
- * or NULL on error (call SDL_GetError() for more info)
- *
- * \remark Supported file formats are: "kPCM" (.kpm), "kFDM" (.kfd), and "WAVE" (.wav)
- */
- extern kit_acodecPCM* kit_acodecLoadAudio(const char* filePath, SDL_AudioFormat format,
- Uint32 sampleRate, SDL_bool linearInterpolation);
- /* ----------------- */
- /* -kit_sdl2_acodec- */
- /* ----------------- */
- #ifdef __cplusplus
- }
- #endif
- #endif /* _KIT_SDL2_ACODEC_H */
- #endif /* _KIT_ACODEC_H */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement