/* * Copyright (C) 2004-2005 Vadim Berezniker * http://www.kryptolus.com * * This Program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * This Program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with GNU Make; see the file COPYING. If not, write to * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. * http://www.gnu.org/copyleft/gpl.html * */ #include "stdafx.h" #include "common.h" #include "sabbu.h" #include "sound.h" static unsigned char kry_peakfile_id[16] = { 0x50, 0x87, 0x87, 0x0f, 0x8a, 0xe0, 0x48, 0x03, 0x8a, 0xd3, 0xf9, 0x27, 0x94, 0xa2, 0x73, 0xee }; static unsigned char kry_peakfile_version = 0; #define FAILIF(val) if(val) { close(fh); return FALSE; } #define WRITE_VAL(var, len) FAILIF(write(fh, var, len) < len) #define READ_VAL(var, len) FAILIF(read(fh, var, len) < len) gboolean kry_peakfile_save(char *file, struct sound_info *sound_info) { #ifdef _WINDOWS int fh = open(file, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY); #else int fh = open(file, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU); #endif if(fh == -1) return FALSE; WRITE_VAL(kry_peakfile_id, 16); WRITE_VAL(&kry_peakfile_version, 1); struct _stat statinfo; FAILIF(stat(sound_info->filename, &statinfo) < 0); WRITE_VAL(&statinfo.st_mtime, 4); WRITE_VAL(&statinfo.st_size, 4); char channels = sound_info->channels; channels = sound_info->channels; WRITE_VAL(&channels, 1); WRITE_VAL(&sound_info->sfinfo.samplerate, 4); WRITE_VAL(&sound_info->length_seconds, 4); WRITE_VAL(&sound_info->vals_per_sec, 4); WRITE_VAL(&sound_info->max_pos, 1); WRITE_VAL(&sound_info->max_neg, 1); for(int i = 0; i < sound_info->channels; i++) { int bytes = sound_info->length_seconds * sound_info->vals_per_sec * 2; unsigned int crc = update_crc(0, sound_info->waveform_data[i], bytes); WRITE_VAL(&crc, 4); WRITE_VAL(sound_info->waveform_data[i], bytes); } return TRUE; } gboolean kry_peakfile_read(char *peakfile, struct sound_info *sfinfo) { #ifdef _WINDOWS int fh = open(peakfile, O_RDONLY | O_BINARY); #else //g_warning("open: %s", peakfile); int fh = open(peakfile, O_RDONLY); #endif if(fh == -1) return FALSE; char buffer[16]; READ_VAL(buffer, 16); FAILIF(memcmp(buffer, &kry_peakfile_id, 16)); READ_VAL(buffer, 1); FAILIF(buffer[0] > kry_peakfile_version); struct _stat stat_info; FAILIF(stat(sfinfo->filename, &stat_info) < 0); long mtime; READ_VAL(&mtime, 4); FAILIF(stat_info.st_mtime != mtime); long msize; READ_VAL(&msize, 4); FAILIF(stat_info.st_size != msize); unsigned char channels; READ_VAL(&channels, 1); FAILIF(channels != sfinfo->sfinfo.channels); long samplerate; READ_VAL(&samplerate, 4); FAILIF(samplerate != sfinfo->sfinfo.samplerate); long length_seconds; READ_VAL(&length_seconds, 4); FAILIF(length_seconds != sfinfo->length_seconds); long vals_per_sec; READ_VAL(&vals_per_sec, 4); FAILIF(vals_per_sec != sfinfo->vals_per_sec); unsigned char max_pos; READ_VAL(&max_pos, 1); unsigned char max_neg; READ_VAL(&max_neg, 1); for(int i = 0; i < channels; i++) { unsigned int crc; READ_VAL(&crc, 4); READ_VAL(sfinfo->waveform_data[i], length_seconds * vals_per_sec * 2); unsigned int crc2 = update_crc(0, sfinfo->waveform_data[i], length_seconds * vals_per_sec * 2); FAILIF(crc != crc2); } sfinfo->channels = channels; sfinfo->length_seconds = length_seconds; sfinfo->max_neg = max_neg; sfinfo->max_pos = max_pos; sfinfo->vals_per_sec = vals_per_sec; return TRUE; }