PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

142 linhas
4.3KB

  1. /* OctoWS2811 addaudio.c - Merge image and sound data to VIDEO.BIN
  2. for Teensy 3.1 running OctoWS2811 VideoSDcard.ino
  3. http://www.pjrc.com/teensy/td_libs_OctoWS2811.html
  4. Copyright (c) 2014 Paul Stoffregen, PJRC.COM, LLC
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. */
  21. // Basis Usage:
  22. //
  23. // First, run movie2sdcard.pde with Processing....
  24. //
  25. // Merge "myvideo.bin" image data, created by movie2sdcard.pde,
  26. // and "myvideo.raw" audio data, created by ffmpeg & sox,
  27. // to create "VIDEO.BIN" which can be played by VideoSDcard.ino
  28. //
  29. // Run with these commands:
  30. //
  31. // ./ffmpeg -i myvideo.mov -vn -f wav myvideo.wav
  32. // ./sox myvideo.wav -c 1 -b 16 -r 44117.647 myvideo.raw
  33. // ./addaudio
  34. //
  35. // Then copy VIDEO.BIN to a Micro SD card and play it on your LEDs :-)
  36. //
  37. // Compile with:
  38. //
  39. // gcc -O2 -Wall -o addaudio addaudio.c
  40. #define VIDEO_INFILE "myvideo.bin"
  41. #define AUDIO_INFILE "myvideo.raw"
  42. #define OUTFILE "VIDEO.BIN"
  43. #define SAMPLE_RATE 44117.64706
  44. #include <stdio.h>
  45. #include <stdint.h>
  46. #include <stdarg.h>
  47. #include <stdlib.h>
  48. void die(const char *format, ...) __attribute__ ((format (printf, 1, 2)));
  49. int main()
  50. {
  51. FILE *fpa, *fpv, *fout;
  52. unsigned char header[5], header2[5];
  53. unsigned char vbuf[65536], abuf[65536];
  54. unsigned int usec;
  55. uint64_t elapsed_usec=0LL;
  56. //double elapsed_usec=0.0;
  57. unsigned int target_samples, elapsed_samples=0;
  58. unsigned int samples;
  59. size_t n, size;
  60. fpv = fopen(VIDEO_INFILE, "rb");
  61. if (!fpv) die("unable to read %s\n", VIDEO_INFILE);
  62. fpa = fopen(AUDIO_INFILE, "rb");
  63. if (!fpa) die("unable to read %s\n", AUDIO_INFILE);
  64. fout = fopen(OUTFILE, "wb");
  65. if (!fout) die("unable to write %s\n", OUTFILE);
  66. while (!feof(fpv)) {
  67. n = fread(header, 1, 5, fpv);
  68. if (n != 5) {
  69. if (feof(fpv)) break;
  70. die("unable to read video header");
  71. }
  72. if (header[0] == '*') {
  73. size = (header[1] | (header[2] << 8)) * 3;
  74. usec = header[3] | (header[4] << 8);
  75. if (size > sizeof(vbuf)) die("vbuf not big enough");
  76. n = fread(vbuf, 1, size, fpv);
  77. if (n != size) die("unable to read video data");
  78. printf("v: %u %u", (int)size, usec);
  79. if (usec == 0xFFFF) die("opps");
  80. elapsed_usec += usec;
  81. target_samples = ((uint64_t)elapsed_usec *
  82. (uint64_t)(SAMPLE_RATE * 1000.0) / 1000000000LL);
  83. //target_samples = elapsed_usec / 1e6 * SAMPLE_RATE;
  84. target_samples += 2560; // try to always buffer 2.5K
  85. samples = (target_samples - elapsed_samples + 127) / 128 * 128;
  86. elapsed_samples += samples;
  87. printf(", s=%u, n=%u", target_samples, samples);
  88. n = fread(abuf, 1, samples*2, fpa);
  89. if (n < samples*2) {
  90. printf(", n(audio) = %d", (int)n);
  91. n &= ~1;
  92. }
  93. header2[0] = '%';
  94. header2[1] = samples & 255;
  95. header2[2] = samples >> 8;
  96. header2[3] = 0;
  97. header2[4] = 0;
  98. fwrite(header2, 1, 5, fout);
  99. fwrite(abuf, 2, samples, fout);
  100. fwrite(header, 1, 5, fout);
  101. fwrite(vbuf, 1, size, fout);
  102. } else if (header[0] == '%') {
  103. // skip audio, if input file has any
  104. size = (header[1] | (header[2] << 8)) * 3;
  105. n = fread(vbuf, 1, size, fpv);
  106. if (n != size) die("unable to read video data");
  107. } else {
  108. die("unknown header type");
  109. }
  110. printf("\n");
  111. }
  112. return 0;
  113. }
  114. void die(const char *format, ...)
  115. {
  116. va_list args;
  117. va_start(args, format);
  118. fprintf(stderr, "addaudio: ");
  119. vfprintf(stderr, format, args);
  120. fprintf(stderr, "\n");
  121. exit(1);
  122. }