xref: /haiku/src/apps/mediaplayer/media_node_framework/audio/SampleBuffer.h (revision 0fc56ed57bdd5d2d44f33edd17d94210704613bd)
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 
21 template<int BYTES_PER_SAMPLE>
22 class SampleBuffer {
23  protected:
24 	typedef	uint8				sample_block_t[BYTES_PER_SAMPLE];
25 
26  public:
SampleBuffer(void * buffer)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 
58 template<typename sample_t>
59 class FloatSampleBuffer : public SampleBuffer<sizeof(float)> {
60  public:
FloatSampleBuffer(void * buffer)61 	inline						FloatSampleBuffer(void* buffer)
62 									: SampleBuffer<sizeof(float)>(buffer) {
63 	}
64 
ReadSample()65 	inline	sample_t			ReadSample() const {
66 		return *((float*)fBuffer);
67 	}
68 
WriteSample(sample_t value)69 	inline	void				WriteSample(sample_t value) {
70 		*((float*)fBuffer) = value;
71 	}
72 };
73 
74 
75 // IntSampleBuffer
76 
77 template<typename sample_t>
78 class IntSampleBuffer : public SampleBuffer<sizeof(int)> {
79  public:
IntSampleBuffer(void * buffer)80 	inline						IntSampleBuffer(void* buffer)
81 									: SampleBuffer<sizeof(int)>(buffer) {
82 	}
83 
ReadSample()84 	inline	sample_t			ReadSample() const {
85 		return (sample_t)*((int*)fBuffer) / (sample_t)0x7fffffff;
86 	}
87 
WriteSample(sample_t value)88 	inline	void				WriteSample(sample_t value) {
89 		*((int*)fBuffer) = int(value * (sample_t)0x7fffffff);
90 	}
91 };
92 
93 
94 // ShortSampleBuffer
95 
96 template<typename sample_t>
97 class ShortSampleBuffer : public SampleBuffer<sizeof(short)> {
98  public:
ShortSampleBuffer(void * buffer)99 	inline						ShortSampleBuffer(void* buffer)
100 									: SampleBuffer<sizeof(short)>(buffer) {
101 	}
102 
ReadSample()103 	inline	sample_t			ReadSample() const {
104 		return (sample_t)*((short*)fBuffer) / (sample_t)32767;
105 	}
106 
WriteSample(sample_t value)107 	inline	void				WriteSample(sample_t value) {
108 		*((short*)fBuffer) = short(value * (sample_t)32767);
109 	}
110 };
111 
112 
113 // UCharSampleBuffer
114 
115 template<typename sample_t>
116 class UCharSampleBuffer : public SampleBuffer<sizeof(uchar)> {
117  public:
UCharSampleBuffer(void * buffer)118 	inline						UCharSampleBuffer(void* buffer)
119 									: SampleBuffer<sizeof(uchar)>(buffer) {
120 	}
121 
ReadSample()122 	inline	sample_t			ReadSample() const {
123 		return ((sample_t)*((uchar*)fBuffer) - 128) / (sample_t)127;
124 	}
125 
WriteSample(sample_t value)126 	inline	void				WriteSample(sample_t value) {
127 		*((uchar*)fBuffer) = uchar(value * (sample_t)127 + 128);
128 	}
129 };
130 
131 
132 // CharSampleBuffer
133 
134 template<typename sample_t>
135 class CharSampleBuffer : public SampleBuffer<sizeof(char)> {
136  public:
CharSampleBuffer(void * buffer)137 	inline						CharSampleBuffer(void* buffer)
138 									: SampleBuffer<sizeof(char)>(buffer) {
139 	}
140 
ReadSample()141 	inline	sample_t			ReadSample() const {
142 		return (sample_t)*((char*)fBuffer) / (sample_t)127;
143 	}
144 
WriteSample(sample_t value)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