PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
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.

play_serialflash_raw.cpp 3.1KB

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