mpg123.h 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. /*
  2. libmpg123: MPEG Audio Decoder library (version @PACKAGE_VERSION@)
  3. copyright 1995-2007 by the mpg123 project - free software under the terms of the LGPL 2.1
  4. see COPYING and AUTHORS files in distribution or http://mpg123.org
  5. */
  6. #ifndef MPG123_LIB_H
  7. #define MPG123_LIB_H
  8. /** \file mpg123.h The header file for the libmpg123 MPEG Audio decoder */
  9. /* These aren't actually in use... seems to work without using libtool. */
  10. #ifdef BUILD_MPG123_DLL
  11. /* The dll exports. */
  12. #define EXPORT __declspec(dllexport)
  13. #else
  14. #ifdef LINK_MPG123_DLL
  15. /* The exe imports. */
  16. #define EXPORT __declspec(dllimport)
  17. #else
  18. /* Nothing on normal/UNIX builds */
  19. #define EXPORT
  20. #endif
  21. #endif
  22. #include <stdlib.h>
  23. #include <sys/types.h>
  24. typedef int ssize_t;
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. /** \defgroup mpg123_init mpg123 library and handle setup
  29. *
  30. * Functions to initialise and shutdown the mpg123 library and handles.
  31. * The parameters of handles have workable defaults, you only have to tune them when you want to tune something;-)
  32. * Tip: Use a RVA setting...
  33. *
  34. * @{
  35. */
  36. /** Opaque structure for the libmpg123 decoder handle. */
  37. struct mpg123_handle_struct;
  38. /** Opaque structure for the libmpg123 decoder handle.
  39. * Most functions take a pointer to a mpg123_handle as first argument and operate on its data in an object-oriented manner.
  40. */
  41. typedef struct mpg123_handle_struct mpg123_handle;
  42. /** Function to initialise the mpg123 library.
  43. * This function is not thread-safe. Call it exactly once per process, before any other (possibly threaded) work with the library.
  44. *
  45. * \return MPG123_OK if successful, otherwise an error number.
  46. */
  47. EXPORT int mpg123_init(void);
  48. /** Function to close down the mpg123 library.
  49. * This function is not thread-safe. Call it exactly once per process, before any other (possibly threaded) work with the library. */
  50. EXPORT void mpg123_exit(void);
  51. /** Create a handle with optional choice of decoder (named by a string, see mpg123_decoders() or mpg123_supported_decoders()).
  52. * and optional retrieval of an error code to feed to mpg123_plain_strerror().
  53. * Optional means: Any of or both the parameters may be NULL.
  54. *
  55. * \return Non-NULL pointer when successful.
  56. */
  57. EXPORT mpg123_handle *mpg123_new(const char* decoder, int *error);
  58. /** Delete handle, mh is either a valid mpg123 handle or NULL. */
  59. EXPORT void mpg123_delete(mpg123_handle *mh);
  60. /** Enumeration of the parameters types that it is possible to set/get. */
  61. enum mpg123_parms
  62. {
  63. MPG123_VERBOSE, /**< set verbosity value for enabling messages to stderr, >= 0 makes sense */
  64. MPG123_FLAGS, /**< set all flags, p.ex val = MPG123_GAPLESS|MPG123_MONO_MIX */
  65. MPG123_ADD_FLAGS, /**< add some flags */
  66. MPG123_FORCE_RATE, /**< when value > 0, force output rate to that value */
  67. MPG123_DOWN_SAMPLE, /**< 0=native rate, 1=half rate, 2=quarter rate */
  68. MPG123_RVA, /**< one of the RVA choices above */
  69. MPG123_DOWNSPEED, /**< play a frame N times */
  70. MPG123_UPSPEED, /**< play every Nth frame */
  71. MPG123_START_FRAME, /**< start with this frame (skip frames before that) */
  72. MPG123_DECODE_FRAMES, /**< decode only this number of frames */
  73. MPG123_ICY_INTERVAL, /**< stream contains ICY metadata with this interval */
  74. MPG123_OUTSCALE, /**< the scale for output samples (amplitude) */
  75. MPG123_TIMEOUT, /**< timeout for reading from a stream (not supported on win32) */
  76. MPG123_REMOVE_FLAGS, /**< remove some flags (inverse of MPG123_ADD_FLAGS) */
  77. MPG123_RESYNC_LIMIT /**< Try resync on frame parsing for that many bytes or until end of stream (<0). */
  78. };
  79. /** Flag bits for MPG123_FLAGS, use the usual binary or to combine. */
  80. enum mpg123_param_flags
  81. {
  82. MPG123_FORCE_MONO = 0x7 /**< 0111 Force some mono mode: This is a test bitmask for seeing if any mono forcing is active. */
  83. ,MPG123_MONO_LEFT = 0x1 /**< 0001 Force playback of left channel only. */
  84. ,MPG123_MONO_RIGHT = 0x2 /**< 0010 Force playback of right channel only. */
  85. ,MPG123_MONO_MIX = 0x4 /**< 0100 Force playback of mixed mono. */
  86. ,MPG123_FORCE_STEREO = 0x8 /**< 1000 Force stereo output. */
  87. ,MPG123_FORCE_8BIT = 0x10 /**< 00010000 Force 8bit formats. */
  88. ,MPG123_QUIET = 0x20 /**< 00100000 Suppress any printouts (overrules verbose). */
  89. ,MPG123_GAPLESS = 0x40 /**< 01000000 Enable gapless decoding (default on if libmpg123 has support). */
  90. ,MPG123_NO_RESYNC = 0x80 /**< 10000000 Disable resync stream after error. */
  91. ,MPG123_SEEKBUFFER = 0x100 /**< 000100000000 Enable small buffer on non-seekable streams to allow some peek-ahead (for better MPEG sync). */
  92. };
  93. /** choices for MPG123_RVA */
  94. enum mpg123_param_rva
  95. {
  96. MPG123_RVA_OFF = 0 /**< RVA disabled (default). */
  97. ,MPG123_RVA_MIX = 1 /**< Use mix/track/radio gain. */
  98. ,MPG123_RVA_ALBUM = 2 /**< Use album/audiophile gain */
  99. ,MPG123_RVA_MAX = MPG123_RVA_ALBUM /**< The maximum RVA code, may increase in future. */
  100. };
  101. /* TODO: Assess the possibilities and troubles of changing parameters during playback. */
  102. /** Set a specific parameter, for a specific mpg123_handle, using a parameter
  103. * type key chosen from the mpg123_parms enumeration, to the specified value. */
  104. EXPORT int mpg123_param(mpg123_handle *mh, enum mpg123_parms type, long value, double fvalue);
  105. /** Get a specific parameter, for a specific mpg123_handle.
  106. * See the mpg123_parms enumeration for a list of available parameters. */
  107. EXPORT int mpg123_getparam(mpg123_handle *mh, enum mpg123_parms type, long *val, double *fval);
  108. /* @} */
  109. /** \defgroup mpg123_error mpg123 error handling
  110. *
  111. * Functions to get text version of the error numbers and an enumeration
  112. * of the error codes returned by libmpg123.
  113. *
  114. * Most functions operating on a mpg123_handle simply return MPG123_OK on success and MPG123_ERR on failure (setting the internal error variable of the handle to the specific error code).
  115. * Decoding/seek functions may also return message codes MPG123_DONE, MPG123_NEW_FORMAT and MPG123_NEED_MORE.
  116. * The positive range of return values is used for "useful" values when appropriate.
  117. *
  118. * @{
  119. */
  120. /** Enumeration of the message and error codes and returned by libmpg123 functions. */
  121. enum mpg123_errors
  122. {
  123. MPG123_DONE=-12, /**< Message: Track ended. */
  124. MPG123_NEW_FORMAT=-11, /**< Message: Output format will be different on next call. */
  125. MPG123_NEED_MORE=-10, /**< Message: For feed reader: "Feed me more!" */
  126. MPG123_ERR=-1, /**< Generic Error */
  127. MPG123_OK=0, /**< Success */
  128. MPG123_BAD_OUTFORMAT, /**< Unable to set up output format! */
  129. MPG123_BAD_CHANNEL, /**< Invalid channel number specified. */
  130. MPG123_BAD_RATE, /**< Invalid sample rate specified. */
  131. MPG123_ERR_16TO8TABLE, /**< Unable to allocate memory for 16 to 8 converter table! */
  132. MPG123_BAD_PARAM, /**< Bad parameter id! */
  133. MPG123_BAD_BUFFER, /**< Bad buffer given -- invalid pointer or too small size. */
  134. MPG123_OUT_OF_MEM, /**< Out of memory -- some malloc() failed. */
  135. MPG123_NOT_INITIALIZED, /**< You didn't initialize the library! */
  136. MPG123_BAD_DECODER, /**< Invalid decoder choice. */
  137. MPG123_BAD_HANDLE, /**< Invalid mpg123 handle. */
  138. MPG123_NO_BUFFERS, /**< Unable to initialize frame buffers (out of memory?). */
  139. MPG123_BAD_RVA, /**< Invalid RVA mode. */
  140. MPG123_NO_GAPLESS, /**< This build doesn't support gapless decoding. */
  141. MPG123_NO_SPACE, /**< Not enough buffer space. */
  142. MPG123_BAD_TYPES, /**< Incompatible numeric data types. */
  143. MPG123_BAD_BAND, /**< Bad equalizer band. */
  144. MPG123_ERR_NULL, /**< Null pointer given where valid storage address needed. */
  145. MPG123_ERR_READER, /**< Error reading the stream. */
  146. MPG123_NO_SEEK_FROM_END,/**< Cannot seek from end (end is not known). */
  147. MPG123_BAD_WHENCE, /**< Invalid 'whence' for seek function.*/
  148. MPG123_NO_TIMEOUT, /**< Build does not support stream timeouts. */
  149. MPG123_BAD_FILE, /**< File access error. */
  150. MPG123_NO_SEEK, /**< Seek not supported by stream. */
  151. MPG123_NO_READER, /**< No stream opened. */
  152. MPG123_BAD_PARS, /**< Bad parameter handle. */
  153. MPG123_BAD_INDEX_PAR, /**< Bad parameters to mpg123_index() */
  154. MPG123_OUT_OF_SYNC, /**< Lost track in bytestream and did not try to resync. */
  155. MPG123_RESYNC_FAIL, /**< Resync failed to find valid MPEG data. */
  156. MPG123_NO_8BIT, /**< No 8bit encoding possible. */
  157. MPG123_BAD_ALIGN, /**< Stack aligmnent error */
  158. MPG123_NULL_BUFFER /**< NULL input buffer with non-zero size... */
  159. };
  160. /** Return a string describing that error errcode means. */
  161. EXPORT const char* mpg123_plain_strerror(int errcode);
  162. /** Give string describing what error has occured in the context of handle mh.
  163. * When a function operating on an mpg123 handle returns MPG123_ERR, you should check for the actual reason via
  164. * char *errmsg = mpg123_strerror(mh)
  165. * This function will catch mh == NULL and return the message for MPG123_BAD_HANDLE. */
  166. EXPORT const char* mpg123_strerror(mpg123_handle *mh);
  167. /** Return the plain errcode intead of a string. */
  168. EXPORT int mpg123_errcode(mpg123_handle *mh);
  169. /*@}*/
  170. /** \defgroup mpg123_decoder mpg123 decoder selection
  171. *
  172. * Functions to list and select the available decoders.
  173. * Perhaps the most prominent feature of mpg123: You have several (optimized) decoders to choose from (on x86 and PPC (MacOS) systems, that is).
  174. *
  175. * @{
  176. */
  177. /** Return a NULL-terminated array of generally available decoder names (plain 8bit ASCII). */
  178. EXPORT char **mpg123_decoders();
  179. /** Return a NULL-terminated array of the decoders supported by the CPU (plain 8bit ASCII). */
  180. EXPORT char **mpg123_supported_decoders();
  181. /** Set the chosen decoder to 'decoder_name' */
  182. EXPORT int mpg123_decoder(mpg123_handle *mh, const char* decoder_name);
  183. /*@}*/
  184. /** \defgroup mpg123_output mpg123 output audio format
  185. *
  186. * Functions to get and select the format of the decoded audio.
  187. *
  188. * @{
  189. */
  190. /** 16 or 8 bits, signed or unsigned... all flags fit into 15 bits and are designed to have meaningful binary AND/OR.
  191. * Adding float and 32bit int definitions for experimental fun. Only 32bit (and possibly 64bit) float is
  192. * somewhat there with a dedicated library build. */
  193. enum mpg123_enc_enum
  194. {
  195. MPG123_ENC_8 = 0x00f /**< 0000 0000 1111 Some 8 bit integer encoding. */
  196. ,MPG123_ENC_16 = 0x040 /**< 0000 0100 0000 Some 16 bit integer encoding. */
  197. ,MPG123_ENC_32 = 0x100 /**< 0001 0000 0000 Some 32 bit integer encoding. */
  198. ,MPG123_ENC_SIGNED = 0x080 /**< 0000 1000 0000 Some signed integer encoding. */
  199. ,MPG123_ENC_FLOAT = 0x800 /**< 1110 0000 0000 Some float encoding. */
  200. ,MPG123_ENC_SIGNED_16 = (MPG123_ENC_16|MPG123_ENC_SIGNED|0x10) /**< 0000 1101 0000 signed 16 bit */
  201. ,MPG123_ENC_UNSIGNED_16 = (MPG123_ENC_16|0x20) /**< 0000 0110 0000 unsigned 16 bit*/
  202. ,MPG123_ENC_UNSIGNED_8 = 0x01 /**< 0000 0000 0001 unsigned 8 bit*/
  203. ,MPG123_ENC_SIGNED_8 = (MPG123_ENC_SIGNED|0x02) /**< 0000 1000 0010 signed 8 bit*/
  204. ,MPG123_ENC_ULAW_8 = 0x04 /**< 0000 0000 0100 ulaw 8 bit*/
  205. ,MPG123_ENC_ALAW_8 = 0x08 /**< 0000 0000 1000 alaw 8 bit */
  206. ,MPG123_ENC_SIGNED_32 = MPG123_ENC_32|MPG123_ENC_SIGNED|0x10 /**< 0001 1001 0000 signed 32 bit */
  207. ,MPG123_ENC_UNSIGNED_32 = MPG123_ENC_32|0x20 /**< 0001 0010 0000 unsigned 32 bit */
  208. ,MPG123_ENC_FLOAT_32 = 0x200 /**< 0010 0000 0000 32bit float */
  209. ,MPG123_ENC_FLOAT_64 = 0x400 /**< 0100 0000 0000 64bit float */
  210. ,MPG123_ENC_ANY = ( MPG123_ENC_SIGNED_16 | MPG123_ENC_UNSIGNED_16 | MPG123_ENC_UNSIGNED_8
  211. | MPG123_ENC_SIGNED_8 | MPG123_ENC_ULAW_8 | MPG123_ENC_ALAW_8
  212. | MPG123_ENC_FLOAT_32 | MPG123_ENC_FLOAT_64 ) /**< any encoding */
  213. };
  214. /** They can be combined into one number (3) to indicate mono and stereo... */
  215. enum mpg123_channelcount
  216. {
  217. MPG123_MONO = 1
  218. ,MPG123_STEREO = 2
  219. };
  220. /** An array of supported standard sample rates
  221. * These are possible native sample rates of MPEG audio files.
  222. * You can still force mpg123 to resample to a different one, but by default you will only get audio in one of these samplings.
  223. * \param list Store a pointer to the sample rates array there.
  224. * \param number Store the number of sample rates there. */
  225. EXPORT void mpg123_rates(const long **list, size_t *number);
  226. /** An array of supported audio encodings.
  227. * An audio encoding is one of the fully qualified members of mpg123_enc_enum (MPG123_ENC_SIGNED_16, not MPG123_SIGNED).
  228. * \param list Store a pointer to the encodings array there.
  229. * \param number Store the number of encodings there. */
  230. EXPORT void mpg123_encodings(const int **list, size_t *number);
  231. /** Configure a mpg123 handle to accept no output format at all,
  232. * use before specifying supported formats with mpg123_format */
  233. EXPORT int mpg123_format_none(mpg123_handle *mh);
  234. /** Configure mpg123 handle to accept all formats
  235. * (also any custom rate you may set) -- this is default. */
  236. EXPORT int mpg123_format_all(mpg123_handle *mh);
  237. /** Set the audio format support of a mpg123_handle in detail:
  238. * \param mh audio decoder handle
  239. * \param rate The sample rate value (in Hertz).
  240. * \param channels A combination of MPG123_STEREO and MPG123_MONO.
  241. * \param encodings A combination of accepted encodings for rate and channels, p.ex MPG123_ENC_SIGNED16 | MPG123_ENC_ULAW_8 (or 0 for no support). Please note that some encodings may not be supported in the library build and thus will be ignored here.
  242. * \return MPG123_OK on success, MPG123_ERR if there was an error. */
  243. EXPORT int mpg123_format(mpg123_handle *mh, long rate, int channels, int encodings);
  244. /** Check to see if a specific format at a specific rate is supported
  245. * by mpg123_handle.
  246. * \return 0 for no support (that includes invalid parameters), MPG123_STEREO,
  247. * MPG123_MONO or MPG123_STEREO|MPG123_MONO. */
  248. EXPORT int mpg123_format_support(mpg123_handle *mh, long rate, int encoding);
  249. /** Get the current output format written to the addresses givenr. */
  250. EXPORT int mpg123_getformat(mpg123_handle *mh, long *rate, int *channels, int *encoding);
  251. /*@}*/
  252. /** \defgroup mpg123_input mpg123 file input and decoding
  253. *
  254. * Functions for input bitstream and decoding operations.
  255. *
  256. * @{
  257. */
  258. /* reading samples / triggering decoding, possible return values: */
  259. /** Enumeration of the error codes returned by libmpg123 functions. */
  260. /** Open and prepare to decode the specified file by filesystem path.
  261. * This does not open HTTP urls; libmpg123 contains no networking code.
  262. * If you want to decode internet streams, use mpg123_open_fd() or mpg123_open_feed().
  263. */
  264. EXPORT int mpg123_open(mpg123_handle *mh, const char *path);
  265. /** Use an already opened file descriptor as the bitstream input
  266. * mpg123_close() will _not_ close the file descriptor.
  267. */
  268. EXPORT int mpg123_open_fd(mpg123_handle *mh, int fd);
  269. /** Open a new bitstream and prepare for direct feeding
  270. * This works together with mpg123_decode(); you are responsible for reading and feeding the input bitstream.
  271. */
  272. EXPORT int mpg123_open_feed(mpg123_handle *mh);
  273. /** Closes the source, if libmpg123 opened it. */
  274. EXPORT int mpg123_close(mpg123_handle *mh);
  275. /** Read from stream and decode up to outmemsize bytes.
  276. * \param outmemory address of output buffer to write to
  277. * \param outmemsize maximum number of bytes to write
  278. * \param done address to store the number of actually decoded bytes to
  279. * \return error/message code (watch out for MPG123_DONE and friends!) */
  280. EXPORT int mpg123_read(mpg123_handle *mh, unsigned char *outmemory, size_t outmemsize, size_t *done);
  281. /** Feed data for a stream that has been opened with mpg123_open_feed().
  282. * It's give and take: You provide the bytestream, mpg123 gives you the decoded samples.
  283. * \param in input buffer
  284. * \param size number of input bytes
  285. * \return error/message code. */
  286. EXPORT int mpg123_feed(mpg123_handle *mh, const unsigned char *in, size_t size);
  287. /** Decode MPEG Audio from inmemory to outmemory.
  288. * This is very close to a drop-in replacement for old mpglib.
  289. * When you give zero-sized output buffer the input will be parsed until
  290. * decoded data is available. This enables you to get MPG123_NEW_FORMAT (and query it)
  291. * without taking decoded data.
  292. * Think of this function being the union of mpg123_read() and mpg123_feed() (which it actually is, sort of;-).
  293. * You can actually always decide if you want those specialized functions in separate steps or one call this one here.
  294. * \param inmemory input buffer
  295. * \param inmemsize number of input bytes
  296. * \param outmemory output buffer
  297. * \param outmemsize maximum number of output bytes
  298. * \param done address to store the number of actually decoded bytes to
  299. * \return error/message code (watch out especially for MPG123_NEED_MORE)
  300. */
  301. EXPORT int mpg123_decode(mpg123_handle *mh, const unsigned char *inmemory, size_t inmemsize,
  302. unsigned char *outmemory, size_t outmemsize, size_t *done);
  303. /** Decode next MPEG frame to internal buffer
  304. * or read a frame and return after setting a new format.
  305. * \param num current frame offset gets stored there
  306. * \param audio This pointer is set to the internal buffer to read the decoded audio from.
  307. * \param bytes number of output bytes ready in the buffer
  308. */
  309. EXPORT int mpg123_decode_frame(mpg123_handle *mh, off_t *num, unsigned char **audio, size_t *bytes);
  310. /*@}*/
  311. /** \defgroup mpg123_seek mpg123 position and seeking
  312. *
  313. * Functions querying and manipulating position in the decoded audio bitstream.
  314. * The position is measured in decoded audio samples, or MPEG frame offset for the specific functions.
  315. * If gapless code is in effect, the positions are adjusted to compensate the skipped padding/delay - meaning, you should not care about that at all and just use the position defined for the samples you get out of the decoder;-)
  316. * The general usage is modelled after stdlib's ftell() and fseek().
  317. * Especially, the whence parameter for the seek functions has the same meaning as the one for fseek() and needs the same constants from stdlib.h:
  318. * - SEEK_SET: set position to (or near to) specified offset
  319. * - SEEK_CUR: change position by offset from now
  320. * - SEEK_END: set position to offset from end
  321. *
  322. * Note that sample-afccurate seek only works when gapless support has been enabled at compile time; seek is frame-accurate otherwise.
  323. * Also, seeking is not guaranteed to work for all streams (underlying stream may not support it).
  324. *
  325. * @{
  326. */
  327. /** Returns the current position in samples.
  328. * On the next read, you'd get that sample. */
  329. EXPORT off_t mpg123_tell(mpg123_handle *mh);
  330. /** Returns the frame number that the next read will give you data from. */
  331. EXPORT off_t mpg123_tellframe(mpg123_handle *mh);
  332. /** Seek to a desired sample offset.
  333. * Set whence to SEEK_SET, SEEK_CUR or SEEK_END.
  334. * \return The resulting offset >= 0 or error/message code */
  335. EXPORT off_t mpg123_seek(mpg123_handle *mh, off_t sampleoff, int whence);
  336. /** Seek to a desired sample offset in data feeding mode.
  337. * This just prepares things to be right only if you ensure that the next chunk of input data will be from input_offset byte position.
  338. * \param input_offset The position it expects to be at the
  339. * next time data is fed to mpg123_decode().
  340. * \return The resulting offset >= 0 or error/message code */
  341. EXPORT off_t mpg123_feedseek(mpg123_handle *mh, off_t sampleoff, int whence, off_t *input_offset);
  342. /** Seek to a desired MPEG frame index.
  343. * Set whence to SEEK_SET, SEEK_CUR or SEEK_END.
  344. * \return The resulting offset >= 0 or error/message code */
  345. EXPORT off_t mpg123_seek_frame(mpg123_handle *mh, off_t frameoff, int whence);
  346. /** Return a MPEG frame offset corresponding to an offset in seconds.
  347. * This assumes that the samples per frame do not change in the file/stream, which is a good assumption for any sane file/stream only.
  348. * \return frame offset >= 0 or error/message code */
  349. EXPORT off_t mpg123_timeframe(mpg123_handle *mh, double sec);
  350. /** Give access to the frame index table that is managed for seeking.
  351. * You are asked not to modify the values... unless you are really aware of what you are doing.
  352. * \param offsets pointer to the index array
  353. * \param step one index byte offset advances this many MPEG frames
  354. * \param fill number of recorded index offsets; size of the array */
  355. EXPORT int mpg123_index(mpg123_handle *mh, off_t **offsets, off_t *step, size_t *fill);
  356. /** Get information about current and remaining frames/seconds.
  357. * WARNING: This function is there because of special usage by standalone mpg123 and may be removed in the final version of libmpg123!
  358. * You provide an offset (in frames) from now and a number of output bytes
  359. * served by libmpg123 but not yet played. You get the projected current frame
  360. * and seconds, as well as the remaining frames/seconds. This does _not_ care
  361. * about skipped samples due to gapless playback. */
  362. EXPORT int mpg123_position( mpg123_handle *mh, off_t frame_offset,
  363. off_t buffered_bytes, off_t *current_frame,
  364. off_t *frames_left, double *current_seconds,
  365. double *seconds_left);
  366. /*@}*/
  367. /** \defgroup mpg123_voleq mpg123 volume and equalizer
  368. *
  369. * @{
  370. */
  371. enum mpg123_channels
  372. {
  373. MPG123_LEFT=0x1 /**< The Left Channel. */
  374. ,MPG123_RIGHT=0x2 /**< The Right Channel. */
  375. ,MPG123_LR=0x3 /**< Both left and right channel; same as MPG123_LEFT|MPG123_RIGHT */
  376. };
  377. /** Set the 32 Band Audio Equalizer settings.
  378. * \param channel Can be MPG123_LEFT, MPG123_RIGHT or MPG123_LEFT|MPG123_RIGHT for both.
  379. * \param band The equaliser band to change (from 0 to 31)
  380. * \param val The (linear) adjustment factor. */
  381. EXPORT int mpg123_eq(mpg123_handle *mh, enum mpg123_channels channel, int band, double val);
  382. /** Reset the 32 Band Audio Equalizer settings to flat */
  383. EXPORT int mpg123_reset_eq(mpg123_handle *mh);
  384. /** Set the absolute output volume including the RVA setting,
  385. * vol<0 just applies (a possibly changed) RVA setting. */
  386. EXPORT int mpg123_volume(mpg123_handle *mh, double vol);
  387. /** Adjust output volume including the RVA setting by chosen amount */
  388. EXPORT int mpg123_volume_change(mpg123_handle *mh, double change);
  389. /** Return current volume setting, the actual value due to RVA, and the RVA
  390. * adjustment itself. It's all as double float value to abstract the sample
  391. * format. The volume values are linear factors / amplitudes (not percent)
  392. * and the RVA value is in decibels. */
  393. EXPORT int mpg123_getvolume(mpg123_handle *mh, double *base, double *really, double *rva_db);
  394. /* TODO: Set some preamp in addition / to replace internal RVA handling? */
  395. /*@}*/
  396. /** \defgroup mpg123_status mpg123 status and information
  397. *
  398. * @{
  399. */
  400. /** Enumeration of the mode types of Variable Bitrate */
  401. enum mpg123_vbr {
  402. MPG123_CBR=0, /**< Constant Bitrate Mode (default) */
  403. MPG123_VBR, /**< Variable Bitrate Mode */
  404. MPG123_ABR /**< Average Bitrate Mode */
  405. };
  406. /** Enumeration of the MPEG Versions */
  407. enum mpg123_version {
  408. MPG123_1_0=0, /**< MPEG Version 1.0 */
  409. MPG123_2_0, /**< MPEG Version 2.0 */
  410. MPG123_2_5 /**< MPEG Version 2.5 */
  411. };
  412. /** Enumeration of the MPEG Audio mode.
  413. * Only the mono mode has 1 channel, the others have 2 channels. */
  414. enum mpg123_mode {
  415. MPG123_M_STEREO=0, /**< Standard Stereo. */
  416. MPG123_M_JOINT, /**< Joint Stereo. */
  417. MPG123_M_DUAL, /**< Dual Channel. */
  418. MPG123_M_MONO /**< Single Channel. */
  419. };
  420. /** Enumeration of the MPEG Audio flag bits */
  421. enum mpg123_flags {
  422. MPG123_CRC=0x1, /**< The bitstream is error protected using 16-bit CRC. */
  423. MPG123_COPYRIGHT=0x2, /**< The bitstream is copyrighted. */
  424. MPG123_PRIVATE=0x4, /**< The private bit has been set. */
  425. MPG123_ORIGINAL=0x8 /**< The bitstream is an original, not a copy. */
  426. };
  427. /** Data structure for storing information about a frame of MPEG Audio */
  428. struct mpg123_frameinfo
  429. {
  430. enum mpg123_version version; /**< The MPEG version (1.0/2.0/2.5). */
  431. int layer; /**< The MPEG Audio Layer (MP1/MP2/MP3). */
  432. long rate; /**< The sampling rate in Hz. */
  433. enum mpg123_mode mode; /**< The audio mode (Mono, Stereo, Joint-stero, Dual Channel). */
  434. int mode_ext; /**< The mode extension bit flag. */
  435. int framesize; /**< The size of the frame (in bytes). */
  436. enum mpg123_flags flags; /**< MPEG Audio flag bits. */
  437. int emphasis; /**< The emphasis type. */
  438. int bitrate; /**< Bitrate of the frame (kbps). */
  439. int abr_rate; /**< The target average bitrate. */
  440. enum mpg123_vbr vbr; /**< The VBR mode. */
  441. };
  442. /** Get frame information about the MPEG audio bitstream and store it in a mpg123_frameinfo structure. */
  443. EXPORT int mpg123_info(mpg123_handle *mh, struct mpg123_frameinfo *mi);
  444. /** Get the safe output buffer size for all cases (when you want to replace the internal buffer) */
  445. EXPORT size_t mpg123_safe_buffer();
  446. /** Make a full parsing scan of each frame in the file. ID3 tags are found. An accurate length
  447. * value is stored. Seek index will be filled. A seek back to current position
  448. * is performed. At all, this function refuses work when stream is
  449. * not seekable.
  450. * \return MPG123_OK or MPG123_ERR.
  451. */
  452. EXPORT int mpg123_scan(mpg123_handle *mh);
  453. /** Return, if possible, the full (expected) length of current track in samples.
  454. * \return length >= 0 or MPG123_ERR if there is no length guess possible. */
  455. EXPORT off_t mpg123_length(mpg123_handle *mh);
  456. /** Override the value for file size in bytes.
  457. * Useful for getting sensible track length values in feed mode or for HTTP streams.
  458. * \return MPG123_OK or MPG123_ERR */
  459. EXPORT int mpg123_set_filesize(mpg123_handle *mh, off_t size);
  460. /** Returns the time (seconds) per frame; <0 is error. */
  461. EXPORT double mpg123_tpf(mpg123_handle *mh);
  462. /** Get and reset the clip count. */
  463. EXPORT long mpg123_clip(mpg123_handle *mh);
  464. /*@}*/
  465. /** \defgroup mpg123_metadata mpg123 metadata handling
  466. *
  467. * Functions to retrieve the metadata from MPEG Audio files and streams.
  468. * Also includes string handling functions.
  469. *
  470. * @{
  471. */
  472. /** Data structure for storing strings in a safer way than a standard C-String.
  473. * Can also hold a number of null-terminated strings. */
  474. typedef struct
  475. {
  476. char* p; /**< pointer to the string data */
  477. size_t size; /**< raw number of bytes allocated */
  478. size_t fill; /**< number of used bytes (including closing zero byte) */
  479. } mpg123_string;
  480. /** Create and allocate memory for a new mpg123_string */
  481. EXPORT void mpg123_init_string(mpg123_string* sb);
  482. /** Free-up mempory for an existing mpg123_string */
  483. EXPORT void mpg123_free_string(mpg123_string* sb);
  484. /** Change the size of a mpg123_string
  485. * \return 0 on error, 1 on success */
  486. EXPORT int mpg123_resize_string(mpg123_string* sb, size_t news);
  487. /** Increase size of a mpg123_string if necessary (it may stay larger).
  488. * Note that the functions for adding and setting in current libmpg123 use this instead of mpg123_resize_string().
  489. * That way, you can preallocate memory and safely work afterwards with pieces.
  490. * \return 0 on error, 1 on success */
  491. EXPORT int mpg123_grow_string(mpg123_string* sb, size_t news);
  492. /** Copy the contents of one mpg123_string string to another.
  493. * \return 0 on error, 1 on success */
  494. EXPORT int mpg123_copy_string(mpg123_string* from, mpg123_string* to);
  495. /** Append a C-String to an mpg123_string
  496. * \return 0 on error, 1 on success */
  497. EXPORT int mpg123_add_string(mpg123_string* sb, const char* stuff);
  498. /** Append a C-substring to an mpg123 string
  499. * \return 0 on error, 1 on success
  500. * \param from offset to copy from
  501. * \param count number of characters to copy (a null-byte is always appended) */
  502. EXPORT int mpg123_add_substring(mpg123_string *sb, const char *stuff, size_t from, size_t count);
  503. /** Set the conents of a mpg123_string to a C-string
  504. * \return 0 on error, 1 on success */
  505. EXPORT int mpg123_set_string(mpg123_string* sb, const char* stuff);
  506. /** Set the contents of a mpg123_string to a C-substring
  507. * \return 0 on error, 1 on success
  508. * \param from offset to copy from
  509. * \param count number of characters to copy (a null-byte is always appended) */
  510. EXPORT int mpg123_set_substring(mpg123_string *sb, const char *stuff, size_t from, size_t count);
  511. /** Sub data structure for ID3v2, for storing various text fields (including comments).
  512. * This is for ID3v2 COMM, TXXX and all the other text fields.
  513. * Only COMM and TXXX have a description, only COMM has a language.
  514. * You should consult the ID3v2 specification for the use of the various text fields ("frames" in ID3v2 documentation, I use "fields" here to separate from MPEG frames). */
  515. typedef struct
  516. {
  517. char lang[3]; /**< Three-letter language code (not terminated). */
  518. char id[4]; /**< The ID3v2 text field id, like TALB, TPE2, ... (4 characters, no string termination). */
  519. mpg123_string description; /**< Empty for the generic comment... */
  520. mpg123_string text; /**< ... */
  521. } mpg123_text;
  522. /** Data structure for storing IDV3v2 tags.
  523. * This structure is not a direct binary mapping with the file contents.
  524. * The ID3v2 text frames are allowed to contain multiple strings.
  525. * So check for null bytes until you reach the mpg123_string fill.
  526. * All text is encoded in UTF-8. */
  527. typedef struct
  528. {
  529. unsigned char version; /**< 3 or 4 for ID3v2.3 or ID3v2.4. */
  530. mpg123_string *title; /**< Title string (pointer into text_list). */
  531. mpg123_string *artist; /**< Artist string (pointer into text_list). */
  532. mpg123_string *album; /**< Album string (pointer into text_list). */
  533. mpg123_string *year; /**< The year as a string (pointer into text_list). */
  534. mpg123_string *genre; /**< Genre String (pointer into text_list). The genre string(s) may very well need postprocessing, esp. for ID3v2.3. */
  535. mpg123_string *comment; /**< Pointer to last encountered comment text with empty description. */
  536. /* Encountered ID3v2 fields are appended to these lists.
  537. There can be multiple occurences, the pointers above always point to the last encountered data. */
  538. mpg123_text *comment_list; /**< Array of comments. */
  539. size_t comments; /**< Number of comments. */
  540. mpg123_text *text; /**< Array of ID3v2 text fields */
  541. size_t texts; /**< Numer of text fields. */
  542. mpg123_text *extra; /**< The array of extra (TXXX) fields. */
  543. size_t extras; /**< Number of extra text (TXXX) fields. */
  544. } mpg123_id3v2;
  545. /** Data structure for ID3v1 tags (the last 128 bytes of a file).
  546. * Don't take anything for granted (like string termination)!
  547. * Also note the change ID3v1.1 did: comment[28] = 0; comment[19] = track_number
  548. * It is your task to support ID3v1 only or ID3v1.1 ...*/
  549. typedef struct
  550. {
  551. char tag[3]; /**< Always the string "TAG", the classic intro. */
  552. char title[30]; /**< Title string. */
  553. char artist[30]; /**< Artist string. */
  554. char album[30]; /**< Album string. */
  555. char year[4]; /**< Year string. */
  556. char comment[30]; /**< Comment string. */
  557. unsigned char genre; /**< Genre index. */
  558. } mpg123_id3v1;
  559. #define MPG123_ID3 0x3 /**< 0011 There is some ID3 info. Also matches 0010 or NEW_ID3. */
  560. #define MPG123_NEW_ID3 0x1 /**< 0001 There is ID3 info that changed since last call to mpg123_id3. */
  561. #define MPG123_ICY 0xc /**< 1100 There is some ICY info. Also matches 0100 or NEW_ICY.*/
  562. #define MPG123_NEW_ICY 0x4 /**< 0100 There is ICY info that changed since last call to mpg123_icy. */
  563. /** Query if there is (new) meta info, be it ID3 or ICY (or something new in future).
  564. The check function returns a combination of flags. */
  565. EXPORT int mpg123_meta_check(mpg123_handle *mh); /* On error (no valid handle) just 0 is returned. */
  566. /** Point v1 and v2 to existing data structures wich may change on any next read/decode function call.
  567. * v1 and/or v2 can be set to NULL when there is no corresponding data.
  568. * \return Return value is MPG123_OK or MPG123_ERR, */
  569. EXPORT int mpg123_id3(mpg123_handle *mh, mpg123_id3v1 **v1, mpg123_id3v2 **v2);
  570. /** Point icy_meta to existing data structure wich may change on any next read/decode function call.
  571. * \return Return value is MPG123_OK or MPG123_ERR, */
  572. EXPORT int mpg123_icy(mpg123_handle *mh, char **icy_meta); /* same for ICY meta string */
  573. /** Decode from windows-1252 (the encoding ICY metainfo used) to UTF-8.
  574. * \param icy_text The input data in ICY encoding
  575. * \return pointer to newly allocated buffer with UTF-8 data (You free() it!) */
  576. EXPORT char* mpg123_icy2utf8(const char* icy_text);
  577. /* @} */
  578. /** \defgroup mpg123_advpar mpg123 advanced parameter API
  579. *
  580. * Direct access to a parameter set without full handle around it.
  581. * Possible uses:
  582. * - Influence behaviour of library _during_ initialization of handle (MPG123_VERBOSE).
  583. * - Use one set of parameters for multiple handles.
  584. *
  585. * The functions for handling mpg123_pars (mpg123_par() and mpg123_fmt()
  586. * family) directly return a fully qualified mpg123 error code, the ones
  587. * operating on full handles normally MPG123_OK or MPG123_ERR, storing the
  588. * specific error code itseld inside the handle.
  589. *
  590. * @{
  591. */
  592. /** Opaque structure for the libmpg123 decoder parameters. */
  593. struct mpg123_pars_struct;
  594. /** Opaque structure for the libmpg123 decoder parameters. */
  595. typedef struct mpg123_pars_struct mpg123_pars;
  596. /** Create a handle with preset parameters. */
  597. EXPORT mpg123_handle *mpg123_parnew(mpg123_pars *mp, const char* decoder, int *error);
  598. /** Allocate memory for and return a pointer to a new mpg123_pars */
  599. EXPORT mpg123_pars *mpg123_new_pars(int *error);
  600. /** Delete and free up memory used by a mpg123_pars data structure */
  601. EXPORT void mpg123_delete_pars(mpg123_pars* mp);
  602. /** Configure mpg123 parameters to accept no output format at all,
  603. * use before specifying supported formats with mpg123_format */
  604. EXPORT int mpg123_fmt_none(mpg123_pars *mp);
  605. /** Configure mpg123 parameters to accept all formats
  606. * (also any custom rate you may set) -- this is default. */
  607. EXPORT int mpg123_fmt_all(mpg123_pars *mp);
  608. /** Set the audio format support of a mpg123_pars in detail:
  609. \param rate The sample rate value (in Hertz).
  610. \param channels A combination of MPG123_STEREO and MPG123_MONO.
  611. \param encodings A combination of accepted encodings for rate and channels, p.ex MPG123_ENC_SIGNED16|MPG123_ENC_ULAW_8 (or 0 for no support).
  612. \return 0 on success, -1 if there was an error. /
  613. */
  614. EXPORT int mpg123_fmt(mpg123_pars *mh, long rate, int channels, int encodings); /* 0 is good, -1 is error */
  615. /** Check to see if a specific format at a specific rate is supported
  616. * by mpg123_pars.
  617. * \return 0 for no support (that includes invalid parameters), MPG123_STEREO,
  618. * MPG123_MONO or MPG123_STEREO|MPG123_MONO. */
  619. EXPORT int mpg123_fmt_support(mpg123_pars *mh, long rate, int encoding);
  620. /** Set a specific parameter, for a specific mpg123_pars, using a parameter
  621. * type key chosen from the mpg123_parms enumeration, to the specified value. */
  622. EXPORT int mpg123_par(mpg123_pars *mp, enum mpg123_parms type, long value, double fvalue);
  623. /** Get a specific parameter, for a specific mpg123_pars.
  624. * See the mpg123_parms enumeration for a list of available parameters. */
  625. EXPORT int mpg123_getpar(mpg123_pars *mp, enum mpg123_parms type, long *val, double *fval);
  626. /* @} */
  627. /** \defgroup mpg123_lowio mpg123 low level I/O
  628. * You may want to do tricky stuff with I/O that does not work with mpg123's default file access or you want to make it decode into your own pocket...
  629. *
  630. * @{ */
  631. /** Replace default internal buffer with user-supplied buffer.
  632. * Instead of working on it's own private buffer, mpg123 will directly use the one you provide for storing decoded audio. */
  633. EXPORT int mpg123_replace_buffer(mpg123_handle *mh, unsigned char *data, size_t size);
  634. /** The max size of one frame's decoded output with current settings.
  635. * Use that to determine an appropriate minimum buffer size for decoding one frame. */
  636. EXPORT size_t mpg123_outblock(mpg123_handle *mh);
  637. /** Replace low-level stream access functions; read and lseek as known in POSIX.
  638. * You can use this to make any fancy file opening/closing yourself,
  639. * using open_fd to set the file descriptor for your read/lseek (doesn't need to be a "real" file descriptor...).
  640. * Setting a function to NULL means that the default internal read is
  641. * used (active from next mpg123_open call on). */
  642. EXPORT int mpg123_replace_reader( mpg123_handle *mh,
  643. ssize_t (*r_read) (int, void *, size_t),
  644. off_t (*r_lseek)(int, off_t, int) );
  645. /* @} */
  646. #ifdef __cplusplus
  647. }
  648. #endif
  649. #endif