1
0

ID3v1Metadata.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #include "ID3v1Metadata.h"
  2. #include "metadata/MetadataKeys.h"
  3. #include <stdlib.h>
  4. api_metadata *ID3v1Metadata::metadata_api=0;
  5. ID3v1Metadata::ID3v1Metadata()
  6. {
  7. id3v1_tag=0;
  8. }
  9. ID3v1Metadata::~ID3v1Metadata()
  10. {
  11. }
  12. int ID3v1Metadata::Initialize(api_metadata *metadata_api)
  13. {
  14. ID3v1Metadata::metadata_api = metadata_api;
  15. return NErr_Success;
  16. }
  17. int ID3v1Metadata::Initialize(nsid3v1_tag_t tag)
  18. {
  19. id3v1_tag = tag;
  20. this->metadata_api = metadata_api;
  21. return NErr_Success;
  22. }
  23. /* ifc_metadata implementation */
  24. int ID3v1Metadata::Metadata_GetField(int field, unsigned int index, nx_string_t *value)
  25. {
  26. if (!id3v1_tag)
  27. return NErr_Unknown;
  28. switch (field)
  29. {
  30. case MetadataKeys::TITLE:
  31. return index?NErr_EndOfEnumeration:NSID3v1_Get_Title(id3v1_tag, value);
  32. case MetadataKeys::ARTIST:
  33. return index?NErr_EndOfEnumeration:NSID3v1_Get_Artist(id3v1_tag, value);
  34. case MetadataKeys::ALBUM:
  35. return index?NErr_EndOfEnumeration:NSID3v1_Get_Album(id3v1_tag, value);
  36. case MetadataKeys::YEAR:
  37. return index?NErr_EndOfEnumeration:NSID3v1_Get_Year(id3v1_tag, value);
  38. case MetadataKeys::COMMENT:
  39. return index?NErr_EndOfEnumeration:NSID3v1_Get_Comment(id3v1_tag, value);
  40. case MetadataKeys::TRACK:
  41. return index?NErr_EndOfEnumeration:NSID3v1_Get_Track(id3v1_tag, value);
  42. case MetadataKeys::GENRE:
  43. {
  44. if (!metadata_api)
  45. return NErr_Unknown;
  46. if (index > 0)
  47. return NErr_EndOfEnumeration;
  48. uint8_t genre_id;
  49. int ret = NSID3v1_Int_Get_Genre(id3v1_tag, &genre_id);
  50. if (ret != NErr_Success)
  51. return ret;
  52. nx_string_t genre;
  53. ret = metadata_api->GetGenre(genre_id, &genre);
  54. if (ret == NErr_Success)
  55. {
  56. *value = NXStringRetain(genre);
  57. return NErr_Success;
  58. }
  59. else if (ret == NErr_Unknown)
  60. {
  61. return NErr_Empty;
  62. }
  63. else
  64. {
  65. return ret;
  66. }
  67. }
  68. }
  69. return NErr_Unknown;
  70. }
  71. int ID3v1Metadata::Metadata_GetInteger(int field, unsigned int index, int64_t *value)
  72. {
  73. if (!id3v1_tag)
  74. return NErr_Unknown;
  75. switch (field)
  76. {
  77. case MetadataKeys::YEAR:
  78. {
  79. if (index > 0)
  80. return NErr_EndOfEnumeration;
  81. unsigned int year;
  82. int ret = NSID3v1_Int_Get_Year(id3v1_tag, &year);
  83. if (ret == NErr_Success)
  84. *value = (int64_t)year;
  85. return ret;
  86. }
  87. case MetadataKeys::TRACK:
  88. {
  89. if (index > 0)
  90. return NErr_EndOfEnumeration;
  91. uint8_t track;
  92. int ret = NSID3v1_Int_Get_Track(id3v1_tag, &track);
  93. if (ret == NErr_Success)
  94. *value = (int64_t)track;
  95. return ret;
  96. }
  97. }
  98. return NErr_Unknown;
  99. }
  100. int ID3v1Metadata::Metadata_GetReal(int field, unsigned int index, double *value)
  101. {
  102. if (!id3v1_tag)
  103. return NErr_Unknown;
  104. return NErr_Unknown;
  105. }
  106. int ID3v1Metadata::MetadataEditor_SetField(int field, unsigned int index, nx_string_t value)
  107. {
  108. if (!id3v1_tag)
  109. return NErr_NullPointer;
  110. switch (field)
  111. {
  112. case MetadataKeys::TITLE:
  113. return index?NErr_EndOfEnumeration:NSID3v1_Set_Title(id3v1_tag, value);
  114. case MetadataKeys::ARTIST:
  115. return index?NErr_EndOfEnumeration:NSID3v1_Set_Artist(id3v1_tag, value);
  116. case MetadataKeys::ALBUM:
  117. return index?NErr_EndOfEnumeration:NSID3v1_Set_Album(id3v1_tag, value);
  118. case MetadataKeys::YEAR:
  119. return index?NErr_EndOfEnumeration:NSID3v1_Set_Year(id3v1_tag, value);
  120. case MetadataKeys::COMMENT:
  121. return index?NErr_EndOfEnumeration:NSID3v1_Set_Comment(id3v1_tag, value);
  122. case MetadataKeys::TRACK:
  123. return index?NErr_EndOfEnumeration:NSID3v1_Set_Track(id3v1_tag, value);
  124. case MetadataKeys::GENRE:
  125. {
  126. if (!metadata_api)
  127. return NErr_Unknown;
  128. if (index > 0)
  129. return NErr_EndOfEnumeration;
  130. uint8_t genre_id;
  131. int ret = metadata_api->GetGenreID(value, &genre_id);
  132. if (ret == NErr_Success)
  133. return NSID3v1_Int_Set_Genre(id3v1_tag, genre_id);
  134. else
  135. return NSID3v1_Int_Set_Genre(id3v1_tag, 0xFF);
  136. }
  137. }
  138. return NErr_Unknown;
  139. }
  140. int ID3v1Metadata::MetadataEditor_SetInteger(int field, unsigned int index, int64_t value)
  141. {
  142. if (!id3v1_tag)
  143. return NErr_NullPointer;
  144. if (index != 0)
  145. return NErr_EndOfEnumeration;
  146. switch (field)
  147. {
  148. case MetadataKeys::YEAR:
  149. return NSID3v1_Int_Set_Year(id3v1_tag, (unsigned int)value);
  150. case MetadataKeys::TRACK:
  151. if (value < 0 || value > 255)
  152. return NErr_ParameterOutOfRange;
  153. return NSID3v1_Int_Set_Track(id3v1_tag, (uint8_t)value);
  154. case MetadataKeys::GENRE:
  155. if (value < 0 || value > 255)
  156. return NErr_ParameterOutOfRange;
  157. return NSID3v1_Int_Set_Genre(id3v1_tag, (uint8_t)value);
  158. }
  159. return NErr_Unknown;
  160. }
  161. #undef DESCRIPTION