PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/libtunepimp-0.5.3/plugins/flac/flac.cpp

#
C++ | 232 lines | 166 code | 36 blank | 30 comment | 9 complexity | 28c3a3927a01961df067c58dff7b4e0d MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0, GPL-2.0, LGPL-2.0
  1. /*----------------------------------------------------------------------------
  2. libtunepimp -- The MusicBrainz tagging library.
  3. Let a thousand taggers bloom!
  4. Copyright (C) Robert Kaye 2003
  5. This file is part of libtunepimp.
  6. libtunepimp is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. libtunepimp is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with libtunepimp; if not, write to the Free Software
  16. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. $Id: flac.cpp 1441 2005-12-18 19:16:49Z luks $
  18. ----------------------------------------------------------------------------*/
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include <assert.h>
  23. #include <errno.h>
  24. #ifdef WIN32
  25. #include <winsock.h>
  26. #else
  27. #include <netinet/in.h>
  28. #include <sys/param.h>
  29. #endif
  30. #include "plugin.h"
  31. #include "flacdecode.h"
  32. #include "flac_meta.h"
  33. extern "C"
  34. {
  35. /*-------------------------------------------------------------------------*/
  36. #ifndef WIN32
  37. #define initPlugin flacInitPlugin
  38. #endif
  39. Plugin *initPlugin (void);
  40. static void flacShutdown (void);
  41. static const char *flacGetVersion (void);
  42. static const char *flacGetName (void);
  43. static int flacGetNumFormats(void);
  44. static int flacGetFormat (int, char ext[TP_EXTENSION_LEN],
  45. char desc[TP_PLUGIN_DESC_LEN], int *functions);
  46. static const char *flacGetError (void);
  47. static int flacWriteMetadata(const metadata_t *mdata, const char *fileName, int flags, const char *encoding);
  48. static int flacReadMetadata (metadata_t *mdata, const char *fileName, int flags, const char *encoding);
  49. static unsigned long flacGetDuration (const char *file, int flags, const char *encoding);
  50. static void *flacDecodeStart (const char *file, int flags, const char *encoding);
  51. static int flacDecodeInfo (void *decode,
  52. unsigned long *duration,
  53. unsigned int *samplesPerSecond,
  54. unsigned int *bitsPerSample,
  55. unsigned int *channels);
  56. static int flacDecodeRead (void *decode, char *data, int maxBytes);
  57. static void flacDecodeEnd (void *decode);
  58. /*-------------------------------------------------------------------------*/
  59. #define PLUGIN_VERSION "1.0.0"
  60. #define PLUGIN_NAME "FLAC decoder & metadata reader/writer"
  61. static char *errorString = "";
  62. /*-------------------------------------------------------------------------*/
  63. static Plugin methods =
  64. {
  65. flacShutdown,
  66. flacGetVersion,
  67. flacGetName,
  68. flacGetNumFormats,
  69. flacGetFormat,
  70. flacGetError,
  71. flacReadMetadata,
  72. flacWriteMetadata,
  73. flacGetDuration,
  74. flacDecodeStart,
  75. flacDecodeInfo,
  76. flacDecodeRead,
  77. flacDecodeEnd
  78. };
  79. /*-------------------------------------------------------------------------*/
  80. Plugin *initPlugin(void)
  81. {
  82. return &methods;
  83. }
  84. static void flacShutdown(void)
  85. {
  86. if (strlen(errorString))
  87. free(errorString);
  88. }
  89. static const char *flacGetVersion(void)
  90. {
  91. return PLUGIN_VERSION;
  92. }
  93. static const char *flacGetName(void)
  94. {
  95. return PLUGIN_NAME;
  96. }
  97. static int flacGetNumFormats(void)
  98. {
  99. return 1;
  100. }
  101. static int flacGetFormat(int i, char ext[TP_EXTENSION_LEN],
  102. char desc[TP_PLUGIN_DESC_LEN], int *functions)
  103. {
  104. if (i > 0)
  105. return 0;
  106. strcpy(ext, ".flac");
  107. strcpy(desc, "FLAC Lossless audio format");
  108. *functions = TP_PLUGIN_FUNCTION_DECODE | TP_PLUGIN_FUNCTION_METADATA;
  109. return 1;
  110. }
  111. static const char *flacGetError(void)
  112. {
  113. return errorString;
  114. }
  115. static void setError(const string &err)
  116. {
  117. if (err.length())
  118. {
  119. if (errorString)
  120. free(errorString);
  121. errorString = strdup(err.c_str());
  122. }
  123. }
  124. static int flacReadMetadata(metadata_t *mdata, const char *fileName, int flags, const char *encoding)
  125. {
  126. Metadata data;
  127. FLAC flac(encoding);
  128. if (flac.read(fileName, data))
  129. {
  130. data.writeToC(mdata);
  131. return 1;
  132. }
  133. else
  134. {
  135. string err;
  136. flac.getError(err);
  137. setError(err);
  138. }
  139. return 0;
  140. }
  141. static int flacWriteMetadata(const metadata_t *mdata, const char *fileName, int flags, const char *encoding)
  142. {
  143. Metadata data;
  144. FLAC flac(encoding);
  145. data.readFromC(mdata);
  146. int ret = flac.write(fileName, data, (flags & TP_PLUGIN_FLAGS_GENERAL_CLEAR_TAGS) != 0);
  147. if (!ret)
  148. {
  149. string err;
  150. flac.getError(err);
  151. setError(err);
  152. }
  153. return ret;
  154. }
  155. static unsigned long flacGetDuration(const char *fileName, int flags, const char *encoding)
  156. {
  157. FlacDecode *decode;
  158. unsigned long duration;
  159. unsigned int samplesPerSecond, bitsPerSample, channels;
  160. decode = new FlacDecode(fileName, encoding);
  161. if (!decode)
  162. return 0;
  163. if (!decode->getInfo(duration, samplesPerSecond, bitsPerSample, channels))
  164. duration = 0;
  165. delete decode;
  166. return duration;
  167. }
  168. static void *flacDecodeStart(const char *fileName, int flags, const char *encoding)
  169. {
  170. return (void *)new FlacDecode(fileName, encoding);
  171. }
  172. static int flacDecodeInfo(void *decode,
  173. unsigned long *duration,
  174. unsigned int *samplesPerSecond,
  175. unsigned int *bitsPerSample,
  176. unsigned int *channels)
  177. {
  178. return ((FlacDecode *)decode)->getInfo(*duration, *samplesPerSecond, *bitsPerSample, *channels);
  179. }
  180. static int flacDecodeRead(void *decode, char *data, int maxBytes)
  181. {
  182. return ((FlacDecode *)decode)->read(data, maxBytes);
  183. }
  184. static void flacDecodeEnd(void *decode)
  185. {
  186. delete (FlacDecode *)decode;
  187. }
  188. } // extern "C"