PEBL 2.2
Psychology Experiment Building Language - Cross-platform psychological experiment development system
sdl/PlatformAudioOut.h
Go to the documentation of this file.
1//* -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- */
3// Name: src/platforms/sdl/PlatformAudioOut.h
4// Purpose: Contains platform-specific sound playing routines
5// Author: Shane T. Mueller, Ph.D.
6// Copyright: (c) 2003-2026 Shane T. Mueller <smueller@obereed.net>
7// License: GPL 2
8//
9//
10//
11// This file is part of the PEBL project.
12//
13// PEBL is free software; you can redistribute it and/or modify
14// it under the terms of the GNU General Public License as published by
15// the Free Software Foundation; either version 2 of the License, or
16// (at your option) any later version.
17//
18// PEBL is distributed in the hope that it will be useful,
19// but WITHOUT ANY WARRANTY; without even the implied warranty of
20// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21// GNU General Public License for more details.
22//
23// You should have received a copy of the GNU General Public License
24// along with PEBL; if not, write to the Free Software
25// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27#ifndef __PLATFORMAUDIOOUT_H__
28#define __PLATFORMAUDIOOUT_H__
29
30#include "../../devices/PAudioOut.h"
31#include "../../base/PEBLObject.h"
32#include "SDL.h"
33#include "SDL_audio.h"
34
35#ifdef PEBL_MIXER
36#include "SDL_mixer.h"
37#endif
38
39#include <string>
40
41// AudioInfo is needed for audio input even when PEBL_MIXER is enabled
42// Now inherits from PEBLObjectBase to use counted_ptr reference counting
43#if !defined(PEBL_MIXER) || defined(PEBL_AUDIOIN)
44class AudioInfo : public PEBLObjectBase
45{
46public:
48 audio(NULL),
49 audiolen(0),
50 audiopos(0),
52 recordpos(0),
53 counter(0),
54 volume(100),
55 name(NULL),
56 ownsBuffer(true) // By default, AudioInfo owns the buffer
57 {
58 SDL_zero(spec);
59 }
60
61 virtual ~AudioInfo()
62 {
63 // Clean up the audio buffer only if we own it
64 // Note: We use free() instead of delete[] because the buffer is allocated with malloc()
65 // When shared with Mix_Chunk, ownsBuffer will be false
66 if(audio && ownsBuffer)
67 {
68 free(audio);
69 audio = NULL;
70 }
71 }
72
73 // Data members (kept public for backward compatibility)
74 SDL_AudioSpec spec;
75 Uint8 *audio; /* Pointer to wave data */
76 Uint32 audiolen; /* Length of wave data */
77 Uint32 audiopos; /* Current play position */
78
79 unsigned int bytesPerSample; //size of a sample
80 Uint32 recordpos; //current index in the buffer (in bytes)
81 Uint32 counter; //A counter to use that keeps track of samples
82 //since the beginning of recording.
83
84 int volume; /* Relative volume. 0-100*/
85 const char* name;
86 bool ownsBuffer; /* Whether AudioInfo should free the buffer in destructor */
87};
88
89#endif
90
91class PlatformAudioOut: virtual public PAudioOut, public PEBLObjectBase
92{
93 public:
95 PlatformAudioOut(const std::string & filename);
97
98 virtual bool LoadSoundFile(const std::string & filename);
99 bool LoadSoundFromData( Uint8 *buffer, long unsigned int size, SDL_AudioSpec *spec, Uint32 recordpos = 0);
100
101
102 virtual bool CreateSineWave(float freq, long unsigned int length,long double volume);
103 // virtual bool CreateSquareWave(float freq, double length, int amplitude);
104 // virtual bool CreateSawtoothWave(float freq, double length, int amplitude);
105
106
107 virtual bool Play();
108 virtual bool PlayForeground();
109 virtual bool Stop();
110 void SaveBufferToWave(Variant filename);
111
112#ifdef PEBL_MIXER
113 virtual bool SetPanning(pDouble left, pDouble right);
114 void SetRepeats(int num){mRepeats=num;};
115 void SetRecordPos(Uint32 pos){mRecordPos=pos;}; // Update recorded position
116#endif
117
118#if !defined(PEBL_MIXER) || defined(PEBL_AUDIOIN)
119 bool ConvertAudio(AudioInfo & info);
121 void PrintAudioInfo();
122#endif
123 virtual bool Initialize();
124
125
126private:
127
128
129 // void PlayCallBack(void * dummy, Uint8 * stream, int len);
130
131 static bool mLoaded; //This will be true when a file
132 //has been loaded or a buffer has been
133 //generated.
134 int mRepeats;
135 std::string mFilename;
136
137
138#ifdef PEBL_MIXER
139 Mix_Chunk * mMixerSample;
140 Uint32 mRecordPos; // Actual recorded size (for audio input buffers)
141 SDL_AudioSpec mOriginalSpec; // Original audio spec from recording (for saving WAV files)
142#else
143 AudioInfo mWave;
144#endif
145
146};
147
148
149#endif
#define NULL
Definition BinReloc.cpp:317
#define pDouble
Definition Defs.h:7
@ CDT_AUDIOBUFFER
Definition PEBLObject.h:48
virtual bool SetPanning(const double left, const double right)
Definition PAudioOut.h:52
virtual bool Initialize()
bool ConvertAudio(AudioInfo &info)
virtual ~PlatformAudioOut()
virtual bool Play()
virtual bool PlayForeground()
void SaveBufferToWave(Variant filename)
bool LoadSoundFromData(Uint8 *buffer, long unsigned int size, SDL_AudioSpec *spec, Uint32 recordpos=0)
bool CreateSineWave(float freq, double length, int amplitude)
virtual bool Stop()
bool LoadSoundFile(const char *filename)
counted_ptr< AudioInfo > GetAudioInfo()
unsigned int bytesPerSample
Uint8 * audio
const char * name
SDL_AudioSpec spec
Uint32 audiolen
virtual ~AudioInfo()