PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

3 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* Audio Library for Teensy 3.X
  2. * Copyright (c) 2014, Paul Stoffregen, paul@pjrc.com
  3. *
  4. * Development of this audio library was funded by PJRC.COM, LLC by sales of
  5. * Teensy and Audio Adaptor boards. Please support PJRC's efforts to develop
  6. * open source software by purchasing Teensy or other PJRC products.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice, development funding notice, and this permission
  16. * notice shall be included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <Arduino.h>
  27. #include "play_sd_raw.h"
  28. #include "spi_interrupt.h"
  29. void AudioPlaySdRaw::begin(void)
  30. {
  31. playing = false;
  32. file_offset = 0;
  33. file_size = 0;
  34. }
  35. bool AudioPlaySdRaw::play(const char *filename)
  36. {
  37. stop();
  38. #if defined(HAS_KINETIS_SDHC)
  39. if (!(SIM_SCGC3 & SIM_SCGC3_SDHC)) AudioStartUsingSPI();
  40. #else
  41. AudioStartUsingSPI();
  42. #endif
  43. __disable_irq();
  44. rawfile = SD.open(filename);
  45. __enable_irq();
  46. if (!rawfile) {
  47. //Serial.println("unable to open file");
  48. #if defined(HAS_KINETIS_SDHC)
  49. if (!(SIM_SCGC3 & SIM_SCGC3_SDHC)) AudioStopUsingSPI();
  50. #else
  51. AudioStopUsingSPI();
  52. #endif
  53. return false;
  54. }
  55. file_size = rawfile.size();
  56. file_offset = 0;
  57. //Serial.println("able to open file");
  58. playing = true;
  59. return true;
  60. }
  61. void AudioPlaySdRaw::stop(void)
  62. {
  63. __disable_irq();
  64. if (playing) {
  65. playing = false;
  66. __enable_irq();
  67. rawfile.close();
  68. #if defined(HAS_KINETIS_SDHC)
  69. if (!(SIM_SCGC3 & SIM_SCGC3_SDHC)) AudioStopUsingSPI();
  70. #else
  71. AudioStopUsingSPI();
  72. #endif
  73. } else {
  74. __enable_irq();
  75. }
  76. }
  77. void AudioPlaySdRaw::update(void)
  78. {
  79. unsigned int i, n;
  80. audio_block_t *block;
  81. // only update if we're playing
  82. if (!playing) return;
  83. // allocate the audio blocks to transmit
  84. block = allocate();
  85. if (block == NULL) return;
  86. if (rawfile.available()) {
  87. // we can read more data from the file...
  88. n = rawfile.read(block->data, AUDIO_BLOCK_SAMPLES*2);
  89. file_offset += n;
  90. for (i=n/2; i < AUDIO_BLOCK_SAMPLES; i++) {
  91. block->data[i] = 0;
  92. }
  93. transmit(block);
  94. } else {
  95. rawfile.close();
  96. #if defined(HAS_KINETIS_SDHC)
  97. if (!(SIM_SCGC3 & SIM_SCGC3_SDHC)) AudioStopUsingSPI();
  98. #else
  99. AudioStopUsingSPI();
  100. #endif
  101. playing = false;
  102. }
  103. release(block);
  104. }
  105. #define B2M (uint32_t)((double)4294967296000.0 / AUDIO_SAMPLE_RATE_EXACT / 2.0) // 97352592
  106. uint32_t AudioPlaySdRaw::positionMillis(void)
  107. {
  108. return ((uint64_t)file_offset * B2M) >> 32;
  109. }
  110. uint32_t AudioPlaySdRaw::lengthMillis(void)
  111. {
  112. return ((uint64_t)file_size * B2M) >> 32;
  113. }