void collect_Audio_Data(){ printf( "Task PID=%u\r\n",nrk_get_pid()); int8_t fd, val, index; uint16_t buf; // Open ADC device as read fd=nrk_open(FIREFLY_3_SENSOR_BASIC,READ); if(fd==NRK_ERROR) nrk_kprintf(PSTR("Failed to open sensor driver\r\n")); while(1) { val=nrk_set_status(fd,SENSOR_SELECT,AUDIO); //8 KHZ sensor nrk_spin_wait_us(125); val=nrk_read(fd,&buf,2); // printf( " audio=%d\r\n", buf); nrk_led_clr(BLUE_LED); nrk_led_clr(RED_LED); nrk_led_toggle(RED_LED); audio_data[index] = buf; index++; if(index == audio_data_size) { index =0; calculate_rms(audio_data, audio_data_size); // nrk_wait_until_next_period(); } } }
int main(int argc, char *argv[]) { interpret_args(argc, argv); // Open files and set info open_source_file(filename); dest_info.samplerate = 44100; dest_info.channels = 2; dest_info.format = SF_FORMAT_WAV ^ SF_FORMAT_PCM_16 ^ SF_ENDIAN_FILE; open_dest_file(dest_file, &dest_info); if (verbose) { logger(NOTICE, "Sample rate: %d", source_info.samplerate); logger(NOTICE, "Frames: %d", (int) source_info.frames); logger(NOTICE, "Channels: %d", source_info.channels); logger(NOTICE, "%s minutes long.", prettify_seconds(source_info.frames/(double) source_info.samplerate, 0)); } // Calculate Root-Mean-Square of source sound FRAMES_IN_RMS_FRAME = (source_info.samplerate*RMS_FRAME_DURATION)/1000; // (44100/1000)*20 RMS_FRAME_COUNT = ceil(source_info.frames/(float) FRAMES_IN_RMS_FRAME); float *rms = malloc(2*RMS_FRAME_COUNT*sizeof(float)); calculate_rms(rms); if (verbose) { logger(NOTICE, "Calculated RMS!"); } // Calculate RMS-derived features of long (1 second) frames FRAMES_IN_LONG_FRAME = (source_info.samplerate*LONG_FRAME_DURATION)/1000; // (44100/1000)*20 LONG_FRAME_COUNT = ceil(source_info.frames/(float) FRAMES_IN_LONG_FRAME); RMS_FRAMES_IN_LONG_FRAME = LONG_FRAME_DURATION/RMS_FRAME_DURATION; float *mean_rms = malloc(2*LONG_FRAME_COUNT*sizeof(float)); float *variance_rms = malloc(2*LONG_FRAME_COUNT*sizeof(float)); float *norm_variance_rms = malloc(2*LONG_FRAME_COUNT*sizeof(float)); float *mler = malloc(2*LONG_FRAME_COUNT*sizeof(float)); calculate_features(rms, mean_rms, variance_rms, norm_variance_rms, mler); logger(NOTICE, "Calculated features!"); // CLASSIFY bool *is_music = malloc(2*LONG_FRAME_COUNT*sizeof(bool)); // Decide whether a given second segment is music or speech, // based on the MLER value and the Upper Music Threshold classify_segments(is_music, mler); // The music-ness of the frame equals the average music-ness of itself and // the two previous and next frames average_musicness(is_music); // Merge smaller segments with larger segments segment *merged_segments = malloc(LONG_FRAME_COUNT*sizeof(segment)); int merged_segment_count = merge_segments(is_music, merged_segments); // Finally, we write only the speech sections to the destination file write_speech_to_file(merged_segments, merged_segment_count); // Close files and free memory bool file_close_success = finalize_files(); logger(WARNING, "Successfully generated %s!", output_path); free(mean_rms); free(variance_rms); free(norm_variance_rms); free(mler); free(merged_segments); free(is_music); free(rms); return !file_close_success; }