2 * Copyright © 2000-2003 Ingo Weinhold <ingo_weinhold@gmx.de>
3 * All rights reserved. Distributed under the terms of the MIT licensce.
6 // This file contains the following classes:
10 // * ShortSampleBuffer
11 // * UCharSampleBuffer
14 #ifndef SAMPLE_BUFFER_H
15 #define SAMPLE_BUFFER_H
17 #include <SupportDefs.h>
21 template<int BYTES_PER_SAMPLE
>
24 typedef uint8 sample_block_t
[BYTES_PER_SAMPLE
];
27 inline SampleBuffer(void* buffer
)
28 : fBuffer((sample_block_t
*)buffer
) { }
30 inline void operator+=(int samples
) {
33 inline void operator-=(int samples
) {
37 inline void operator++(int) {
40 inline void operator--(int) {
44 inline void* operator+(int samples
) {
45 return fBuffer
+ samples
;
47 inline void* operator-(int samples
) {
48 return fBuffer
+ samples
;
52 sample_block_t
* fBuffer
;
58 template<typename sample_t
>
59 class FloatSampleBuffer
: public SampleBuffer
<sizeof(float)> {
61 inline FloatSampleBuffer(void* buffer
)
62 : SampleBuffer
<sizeof(float)>(buffer
) {
65 inline sample_t
ReadSample() const {
66 return *((float*)fBuffer
);
69 inline void WriteSample(sample_t value
) {
70 *((float*)fBuffer
) = value
;
77 template<typename sample_t
>
78 class IntSampleBuffer
: public SampleBuffer
<sizeof(int)> {
80 inline IntSampleBuffer(void* buffer
)
81 : SampleBuffer
<sizeof(int)>(buffer
) {
84 inline sample_t
ReadSample() const {
85 return (sample_t
)*((int*)fBuffer
) / (sample_t
)0x7fffffff;
88 inline void WriteSample(sample_t value
) {
89 *((int*)fBuffer
) = int(value
* (sample_t
)0x7fffffff);
96 template<typename sample_t
>
97 class ShortSampleBuffer
: public SampleBuffer
<sizeof(short)> {
99 inline ShortSampleBuffer(void* buffer
)
100 : SampleBuffer
<sizeof(short)>(buffer
) {
103 inline sample_t
ReadSample() const {
104 return (sample_t
)*((short*)fBuffer
) / (sample_t
)32767;
107 inline void WriteSample(sample_t value
) {
108 *((short*)fBuffer
) = short(value
* (sample_t
)32767);
115 template<typename sample_t
>
116 class UCharSampleBuffer
: public SampleBuffer
<sizeof(uchar
)> {
118 inline UCharSampleBuffer(void* buffer
)
119 : SampleBuffer
<sizeof(uchar
)>(buffer
) {
122 inline sample_t
ReadSample() const {
123 return ((sample_t
)*((uchar
*)fBuffer
) - 128) / (sample_t
)127;
126 inline void WriteSample(sample_t value
) {
127 *((uchar
*)fBuffer
) = uchar(value
* (sample_t
)127 + 128);
134 template<typename sample_t
>
135 class CharSampleBuffer
: public SampleBuffer
<sizeof(char)> {
137 inline CharSampleBuffer(void* buffer
)
138 : SampleBuffer
<sizeof(char)>(buffer
) {
141 inline sample_t
ReadSample() const {
142 return (sample_t
)*((char*)fBuffer
) / (sample_t
)127;
145 inline void WriteSample(sample_t value
) {
146 *((uchar
*)fBuffer
) = uchar(value
* (sample_t
)127);
151 #endif // SAMPLE_BUFFER_H