Guest User

Untitled

a guest
Aug 17th, 2025
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. #include <FS.h>
  2. #include <SD.h>
  3. #include <SPI.h>
  4.  
  5. #define MIC_PIN 1 // ADC input pin (besser A1 statt 1)
  6. #define BUTTON_PIN 0 // Boot button
  7. #define LED_PIN 38 // Status LED
  8. #define SD_CS 10 // SD card Chip Select
  9.  
  10. File audioFile;
  11. bool isRecording = false;
  12. bool buttonPressed = false;
  13.  
  14. const int sampleRate = 22050; // Sample rate in Hz
  15. unsigned long lastSampleTime = 0;
  16. uint32_t audioDataBytes = 0;
  17. int recordingCount = 1;
  18.  
  19. // Audio processing variables
  20. int16_t dcOffset = 2048; // DC bias removal (12-bit ADC center)
  21. int16_t lastSample = 0; // For simple low-pass filtering
  22.  
  23. // Create WAV file header
  24. void createWavHeader(File &file, uint32_t sampleRate, uint16_t bitsPerSample, uint16_t channels, uint32_t dataSize) {
  25. file.seek(0);
  26.  
  27. // Main RIFF chunk
  28. file.write((const uint8_t*)"RIFF", 4);
  29. uint32_t totalSize = 36 + dataSize;
  30. file.write((uint8_t *)&totalSize, 4);
  31. file.write((const uint8_t*)"WAVE", 4);
  32.  
  33. // Format chunk
  34. file.write((const uint8_t*)"fmt ", 4);
  35. uint32_t fmtChunkSize = 16;
  36. file.write((uint8_t *)&fmtChunkSize, 4);
  37. uint16_t audioFormat = 1; // PCM
  38. file.write((uint8_t *)&audioFormat, 2);
  39. file.write((uint8_t *)&channels, 2);
  40. file.write((uint8_t *)&sampleRate, 4);
  41. uint32_t byteRate = sampleRate * channels * bitsPerSample / 8;
  42. file.write((uint8_t *)&byteRate, 4);
  43. uint16_t blockAlign = channels * bitsPerSample / 8;
  44. file.write((uint8_t *)&blockAlign, 2);
  45. file.write((uint8_t *)&bitsPerSample, 2);
  46.  
  47. // Data chunk header
  48. file.write((const uint8_t*)"data", 4);
  49. file.write((uint8_t *)&dataSize, 4);
  50. }
  51.  
  52. void setup() {
  53. Serial.begin(115200);
  54. pinMode(BUTTON_PIN, INPUT_PULLUP);
  55. pinMode(LED_PIN, OUTPUT);
  56. digitalWrite(LED_PIN, LOW);
  57.  
  58. delay(100);
  59.  
  60. if (!SD.begin(SD_CS)) {
  61. Serial.println("Oops! Can't find the SD card. Please check the connection.");
  62. while (1) {
  63. digitalWrite(LED_PIN, HIGH);
  64. delay(200);
  65. digitalWrite(LED_PIN, LOW);
  66. delay(200);
  67. }
  68. }
  69.  
  70. Serial.println("🎤 Audio Recorder Ready!");
  71. Serial.println("Press the button to start recording, press again to stop.");
  72.  
  73. digitalWrite(LED_PIN, HIGH);
  74. delay(100);
  75. digitalWrite(LED_PIN, LOW);
  76. }
  77.  
  78. void loop() {
  79. handleButtonPress();
  80.  
  81. if (isRecording) {
  82. recordAudioSample();
  83. }
  84. }
  85.  
  86. void handleButtonPress() {
  87. bool currentButtonState = (digitalRead(BUTTON_PIN) == LOW);
  88.  
  89. if (currentButtonState && !buttonPressed) {
  90. buttonPressed = true;
  91. delay(30); // debounce
  92.  
  93. if (!isRecording) {
  94. beginRecording();
  95. } else {
  96. finishRecording();
  97. }
  98. } else if (!currentButtonState) {
  99. buttonPressed = false;
  100. }
  101. }
  102.  
  103. void recordAudioSample() {
  104. unsigned long currentTime = micros();
  105.  
  106. if (currentTime - lastSampleTime >= (1000000UL / sampleRate)) {
  107. lastSampleTime = currentTime;
  108.  
  109. int rawSample = analogRead(MIC_PIN);
  110.  
  111. int16_t centeredSample = rawSample - dcOffset;
  112.  
  113. // Sanfterer Lowpass Filter fĂźr ausgewogenen Klang
  114. centeredSample = (centeredSample * 5 + lastSample * 3) / 8;
  115. lastSample = centeredSample;
  116.  
  117. // Verstärkung anpassen
  118. int16_t processedSample = centeredSample * 6;
  119.  
  120. // Soft limiting
  121. if (processedSample > 16000) processedSample = 16000;
  122. if (processedSample < -16000) processedSample = -16000;
  123.  
  124. audioFile.write((uint8_t)(processedSample & 0xFF));
  125. audioFile.write((uint8_t)(processedSample >> 8));
  126. audioDataBytes += 2;
  127. }
  128. }
  129.  
  130. void beginRecording() {
  131. char fileName[30];
  132. snprintf(fileName, sizeof(fileName), "/audio_clip_%03d.wav", recordingCount);
  133.  
  134. audioFile = SD.open(fileName, FILE_WRITE);
  135. if (!audioFile) {
  136. Serial.println("Sorry, couldn't create the audio file. Please try again.");
  137. return;
  138. }
  139.  
  140. lastSample = 0;
  141.  
  142. // DC Offset Kalibrierung
  143. int32_t offsetSum = 0;
  144. for (int i = 0; i < 100; i++) {
  145. offsetSum += analogRead(MIC_PIN);
  146. delayMicroseconds(100);
  147. }
  148. dcOffset = offsetSum / 100;
  149.  
  150. // Platzhalter WAV-Header schreiben
  151. for (int i = 0; i < 44; i++) {
  152. audioFile.write((uint8_t)0);
  153. }
  154.  
  155. audioDataBytes = 0;
  156. recordingCount++;
  157. Serial.println("🔴 Recording started: " + String(fileName));
  158. Serial.println("DC offset calibrated to: " + String(dcOffset));
  159. digitalWrite(LED_PIN, HIGH);
  160. isRecording = true;
  161. }
  162.  
  163. void finishRecording() {
  164. isRecording = false;
  165.  
  166. createWavHeader(audioFile, sampleRate, 16, 1, audioDataBytes);
  167.  
  168. audioFile.close();
  169.  
  170. Serial.println("⏚ Recording stopped.");
  171. Serial.println("File saved successfully!");
  172. digitalWrite(LED_PIN, LOW);
  173. }
Advertisement
Add Comment
Please, Sign In to add comment