2008-01-31 07:04:26 +01:00
|
|
|
/*
|
|
|
|
* util.h - utility functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "streamtypes.h"
|
|
|
|
|
|
|
|
#ifndef _UTIL_H
|
|
|
|
#define _UTIL_H
|
|
|
|
|
|
|
|
/* host endian independent multi-byte integer reading */
|
|
|
|
|
|
|
|
static inline int16_t get_16bitBE(uint8_t * p) {
|
|
|
|
return (p[0]<<8) | (p[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int16_t get_16bitLE(uint8_t * p) {
|
|
|
|
return (p[0]) | (p[1]<<8);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int32_t get_32bitBE(uint8_t * p) {
|
|
|
|
return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | (p[3]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int32_t get_32bitLE(uint8_t * p) {
|
|
|
|
return (p[0]) | (p[1]<<8) | (p[2]<<16) | (p[3]<<24);
|
|
|
|
}
|
|
|
|
|
2016-12-18 12:46:11 +01:00
|
|
|
void put_8bit(uint8_t * buf, int8_t i);
|
|
|
|
|
2008-02-04 11:34:18 +01:00
|
|
|
void put_16bitLE(uint8_t * buf, int16_t i);
|
|
|
|
|
|
|
|
void put_32bitLE(uint8_t * buf, int32_t i);
|
|
|
|
|
2009-03-16 16:45:02 +01:00
|
|
|
void put_16bitBE(uint8_t * buf, int16_t i);
|
|
|
|
|
|
|
|
void put_32bitBE(uint8_t * buf, int32_t i);
|
|
|
|
|
2008-01-31 07:04:26 +01:00
|
|
|
/* signed nibbles come up a lot */
|
2008-01-31 23:26:11 +01:00
|
|
|
static int nibble_to_int[16] = {0,1,2,3,4,5,6,7,-8,-7,-6,-5,-4,-3,-2,-1};
|
2008-01-31 07:04:26 +01:00
|
|
|
|
2008-02-04 09:57:44 +01:00
|
|
|
static inline int get_high_nibble_signed(uint8_t n) {
|
2008-01-31 23:26:11 +01:00
|
|
|
/*return ((n&0x70)-(n&0x80))>>4;*/
|
|
|
|
return nibble_to_int[n>>4];
|
2008-01-31 07:04:26 +01:00
|
|
|
}
|
|
|
|
|
2008-02-04 09:57:44 +01:00
|
|
|
static inline int get_low_nibble_signed(uint8_t n) {
|
2008-01-31 23:26:11 +01:00
|
|
|
/*return (n&7)-(n&8);*/
|
|
|
|
return nibble_to_int[n&0xf];
|
2008-01-31 07:04:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* return true for a good sample rate */
|
|
|
|
int check_sample_rate(int32_t sr);
|
|
|
|
|
|
|
|
/* return a file's extension (a pointer to the first character of the
|
|
|
|
* extension in the original filename or the ending null byte if no extension
|
|
|
|
*/
|
|
|
|
const char * filename_extension(const char * filename);
|
|
|
|
|
2008-01-31 23:26:11 +01:00
|
|
|
static inline int clamp16(int32_t val) {
|
2008-01-31 07:04:26 +01:00
|
|
|
if (val>32767) return 32767;
|
|
|
|
if (val<-32768) return -32768;
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2010-08-31 06:44:04 +02:00
|
|
|
void swap_samples_le(sample *buf, int count);
|
2008-02-04 11:34:18 +01:00
|
|
|
|
2008-03-11 02:27:59 +01:00
|
|
|
void concatn(int length, char * dst, const char * src);
|
|
|
|
|
2016-12-27 14:28:12 +01:00
|
|
|
|
|
|
|
/* Simple stdout logging for debugging and regression testing purposes.
|
|
|
|
* Needs C99 variadic macros. */
|
|
|
|
#ifdef VGM_DEBUG_OUTPUT
|
|
|
|
|
|
|
|
#define VGM_ASSERT(condition, ...) \
|
|
|
|
do { if (condition) printf(__VA_ARGS__); } while (0)
|
|
|
|
#define VGM_LOG(...) \
|
|
|
|
do { printf(__VA_ARGS__); } while (0)
|
2016-12-29 13:27:10 +01:00
|
|
|
#define VGM_LOGF() \
|
|
|
|
do { printf("%s:%i '%s'\n", __FILE__, __LINE__, __func__); } while (0)
|
2016-12-27 14:28:12 +01:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#define VGM_ASSERT(condition,fmt, ...) /* nothing */
|
|
|
|
#define VGM_LOG(...) /* nothing */
|
2016-12-29 13:27:10 +01:00
|
|
|
#define VGM_LOGF(...) /* nothing */
|
2016-12-27 14:28:12 +01:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2008-01-31 07:04:26 +01:00
|
|
|
#endif
|