You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.8KB

  1. #include "riff-acm.h"
  2. int wave_format_load(const uint8_t* data, int bytes, struct wave_format_t* wav)
  3. {
  4. if (bytes < 18)
  5. return -1;
  6. // little endian
  7. wav->wFormatTag = data[0] | ((uint16_t)data[1] << 8);
  8. wav->nChannels = data[2] | ((uint16_t)data[3] << 8);
  9. wav->nSamplesPerSec = ((uint32_t)data[4] << 0) | ((uint32_t)data[5] << 8) | ((uint32_t)data[6] << 16) | ((uint32_t)data[7] << 24);
  10. wav->nAvgBytesPerSec = ((uint32_t)data[8] << 0) | ((uint32_t)data[9] << 8) | ((uint32_t)data[10] << 16) | ((uint32_t)data[11] << 24);
  11. wav->nBlockAlign = data[12] | ((uint16_t)data[13] << 8);
  12. wav->wBitsPerSample = data[14] | ((uint16_t)data[15] << 8);
  13. wav->cbSize = data[16] | ((uint16_t)data[17] << 8);
  14. if (18 + wav->cbSize > bytes)
  15. return -1;
  16. return 18 + wav->cbSize;
  17. }
  18. int wave_format_save(const struct wave_format_t* wav, uint8_t* data, int bytes)
  19. {
  20. if (bytes < wav->cbSize)
  21. return -1;
  22. // little endian
  23. data[0] = (uint8_t)wav->wFormatTag;
  24. data[1] = (uint8_t)(wav->wFormatTag >> 8);
  25. data[2] = (uint8_t)wav->nChannels;
  26. data[3] = (uint8_t)(wav->nChannels >> 8);
  27. data[4] = (uint8_t)wav->nSamplesPerSec;
  28. data[5] = (uint8_t)(wav->nSamplesPerSec >> 8);
  29. data[6] = (uint8_t)(wav->nSamplesPerSec >> 16);
  30. data[7] = (uint8_t)(wav->nSamplesPerSec >> 24);
  31. data[8] = (uint8_t)wav->nAvgBytesPerSec;
  32. data[9] = (uint8_t)(wav->nAvgBytesPerSec >> 8);
  33. data[10] = (uint8_t)(wav->nAvgBytesPerSec >> 16);
  34. data[11] = (uint8_t)(wav->nAvgBytesPerSec >> 24);
  35. data[12] = (uint8_t)wav->nBlockAlign;
  36. data[13] = (uint8_t)(wav->nBlockAlign >> 8);
  37. data[14] = (uint8_t)wav->wBitsPerSample;
  38. data[15] = (uint8_t)(wav->wBitsPerSample >> 8);
  39. data[16] = (uint8_t)wav->cbSize;
  40. data[17] = (uint8_t)(wav->cbSize >> 8);
  41. //if(wav->cbSize > 0)
  42. // memcpy(data + 18, wav->extra, wav->cbSize);
  43. return wav->cbSize + 18;
  44. }