@@ -10,32 +10,44 @@ namespace FLAC {
1010 const FLAC__Frame *frame = nullptr ;
1111 const FLAC__int32 * const *buffer = nullptr ;
1212 FLAC__uint8 channels = 0 ;
13- FLAC__uint32 sample_rate = 0 ;
13+ FLAC__uint32 rate = 0 ;
1414 FLAC__uint32 bps = 0 ;
1515 FLAC__uint64 position = 0 ;
16- FLAC__uint64 samples_read = 0 ;
17- FLAC__uint64 total_samples = 0 ;
16+ FLAC__uint64 samples = 0 ;
17+ FLAC__uint64 totalSamples = 0 ;
1818 } FLACInfo;
1919
2020 static FLAC__StreamDecoder *flac = nullptr ;
21- static FLACInfo cb_info { 0 };
21+ static FLACInfo info { 0 };
2222
23- static FLAC__StreamDecoderWriteStatus WriteCB (const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data) {
23+ static FLAC__StreamDecoderWriteStatus WriteCB (const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *const buffer[], void *client_data) {
2424 FLACInfo *info = reinterpret_cast <FLACInfo *>(client_data);
2525
26- if (info->total_samples == 0 ) {
27- Log::Error (" No samples to decode! \n " );
26+ if (info->totalSamples == 0 ) {
27+ Log::Error (" This decoder only works for FLAC files that have a total_samples count in STREAMINFO \n " );
2828 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
2929 }
3030 if ((info->channels != 2 ) || (info->bps != 16 )) {
31- Log::Error (" Not stereo 16-bit!\n " );
31+ Log::Error (" This decoder only supports 16bit stereo streams\n " );
32+ return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
33+ }
34+ if (frame->header .channels != 2 ) {
35+ Log::Error (" This frame contains %u channels (should be 2)\n " , frame->header .channels );
36+ return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
37+ }
38+ if (buffer[0 ] == NULL ) {
39+ Log::Error (" buffer [0] is NULL\n " );
40+ return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
41+ }
42+ if (buffer[1 ] == NULL ) {
43+ Log::Error (" buffer [1] is NULL\n " );
3244 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
3345 }
3446
3547 info->frame = frame;
3648 info->buffer = buffer;
3749 info->position = 0 ;
38- info->samples_read = frame->header .number .sample_number ;
50+ info->samples = frame->header .number .sample_number ;
3951 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
4052 }
4153
@@ -44,43 +56,40 @@ namespace FLAC {
4456
4557 switch (stream->type ) {
4658 case FLAC__METADATA_TYPE_STREAMINFO:
47- info->total_samples = stream->data .stream_info .total_samples ;
48- info->sample_rate = stream->data .stream_info .sample_rate ;
59+ info->totalSamples = stream->data .stream_info .total_samples ;
60+ info->rate = stream->data .stream_info .sample_rate ;
4961 info->channels = stream->data .stream_info .channels ;
5062 info->bps = stream->data .stream_info .bits_per_sample ;
5163 break ;
5264
5365 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
5466 for (FLAC__uint32 i = 0 ; i < stream->data .vorbis_comment .num_comments ; i++) {
5567 char *tag = reinterpret_cast <char *>(stream->data .vorbis_comment .comments [i].entry );
68+ if (tag != nullptr ) {
69+ metadata.hasMeta = true ;
70+ }
5671
5772 if (!strncasecmp (" TITLE=" , tag, 6 )) {
58- metadata.has_meta = true ;
5973 metadata.title = tag + 6 ;
6074 }
6175
6276 if (!strncasecmp (" ALBUM=" , tag, 6 )) {
63- metadata.has_meta = true ;
6477 metadata.album = tag + 6 ;
6578 }
6679
6780 if (!strncasecmp (" ARTIST=" , tag, 7 )) {
68- metadata.has_meta = true ;
6981 metadata.artist = tag + 7 ;
7082 }
7183
7284 if (!strncasecmp (" DATE=" , tag, 5 )) {
73- metadata.has_meta = true ;
7485 metadata.year = tag + 5 ;
7586 }
7687
7788 if (!strncasecmp (" COMMENT=" , tag, 8 )) {
78- metadata.has_meta = true ;
7989 metadata.comment = tag + 8 ;
8090 }
8191
8292 if (!strncasecmp (" GENRE=" , tag, 6 )) {
83- metadata.has_meta = true ;
8493 metadata.genre = tag + 6 ;
8594 }
8695 }
@@ -89,16 +98,14 @@ namespace FLAC {
8998 case FLAC__METADATA_TYPE_PICTURE:
9099 if (stream->data .picture .type == FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER) {
91100 if ((!strcasecmp (stream->data .picture .mime_type , " image/jpeg" )) || (!strcasecmp (stream->data .picture .mime_type , " image/jpg" ))) {
92- metadata.has_meta = true ;
93-
94- if (!metadata.cover_image )
95- metadata.cover_image = Textures::LoadImageBufferJPEG (stream->data .picture .data , stream->data .picture .data_length );
101+ if (!metadata.image ) {
102+ metadata.image = Textures::LoadImageBufferJPEG (stream->data .picture .data , stream->data .picture .data_length );
103+ }
96104 }
97105 else if (!strcasecmp (stream->data .picture .mime_type , " image/png" )) {
98- metadata.has_meta = true ;
99-
100- if (!metadata.cover_image )
101- metadata.cover_image = Textures::LoadImageBufferPNG (stream->data .picture .data , stream->data .picture .data_length );
106+ if (!metadata.image ) {
107+ metadata.image = Textures::LoadImageBufferPNG (stream->data .picture .data , stream->data .picture .data_length );
108+ }
102109 }
103110 }
104111 break ;
@@ -109,53 +116,34 @@ namespace FLAC {
109116 }
110117
111118 static void ErrorCB (const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data) {
112- std::string error;
113-
114- switch (status) {
115- case FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC:
116- error = " Lost sync" ;
117- break ;
118-
119- case FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER:
120- error = " Bad header" ;
121- break ;
122-
123- case FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH:
124- error = " Frame CRC mismatch" ;
125- break ;
126-
127- case FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM:
128- error = " Unparseable stream" ;
129- break ;
130-
131- default :
132- break ;
133- }
134-
135- Log::Error (" error: %s\n " , error.c_str ());
119+ Log::Error (" FLAC error callback: %s\n " , FLAC__StreamDecoderErrorStatusString[status]);
136120 }
137121
138122 int Init (const std::string &path) {
139123 FLAC__StreamDecoderInitStatus ret = FLAC__STREAM_DECODER_INIT_STATUS_OK;
140124
141- if ((flac = FLAC__stream_decoder_new ()) == nullptr )
125+ if ((flac = FLAC__stream_decoder_new ()) == nullptr ) {
142126 return -1 ;
127+ }
143128
144129 if (FLAC__stream_decoder_set_metadata_respond (flac, FLAC__METADATA_TYPE_STREAMINFO) == false ) {
145130 Log::Error (" FLAC__METADATA_TYPE_STREAMINFO response failed\n " );
146131 return -1 ;
147132 }
148133
149- if (FLAC__stream_decoder_set_metadata_respond (flac, FLAC__METADATA_TYPE_SEEKTABLE) == false )
134+ if (FLAC__stream_decoder_set_metadata_respond (flac, FLAC__METADATA_TYPE_SEEKTABLE) == false ) {
150135 Log::Error (" FLAC__METADATA_TYPE_SEEKTABLE response failed\n " );
136+ }
151137
152- if (FLAC__stream_decoder_set_metadata_respond (flac, FLAC__METADATA_TYPE_VORBIS_COMMENT) == false )
138+ if (FLAC__stream_decoder_set_metadata_respond (flac, FLAC__METADATA_TYPE_VORBIS_COMMENT) == false ) {
153139 Log::Error (" FLAC__METADATA_TYPE_VORBIS_COMMENT response failed\n " );
140+ }
154141
155- if (FLAC__stream_decoder_set_metadata_respond (flac, FLAC__METADATA_TYPE_PICTURE) == false )
142+ if (FLAC__stream_decoder_set_metadata_respond (flac, FLAC__METADATA_TYPE_PICTURE) == false ) {
156143 Log::Error (" FLAC__METADATA_TYPE_PICTURE response failed\n " );
144+ }
157145
158- if ((ret = FLAC__stream_decoder_init_file (flac, path.c_str (), FLAC::WriteCB, FLAC::MetadataCB, FLAC::ErrorCB, &cb_info )) != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
146+ if ((ret = FLAC__stream_decoder_init_file (flac, path.c_str (), FLAC::WriteCB, FLAC::MetadataCB, FLAC::ErrorCB, &info )) != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
159147 Log::Error (" FLAC__stream_decoder_init_file failed: %s\n " , FLAC__StreamDecoderInitStatusString[ret]);
160148 return ret;
161149 }
@@ -164,28 +152,31 @@ namespace FLAC {
164152 }
165153
166154 u32 GetSampleRate (void ) {
167- return cb_info. sample_rate ;
155+ return info. rate ;
168156 }
169157
170158 u8 GetChannels (void ) {
171- return cb_info .channels ;
159+ return info .channels ;
172160 }
173161
174162 void Decode (void *buf, unsigned int length, void *userdata) {
175163 unsigned int decoded = 0 ;
176164 FLAC__bool ret = false ;
177165
178- if (length <= 0 )
166+ if (length <= 0 ) {
179167 return ;
168+ }
180169
181170 FLAC__StreamDecoderState state = FLAC__stream_decoder_get_state (flac);
182- if (state == FLAC__STREAM_DECODER_END_OF_STREAM)
171+ if (state == FLAC__STREAM_DECODER_END_OF_STREAM) {
183172 playing = false ;
184- else if (state == FLAC__STREAM_DECODER_ABORTED)
173+ }
174+ else if (state == FLAC__STREAM_DECODER_ABORTED) {
185175 return ;
176+ }
186177
187178 while (decoded < length) {
188- if ((cb_info .frame == nullptr ) || (cb_info .position == cb_info .frame ->header .blocksize )) {
179+ if ((info .frame == nullptr ) || (info .position == info .frame ->header .blocksize )) {
189180 ret = FLAC__stream_decoder_process_single (flac);
190181
191182 if (ret == false ) {
@@ -201,30 +192,30 @@ namespace FLAC {
201192 }
202193 }
203194
204- for (; decoded < length && cb_info .position < cb_info .frame ->header .blocksize ; cb_info .position ++, decoded++) {
195+ for (; decoded < length && info .position < info .frame ->header .blocksize ; info .position ++, decoded++) {
205196 // Copy to buffer here; convert from BE to LE
206197 short *buffer = static_cast <short *>(buf);
207- buffer[decoded * 2 ] = cb_info .buffer [0 ][cb_info .position ];
208- buffer[decoded * 2 + 1 ] = cb_info .buffer [1 ][cb_info .position ];
198+ buffer[decoded * 2 ] = info .buffer [0 ][info .position ];
199+ buffer[decoded * 2 + 1 ] = info .buffer [1 ][info .position ];
209200 }
210201 }
211202 }
212203
213204 u64 GetPosition (void ) {
214- return cb_info. samples_read ;
205+ return info. samples ;
215206 }
216207
217208 u64 GetLength (void ) {
218- return cb_info. total_samples ;
209+ return info. totalSamples ;
219210 }
220211
221212 u64 Seek (u64 index) {
222- FLAC__uint64 seek_sample = (cb_info. total_samples * (index / 225.0 ));
223- return FLAC__stream_decoder_seek_absolute (flac, seek_sample );
213+ FLAC__uint64 seek = (info. totalSamples * (index / 225.0 ));
214+ return FLAC__stream_decoder_seek_absolute (flac, seek );
224215 }
225216
226217 void Exit (void ) {
227- cb_info = { 0 };
218+ info = { 0 };
228219 FLAC__stream_decoder_finish (flac);
229220 FLAC__stream_decoder_delete (flac);
230221 }
0 commit comments