kws.ino 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: kws.ino
  4. * Description: Very simple Yes detector
  5. *
  6. * $Date: 16 March 2022
  7. *
  8. * Target Processor: Cortex-M and Cortex-A cores
  9. * -------------------------------------------------------------------- */
  10. /*
  11. * Copyright (C) 2010-2022 ARM Limited or its affiliates. All rights reserved.
  12. *
  13. * SPDX-License-Identifier: Apache-2.0
  14. *
  15. * Licensed under the Apache License, Version 2.0 (the License); you may
  16. * not use this file except in compliance with the License.
  17. * You may obtain a copy of the License at
  18. *
  19. * www.apache.org/licenses/LICENSE-2.0
  20. *
  21. * Unless required by applicable law or agreed to in writing, software
  22. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  23. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  24. * See the License for the specific language governing permissions and
  25. * limitations under the License.
  26. */
  27. #include <PDM.h>
  28. #include "arm_math.h"
  29. #include "coef.h"
  30. #include "scheduler.h"
  31. // default number of output channels
  32. static const char channels = 1;
  33. // default PCM output frequency
  34. static const int frequency = 16000;
  35. // Buffer to read samples into, each sample is 16-bits
  36. short sampleBuffer[AUDIOBUFFER_LENGTH];
  37. // Number of audio samples read
  38. volatile int samplesRead=0;
  39. void setup() {
  40. Serial.begin(9600);
  41. while (!Serial);
  42. Serial.println("Starting ...");
  43. // Configure the data receive callback
  44. PDM.onReceive(onPDMdata);
  45. // Optionally set the gain
  46. // Defaults to 20 on the BLE Sense and 24 on the Portenta Vision Shield
  47. // PDM.setGain(30);
  48. // Initialize PDM with:
  49. // - one channel (mono mode)
  50. // - a 16 kHz sample rate for the Arduino Nano 33 BLE Sense
  51. // - a 32 kHz or 64 kHz sample rate for the Arduino Portenta Vision Shield
  52. if (!PDM.begin(channels, frequency)) {
  53. Serial.println("Failed to start PDM!");
  54. while (1);
  55. }
  56. }
  57. void loop() {
  58. int error;
  59. uint32_t nb;
  60. // Start the CMSIS-DSP generated synchronous scheduling
  61. Serial.println("Scheduling ...");
  62. nb = scheduler(&error,window,coef_q15,coef_shift,intercept_q15,intercept_shift);
  63. Serial.println("End of scheduling");
  64. Serial.print("Error status = ");
  65. Serial.println(error);
  66. Serial.print("Nb iterations");
  67. Serial.println(nb);
  68. }
  69. /**
  70. * Callback function to process the data from the PDM microphone.
  71. * NOTE: This callback is executed as part of an ISR.
  72. * Therefore using `Serial` to print messages inside this function isn't supported.
  73. * */
  74. void onPDMdata() {
  75. // Query the number of available bytes
  76. int bytesAvailable = PDM.available();
  77. // Get free remaining bytes in the buffer
  78. // If real time is respected, there should never be
  79. // any overflow.
  80. int remainingFreeBytes = AUDIOBUFFER_LENGTH*2 - samplesRead*2;
  81. if (remainingFreeBytes >= bytesAvailable)
  82. {
  83. // Read into the sample buffer
  84. int nbReadBytes = PDM.read(sampleBuffer+samplesRead, bytesAvailable);
  85. // 16-bit, 2 bytes per sample
  86. samplesRead += nbReadBytes / 2;
  87. }
  88. else if (remainingFreeBytes > 0)
  89. {
  90. // Read into the sample buffer
  91. int nbReadBytes = PDM.read(sampleBuffer+samplesRead, remainingFreeBytes);
  92. // 16-bit, 2 bytes per sample
  93. samplesRead += nbReadBytes / 2;
  94. }
  95. }