wav.hpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #pragma once
  2. namespace nall::Encode {
  3. struct WAV {
  4. static auto stereo_16bit(const string& filename, array_view<int16_t> left, array_view<int16_t> right, uint frequency) -> bool {
  5. if(left.size() != right.size()) return false;
  6. static uint channels = 2;
  7. static uint bits = 16;
  8. static uint samples = left.size();
  9. file_buffer fp;
  10. if(!fp.open(filename, file::mode::write)) return false;
  11. fp.write('R');
  12. fp.write('I');
  13. fp.write('F');
  14. fp.write('F');
  15. fp.writel(4 + (8 + 16) + (8 + samples * 4), 4);
  16. fp.write('W');
  17. fp.write('A');
  18. fp.write('V');
  19. fp.write('E');
  20. fp.write('f');
  21. fp.write('m');
  22. fp.write('t');
  23. fp.write(' ');
  24. fp.writel(16, 4);
  25. fp.writel(1, 2);
  26. fp.writel(channels, 2);
  27. fp.writel(frequency, 4);
  28. fp.writel(frequency * channels * bits, 4);
  29. fp.writel(channels * bits, 2);
  30. fp.writel(bits, 2);
  31. fp.write('d');
  32. fp.write('a');
  33. fp.write('t');
  34. fp.write('a');
  35. fp.writel(samples * 4, 4);
  36. for(uint sample : range(samples)) {
  37. fp.writel(left[sample], 2);
  38. fp.writel(right[sample], 2);
  39. }
  40. return true;
  41. }
  42. };
  43. }