view wave.c @ 922:913a6336ce20

Shift slot number to slot behavior mapping by six slots in H40 mode. This makes the line change slot align with the point at which the display turns on and off at the end of the active display area. Also fixed a regression in which an external slot got accidentally changed into a sprite draw slot
author Michael Pavone <pavone@retrodev.com>
date Thu, 28 Jan 2016 09:10:14 -0800
parents 6e9d1a8c1b08
children efc75ea79164
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);
}

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;
}