File: Mac_Wave.h

package info (click to toggle)
maelstrom 3.0.7-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,952 kB
  • sloc: cpp: 10,947; sh: 3,406; ansic: 2,781; makefile: 175
file content (112 lines) | stat: -rw-r--r-- 2,748 bytes parent folder | download | duplicates (2)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
    MACLIB:  A companion library to SDL for working with Macintosh (tm) data
    Copyright (C) 1997-2021 Sam Lantinga <slouken@libsdl.org>

    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 of the License, 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 this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

/* A WAVE class that can load itself from WAVE files or Mac 'snd ' resources */

#include <stdio.h>
#include <stdarg.h>
#include "SDL_audio.h"
#include "Mac_Resource.h"

class Wave {

public:
	Wave() {
		Init();
	}
	Wave(const char *wavefile, Uint16 desired_rate = 0) {
		Init();
		Load(wavefile, desired_rate);
	}
	Wave(Mac_ResData *snd, Uint16 desired_rate = 0) {
		Init();
		Load(snd, desired_rate);
	}
	~Wave() {
		Free();
	}

	/* Load WAVE resources, converting to the desired sample rate */
	int Load(const char *wavefile, Uint16 desired_rate = 0);
	int Load(Mac_ResData *snd, Uint16 desired_rate = 0);
	int Save(const char *wavefile);

	void Rewind(void) {
		soundptr = sound_data;
		soundlen = sound_datalen;
	}
	void Forward(Uint32 distance) {
		soundlen -= distance;
		soundptr += distance;
	}
	Uint32 DataLeft(void) {
		return(soundlen > 0 ? soundlen : 0);
	}
	Uint8 *Data(void) {
		if ( soundlen > 0 )
			return(soundptr);
		return(NULL);
	}
	SDL_AudioSpec *Spec(void) {
		return(&spec);
	}
	Uint32 Frequency(Uint16 desired_rate = 0);
	Uint16 SampleSize(void) {
		return(((spec.format&0xFF)/8)*spec.channels);
	}
	int BitsPerSample(void) {
		return(spec.format&0xFF);
	}
	int Stereo(void) {
		return(spec.channels/2);
	}

	char *Error(void) {
		return(errstr);
	}

private:
	void Init(void);
	void Free(void);

	/* The SDL-ready audio specification */
	SDL_AudioSpec spec;
	Uint8 *sound_data;
	Uint32 sound_datalen;

	/* Current position of the WAVE file */
	Uint8 *soundptr;
	Sint32 soundlen;

	/* Utility functions */
	Uint32 ConvertRate(Uint16 rate_in, Uint16 rate_out, 
			Uint8 **samples, Uint32 n_samples, Uint8 s_size);
	
	/* Useful for getting error feedback */
	void error(const char *fmt, ...) {
		va_list ap;

		va_start(ap, fmt);
		SDL_vsnprintf(errbuf, sizeof(errbuf), fmt, ap);
		va_end(ap);
		errstr = errbuf;
	}
	char *errstr;
	char  errbuf[BUFSIZ];
};