btrfs: Attempt to fix GCC2 build.
[haiku.git] / src / apps / mediaplayer / media_node_framework / audio / SampleBuffer.h
blob16668208904d5286dfadc4c9e73594c77bf0db79
1 /*
2 * Copyright © 2000-2003 Ingo Weinhold <ingo_weinhold@gmx.de>
3 * All rights reserved. Distributed under the terms of the MIT licensce.
4 */
6 // This file contains the following classes:
7 // * SampleBuffer
8 // * FloatSampleBuffer
9 // * IntSampleBuffer
10 // * ShortSampleBuffer
11 // * UCharSampleBuffer
12 // * CharSampleBuffer
14 #ifndef SAMPLE_BUFFER_H
15 #define SAMPLE_BUFFER_H
17 #include <SupportDefs.h>
19 // SampleBuffer
21 template<int BYTES_PER_SAMPLE>
22 class SampleBuffer {
23 protected:
24 typedef uint8 sample_block_t[BYTES_PER_SAMPLE];
26 public:
27 inline SampleBuffer(void* buffer)
28 : fBuffer((sample_block_t*)buffer) { }
30 inline void operator+=(int samples) {
31 fBuffer += samples;
33 inline void operator-=(int samples) {
34 fBuffer -= samples;
37 inline void operator++(int) {
38 fBuffer++;
40 inline void operator--(int) {
41 fBuffer--;
44 inline void* operator+(int samples) {
45 return fBuffer + samples;
47 inline void* operator-(int samples) {
48 return fBuffer + samples;
51 protected:
52 sample_block_t* fBuffer;
56 // FloatSampleBuffer
58 template<typename sample_t>
59 class FloatSampleBuffer : public SampleBuffer<sizeof(float)> {
60 public:
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;
75 // IntSampleBuffer
77 template<typename sample_t>
78 class IntSampleBuffer : public SampleBuffer<sizeof(int)> {
79 public:
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);
94 // ShortSampleBuffer
96 template<typename sample_t>
97 class ShortSampleBuffer : public SampleBuffer<sizeof(short)> {
98 public:
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);
113 // UCharSampleBuffer
115 template<typename sample_t>
116 class UCharSampleBuffer : public SampleBuffer<sizeof(uchar)> {
117 public:
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);
132 // CharSampleBuffer
134 template<typename sample_t>
135 class CharSampleBuffer : public SampleBuffer<sizeof(char)> {
136 public:
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