Advertisement
Guest User

Finding first audio frame in a MP3 file

a guest
Apr 26th, 2019
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.35 KB | None | 0 0
  1. // XXX: Find the first sync
  2.  
  3. uint32\_t id3\_bytes\_read = 0;
  4.  
  5. uint8\_t id3\_buffer\[2881\] = {0};
  6.  
  7. uint32\_t last\_file\_location = 0;
  8.  
  9. uint32\_t current\_file\_location = 0;
  10.  
  11. ptrdiff\_t diff = 0;
  12.  
  13.  
  14. do {
  15.  
  16. last_file_location = f_tell(&track);
  17.  
  18. // XXX: Get a chunk of data from the SD card
  19.  
  20. retSD = f_read(&track, id3_buffer, 1024, (UINT *) &id3_bytes_read);
  21.  
  22. if (FR_OK != retSD) {
  23.  
  24. FatFsError(retSD);
  25.  
  26.     }
  27.  
  28. // XXX: Set the read data as mad stream
  29.  
  30.     mad\_stream\_buffer(&id3\_stream, (unsigned char const \*) id3\_buffer, sizeof id3\_buffer);
  31.  
  32. // XXX: Try to decode it, TODO: check return
  33.  
  34.     mad\_frame\_decode(&id3\_frame, &id3\_stream);
  35.  
  36. // XXX: Try to find a SYNC flag
  37.  
  38.     if (0 == mad\_stream\_sync(&id3\_stream)) {
  39.  
  40. diff = id3_stream.this_frame - id3_buffer;
  41.  
  42. break;
  43.  
  44.     }
  45.  
  46.     HAL\_Delay(25);
  47.  
  48. } while (1);
  49.  
  50.  
  51. current_file_location = f_tell(&track);
  52.  
  53. DBG_println("We're at 0x%X", current_file_location);
  54.  
  55. DBG_println("Diff: 0x%X (%d)", current_file_location - (last_file_location + diff), current_file_location - (last_file_location + diff));
  56.  
  57. // XXX: Go back N bytes so from now on we read data from the location of the first SYNC flag
  58.  
  59. retSD = f_lseek(&track, current_file_location - (current_file_location - last_file_location + diff));
  60.  
  61.  
  62. // XXX: We're done
  63.  
  64. mad_stream_finish(&id3_stream);
  65.  
  66. mad_frame_finish(&id3_frame);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement