summaryrefslogtreecommitdiffstats
path: root/parecord/audio_types.c
blob: 59652e049c9a79ce413a1d237af2f8ac7d9ea763 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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;
}