view wave.c @ 2329:06d5e9b08bdb

Add NTSC composite shader by Sik
author Michael Pavone <pavone@retrodev.com>
date Wed, 23 Aug 2023 21:38:39 -0700
parents efc75ea79164
children
line wrap: on
line source

/*
 Copyright 2013 Michael Pavone
 This file is part of BlastEm.
 BlastEm is free software distributed under the terms of the GNU General Public License version 3 or greater. See COPYING for full license text.
*/
#include "wave.h"
#include <stddef.h>
#include <string.h>

int wave_init(FILE * f, uint32_t sample_rate, uint16_t bits_per_sample, uint16_t num_channels)
{
	wave_header header;
	memcpy(header.chunk.id, "RIFF", 4);
	memcpy(header.chunk.format, "WAVE", 4);
	header.chunk.size = 0; //This will be filled in later
	memcpy(header.format_header.id, "fmt ", 4);
	header.format_header.size = sizeof(wave_header) - (sizeof(header.chunk) + sizeof(header.data_header) + sizeof(header.format_header));
	header.audio_format = 1;
	header.num_channels = num_channels;
	header.sample_rate = sample_rate;
	header.byte_rate = sample_rate * num_channels * (bits_per_sample/8);
	header.block_align = num_channels * (bits_per_sample/8);
	header.bits_per_sample = bits_per_sample;
	memcpy(header.data_header.id, "data", 4);
	header.data_header.size = 0;//This will be filled in later;
	return fwrite(&header, 1, sizeof(header), f) == sizeof(header);
}

uint8_t wave_read_header(FILE *f, wave_header *header)
{
	size_t initial_read = sizeof(*header) - sizeof(header->data_header);
	if (fread(header, 1, initial_read, f) != initial_read) {
		return 0;
	}
	if (memcmp(header->chunk.id, "RIFF", 4)) {
		return 0;
	}
	if (memcmp(header->chunk.format, "WAVE", 4)) {
		return 0;
	}
	if (header->chunk.size < sizeof(*header) - 8) {
		return 0;
	}
	if (memcmp(header->format_header.id, "fmt ", 4)) {
		return 0;
	}
	if (header->format_header.size < sizeof(*header) - sizeof(header->chunk) - sizeof(header->format_header) - sizeof(header->data_header)) {
		return 0;
	}
	fseek(f, header->format_header.size + sizeof(header->chunk) + sizeof(header->format_header), SEEK_SET);
	if (fread(&header->data_header, 1, sizeof(header->data_header), f) != sizeof(header->data_header)) {
		return 0;
	}
	if (memcmp(header->data_header.id, "data", 4)) {
		return 0;
	}
	return 1;
}

int wave_finalize(FILE * f)
{
	uint32_t size = ftell(f);
	fseek(f, offsetof(wave_header, chunk.size), SEEK_SET);
	size -= 8;
	if (fwrite(&size, sizeof(size), 1, f) != 1) {
		fclose(f);
		return 0;
	}
	fseek(f, offsetof(wave_header, data_header.size), SEEK_SET);
	size -= 36;
	if (fwrite(&size, sizeof(size), 1, f) != 1) {
		fclose(f);
		return 0;
	}
	fclose(f);
	return 1;
}