Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Arduino SD library; #9

Open
7doctor7 opened this issue Jun 26, 2021 · 2 comments
Open

Using Arduino SD library; #9

7doctor7 opened this issue Jun 26, 2021 · 2 comments

Comments

@7doctor7
Copy link

Hi!
Maybe you can provide some help for using the Arduino-provided SD library for saving WAV files to SD card?
Because now I can't do it with stdio.h in yours WAVFileWriter and Arduino SD and FS libs.
Thank in advance.

@beustens
Copy link

First, change the contents of the following files:

WAVFileWriter.h

#pragma once

#include "FS.h"
#include "WAVFile.h"

class WAVFileWriter
{
private:
  int m_file_size;

  File m_fp;
  wav_header_t m_header;

public:
  WAVFileWriter(File fp, int sample_rate);
  void start();
  void write(int16_t *samples, int count);
  void finish();
};

WAVFileWriter.cpp

#include "WAVFileWriter.h"

static const char *TAG = "WAV";

WAVFileWriter::WAVFileWriter(File fp, int sample_rate)
{
  m_fp = fp;
  m_header.sample_rate = sample_rate;
  // write out the header - we'll fill in some of the blanks later
  m_fp.write((uint8_t *) &m_header, sizeof(m_header));
  m_file_size = sizeof(wav_header_t);
}

void WAVFileWriter::write(int16_t *samples, int count)
{
  // write the samples and keep track of the file size so far
  uint8_t* sampleBytes = (uint8_t*)samples;
  m_fp.write(sampleBytes, sizeof(int16_t) * count);
  m_file_size += sizeof(int16_t) * count;
}

void WAVFileWriter::finish()
{
  // now fill in the header with the correct information and write it again
  m_header.data_bytes = m_file_size - sizeof(wav_header_t);
  m_header.wav_size = m_file_size - 8;
  m_fp.seek(0);
  m_fp.write((uint8_t *) &m_header, sizeof(m_header));
}

Then, in main.cpp, you can change the record function:

#include "SD.h"
#include "FS.h"
#include <WAVFileWriter.h>

#define MIC_BUFFER_LEN 1024
#define MIC_SAMPLERATE 16000

void record(const char *fname) {
	int16_t *samples = (int16_t *)malloc(sizeof(int16_t) * MIC_BUFFER_LEN);
	startMic();
	// open the file on the sdcard
	File fp = SD.open(fname, FILE_WRITE);
	// create wav file writer
	WAVFileWriter *writer = new WAVFileWriter(fp, MIC_SAMPLERATE);
	// keep writing until the user releases the button
	while (gpio_get_level(BTN_REC) == 1) {
		int samples_read = readMic(samples, MIC_BUFFER_LEN);
		writer->write(samples, samples_read);
	}
	// stop the input
	stopMic();
	// and finish the writing
	writer->finish();
	fp.close();
	delete writer;
	free(samples);
}

@cgreening
Copy link
Contributor

@beustens I know this is an old issue - but if you want to turn your changes into a PR I'd be happy to merge them in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants