1/*
2 * Copyright © 2000-2003 Ingo Weinhold <ingo_weinhold@gmx.de>
3 * All rights reserved. Distributed under the terms of the MIT licensce.
4 */
5
6// This file contains the following classes:
7// * SampleBuffer
8// * FloatSampleBuffer
9// * IntSampleBuffer
10// * ShortSampleBuffer
11// * UCharSampleBuffer
12// * CharSampleBuffer
13
14#ifndef SAMPLE_BUFFER_H
15#define SAMPLE_BUFFER_H
16
17#include <SupportDefs.h>
18
19// SampleBuffer
20
21template<int BYTES_PER_SAMPLE>
22class SampleBuffer {
23 protected:
24	typedef	uint8				sample_block_t[BYTES_PER_SAMPLE];
25
26 public:
27	inline						SampleBuffer(void* buffer)
28									: fBuffer((sample_block_t*)buffer) { }
29
30	inline	void				operator+=(int samples) {
31		fBuffer += samples;
32	}
33	inline	void				operator-=(int samples) {
34		fBuffer -= samples;
35	}
36
37	inline	void				operator++(int) {
38		fBuffer++;
39	}
40	inline	void				operator--(int) {
41		fBuffer--;
42	}
43
44	inline	void*				operator+(int samples) {
45		return fBuffer + samples;
46	}
47	inline	void*				operator-(int samples) {
48		return fBuffer + samples;
49	}
50
51 protected:
52			sample_block_t*		fBuffer;
53};
54
55
56// FloatSampleBuffer
57
58template<typename sample_t>
59class FloatSampleBuffer : public SampleBuffer<sizeof(float)> {
60 public:
61	inline						FloatSampleBuffer(void* buffer)
62									: SampleBuffer<sizeof(float)>(buffer) {
63	}
64
65	inline	sample_t			ReadSample() const {
66		return *((float*)fBuffer);
67	}
68
69	inline	void				WriteSample(sample_t value) {
70		*((float*)fBuffer) = value;
71	}
72};
73
74
75// IntSampleBuffer
76
77template<typename sample_t>
78class IntSampleBuffer : public SampleBuffer<sizeof(int)> {
79 public:
80	inline						IntSampleBuffer(void* buffer)
81									: SampleBuffer<sizeof(int)>(buffer) {
82	}
83
84	inline	sample_t			ReadSample() const {
85		return (sample_t)*((int*)fBuffer) / (sample_t)0x7fffffff;
86	}
87
88	inline	void				WriteSample(sample_t value) {
89		*((int*)fBuffer) = int(value * (sample_t)0x7fffffff);
90	}
91};
92
93
94// ShortSampleBuffer
95
96template<typename sample_t>
97class ShortSampleBuffer : public SampleBuffer<sizeof(short)> {
98 public:
99	inline						ShortSampleBuffer(void* buffer)
100									: SampleBuffer<sizeof(short)>(buffer) {
101	}
102
103	inline	sample_t			ReadSample() const {
104		return (sample_t)*((short*)fBuffer) / (sample_t)32767;
105	}
106
107	inline	void				WriteSample(sample_t value) {
108		*((short*)fBuffer) = short(value * (sample_t)32767);
109	}
110};
111
112
113// UCharSampleBuffer
114
115template<typename sample_t>
116class UCharSampleBuffer : public SampleBuffer<sizeof(uchar)> {
117 public:
118	inline						UCharSampleBuffer(void* buffer)
119									: SampleBuffer<sizeof(uchar)>(buffer) {
120	}
121
122	inline	sample_t			ReadSample() const {
123		return ((sample_t)*((uchar*)fBuffer) - 128) / (sample_t)127;
124	}
125
126	inline	void				WriteSample(sample_t value) {
127		*((uchar*)fBuffer) = uchar(value * (sample_t)127 + 128);
128	}
129};
130
131
132// CharSampleBuffer
133
134template<typename sample_t>
135class CharSampleBuffer : public SampleBuffer<sizeof(char)> {
136 public:
137	inline						CharSampleBuffer(void* buffer)
138									: SampleBuffer<sizeof(char)>(buffer) {
139	}
140
141	inline	sample_t			ReadSample() const {
142		return (sample_t)*((char*)fBuffer) / (sample_t)127;
143	}
144
145	inline	void				WriteSample(sample_t value) {
146		*((uchar*)fBuffer) = uchar(value * (sample_t)127);
147	}
148};
149
150
151#endif	// SAMPLE_BUFFER_H
152