Components and supplies
MAX4466 microphone
Arduino® UNO R4 WiFi
Apps and platforms
Arduino IDE 1.8.19
NanoEdge AI Studio
Project description
Code
Code_with_matrix_display
c
The final code
1/* Libraries ----------------------------------------------------------*/ 2#include "ArduinoGraphics.h" 3#include "Arduino_LED_Matrix.h" 4#include "NanoEdgeAI.h" 5#include "knowledge.h" 6 7/* Defines ----------------------------------------------------------*/ 8#define SENSOR_SAMPLES 1024 //buffer size 9#define AXIS 1 //microphone is 1 axis 10#define DOWNSAMPLE 32 //microphone as a very high data rate, we downsample it 11 12 13/* Prototypes ----------------------------------------------------------*/ 14void get_microphone_data(); //function to collect buffer of sound 15 16/* Global variables ----------------------------------------------------------*/ 17static uint16_t neai_ptr = 0; //pointers to fill for sound buffer 18static float neai_buffer[SENSOR_SAMPLES * AXIS] = {0.0}; //souhnd buffer 19int const AMP_PIN = A0; // Preamp output pin connected to A0 20 21/* NEAI PART*/ 22uint8_t neai_code = 0; //initialization code 23uint16_t id_class = 0; // Point to id class (see argument of neai_classification fct) 24float output_class_buffer[CLASS_NUMBER]; // Buffer of class probabilities 25const char *id2class[CLASS_NUMBER + 1] = { // Buffer for mapping class id to class name 26 "unknown", 27 "up", 28 "down", 29}; 30 31/* Declare matrix to display */ 32byte frame[8][12] = { 33 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 34 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 35 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 36 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 37 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 38 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 39 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 40 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } 41}; 42 43char text[30]; 44 45 46/* Objects ----------------------------------------------------------*/ 47ArduinoLEDMatrix matrix; 48 49 50/* Setup function ----------------------------------------------------------*/ 51void setup() { 52 Serial.begin(115200); 53 delay(10); 54 matrix.begin(); 55 56 /* Initialize NanoEdgeAI AI */ 57 neai_code = neai_classification_init(knowledge); 58 if (neai_code != NEAI_OK) { 59 Serial.print("Not supported board.\n"); 60 } 61} 62 63/* Infinite loop ----------------------------------------------------------*/ 64void loop() { 65 get_microphone_data(); 66 neai_classification(neai_buffer, output_class_buffer, &id_class); 67 /* DISPLAY THE SONG NAME */ 68 switch(id_class){ 69 case 1: 70 strcpy (text, " song1 "); 71 break; 72 case 2: 73 strcpy (text, " song2 "); 74 break; 75 default: 76 strcpy (text, " check switch in code "); 77 break; 78 } 79 80 Serial.println(id_class); 81 matrix.beginDraw(); 82 matrix.stroke(0xFFFFFFFF); 83 matrix.textScrollSpeed(50); 84 matrix.textFont(Font_5x7); 85 matrix.beginText(0, 1, 0xFFFFFF); 86 matrix.println(text); 87 matrix.endText(SCROLL_LEFT); 88 matrix.endDraw(); 89} 90 91 92/* Functions declaration ----------------------------------------------------------*/ 93void get_microphone_data() 94{ 95 static uint16_t temp = 0; //stock values 96 int sub = 0; //increment to downsample 97 //while the buffer is not full 98 while (neai_ptr < SENSOR_SAMPLES) { 99 //we only get a value every DOWNSAMPLE (32 in this case) 100 if (sub > DOWNSAMPLE) { 101 /* Fill neai buffer with new accel data */ 102 neai_buffer[neai_ptr] = analogRead(AMP_PIN); 103 /* Increment neai pointer */ 104 neai_ptr++; 105 sub = 0; //reset increment 106 } 107 else { 108 //we read the sample even if we don't use it 109 //else it is instantaneous and we don't downsample 110 temp = analogRead(AMP_PIN); 111 } 112 sub ++; 113 } 114 neai_ptr = 0; //reset the beginning position 115}
Downloadable files
Code_with_matrix_display
The final code
arduino_demo_shazam_matrix.ino
Comments
Only logged in users can leave comments