summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoursoir <chat@joursoir.net>2021-01-26 21:15:43 +0000
committerJoursoir <chat@joursoir.net>2021-01-26 21:15:43 +0000
commit075693faeaa6420de653ad6e81c2d708e354b314 (patch)
tree00f360abf127f84d48d7a2ba33ee013845276538
parentc07ea4108bf3c1b55c7f454c86a4b72e0c0f8bc6 (diff)
downloadaudio-tools-075693faeaa6420de653ad6e81c2d708e354b314.tar.gz
audio-tools-075693faeaa6420de653ad6e81c2d708e354b314.tar.bz2
audio-tools-075693faeaa6420de653ad6e81c2d708e354b314.zip
add code for audio types, support record in wave format
Take out code with audio types to audio_types.c
-rw-r--r--parecord/audio_types.c46
-rw-r--r--parecord/audio_types.h23
-rw-r--r--parecord/wav_header.h21
3 files changed, 90 insertions, 0 deletions
diff --git a/parecord/audio_types.c b/parecord/audio_types.c
new file mode 100644
index 0000000..59652e0
--- /dev/null
+++ b/parecord/audio_types.c
@@ -0,0 +1,46 @@
+#include <string.h>
+
+#include "audio_types.h"
+
+static const struct audio_format support_formats[] = {
+ {"wav", 44}, // AUDIO_FORMAT_WAVE = 0
+ {NULL, 0}
+};
+
+struct wav_header *init_wav_header(struct wav_header *header,
+ uint32_t size, uint32_t subchunk1Size, uint16_t audioFormat,
+ uint16_t numChannels, uint32_t sampleRate, uint32_t bitsPerSample)
+{
+ header->chunkId = 0x46464952; // = "RIFF"
+ header->chunkSize = size - 8;
+ header->format = 0x45564157; // = "WAVE"
+ header->subchunk1Id = 0x20746d66; // = "fmt "
+ header->subchunk1Size = subchunk1Size;
+ header->audioFormat = audioFormat;
+ header->numChannels = numChannels;
+ header->sampleRate = sampleRate;
+ header->byteRate = sampleRate * numChannels * (bitsPerSample/8);
+ header->blockAlign = numChannels * (bitsPerSample/8);
+ header->bitsPerSample = bitsPerSample;
+ header->subchunk2Id = 0x61746164; // = "data"
+ header->subchunk2Size = size - 44;
+
+ return header;
+}
+
+int checkAudioFormat(char *source)
+{
+ int i;
+ for(i = 0; support_formats[i].name != NULL; i++) {
+ if(strcmp(source, support_formats[i].name) == 0) {
+ return i;
+ }
+ }
+
+ return AUDIO_FORMAT_NONE;
+}
+
+off_t getOffset(int format)
+{
+ return support_formats[format].rsv_bytes;
+} \ No newline at end of file
diff --git a/parecord/audio_types.h b/parecord/audio_types.h
new file mode 100644
index 0000000..90a5fc6
--- /dev/null
+++ b/parecord/audio_types.h
@@ -0,0 +1,23 @@
+#ifndef PAR_AUDIOTYPES_H
+#define PAR_AUDIOTYPES_H
+
+#include <stdint.h>
+#include <sys/types.h>
+
+#include "wav_header.h"
+
+#define AUDIO_FORMAT_NONE -1
+#define AUDIO_FORMAT_WAVE 0
+
+struct audio_format {
+ const char *name;
+ off_t rsv_bytes;
+};
+
+struct wav_header *init_wav_header(struct wav_header *header,
+ uint32_t size, uint32_t subchunk1Size, uint16_t audioFormat,
+ uint16_t numChannels, uint32_t sampleRate, uint32_t bitsPerSample);
+int checkAudioFormat(char *source);
+off_t getOffset(int format);
+
+#endif /* AT_AUDIOTYPES_H */
diff --git a/parecord/wav_header.h b/parecord/wav_header.h
new file mode 100644
index 0000000..eaf862b
--- /dev/null
+++ b/parecord/wav_header.h
@@ -0,0 +1,21 @@
+#ifndef WAV_HEADER_H
+#define WAV_HEADER_H
+
+struct wav_header
+{
+ uint32_t chunkId;
+ uint32_t chunkSize;
+ uint32_t format;
+ uint32_t subchunk1Id;
+ uint32_t subchunk1Size;
+ uint16_t audioFormat;
+ uint16_t numChannels;
+ uint32_t sampleRate;
+ uint32_t byteRate;
+ uint16_t blockAlign;
+ uint16_t bitsPerSample;
+ uint32_t subchunk2Id;
+ uint32_t subchunk2Size;
+};
+
+#endif /* WAV_HEADER_H */