PEBL
2.2
Psychology Experiment Building Language - Cross-platform psychological experiment development system
PlatformAudioIn.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/PlatformAudioIn.h
4
// Purpose: Contains platform-specific sound recording 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 __PLATFORMAUDIOIN_H__
28
#define __PLATFORMAUDIOIN_H__
29
#ifdef PEBL_AUDIOIN
30
//#include "../../devices/PAudioIn.h"
31
#include "../../base/Variant.h"
32
#include "../../base/PEBLObject.h"
33
34
#ifdef PEBL_OSX
35
36
#else
37
#include "SDL.h"
38
//#include "SDL/SDL_audioin.h" //now handled directly.
39
#endif
40
41
#include "SDL_audio.h"
42
#include "PlatformAudioOut.h"
43
44
#include <string>
45
#include <iostream>
46
47
48
#ifdef PEBL_MIXER
49
#include "SDL_mixer.h"
50
#endif
51
52
#include <string>
53
54
#ifndef PEBL_MIXER
55
struct
AudioInfo
{
56
57
SDL_AudioSpec
spec
;
58
Uint8 *
audio
;
/* Pointer to wave data */
59
Uint32
audiolen
;
/* Length of wave data */
60
Uint32
audiopos
;
/* Current play position */
61
62
63
unsigned
int
bytesPerSample
;
//size of a sample
64
Uint32
recordpos
;
//current index in the buffer (in bytes)
65
Uint32
counter
;
//A counter to use that keeps track of samples
66
//since the beginning of recording.
67
68
69
int
volume
;
/* Relative volume. 0-100*/
70
const
char
*
name
;
71
72
// Constructor - initialize audio pointer to NULL
73
AudioInfo
() :
audio
(
NULL
),
audiolen
(0),
audiopos
(0),
recordpos
(0),
counter
(0),
volume
(100),
name
(
NULL
),
bytesPerSample
(0) {
74
}
75
76
// Destructor - WARNING: We intentionally DO NOT free the audio buffer here
77
// because SDL_mixer may still be using it for playback. Freeing it would
78
// cause heap corruption when SDL_mixer accesses the freed memory.
79
// This causes a small memory leak (~264KB per voice key call), but that's
80
// acceptable for calibration that runs once per session.
81
// TODO: Implement proper reference counting or notification when SDL_mixer
82
// is done with the buffer.
83
~AudioInfo
() {
84
std::cerr <<
"~AudioInfo: Destructor called, audio="
<< (
void
*)
audio
85
<<
" ("
<<
audiolen
<<
" bytes) - NOT freeing (SDL_mixer may be using it)\n"
;
86
// INTENTIONALLY NOT FREEING: free(audio);
87
}
88
};
89
90
#endif
91
92
93
94
//struct SoundInfo{
95
// SDL_AudioSpec spec;
96
// Uint8 *audio; //A pointer to the buffer.
97
// Uint32 audiolen; //length of the buffer (in bytes, not samples!!!)
98
// Uint32 audiopos;
99
// Uint8 bytesPerSample; //size of a sample
100
// Uint32 recordpos; //current index in the buffer (in samples, not bytes)
101
// Uint32 counter; //A counter to use that keeps track of samples
102
// //since the beginning of recording.
103
//
104
// const char*name;
105
//};
106
107
// struct AudioInfo{
108
// SDL_AudioSpec spec;
109
// Uint8 *audio; /* Pointer to wave data */
110
// Uint32 audiolen; /* Length of wave data */
111
// int audiopos; /* Current play position */
112
// int volume; /* Relative volume. 0-100*/
113
// const char* name;
114
// };
115
116
class
PlatformAudioIn:
public
PEBLObjectBase
117
{
118
public
:
119
PlatformAudioIn();
120
~PlatformAudioIn();
121
counted_ptr<AudioInfo>
GetAudioOutBuffer();
122
counted_ptr<AudioInfo>
ReleaseAudioOutBuffer();
123
bool
UseBuffer(
counted_ptr<AudioInfo>
buffer);
124
bool
CreateBuffer(
int
size);
125
bool
RecordToBuffer
();
126
bool
Record();
127
bool
PauseAudioMonitor();
// Pause recording (does NOT close device)
128
bool
CloseAudio();
// Explicitly close and cleanup audio device
129
130
131
Variant
VoiceKey(
double
thresh,
unsigned
int
sustain);
132
void
SaveBufferToWave(
Variant
filename) ;
133
bool
Initialize(
int
);
134
135
double
Power(Sint16 * data,
int
length);
136
137
// Get audio statistics for the most recent N milliseconds from ring buffer
138
// Returns: [energy, power, rmssd] as a PEBL list
139
Variant
GetRecentAudioStats(
int
milliseconds);
140
141
void
ComputeStats(Sint16 * data,
int
length,
142
double
&,
double
&,
143
int
&,
int
&,
double
& );
144
145
private
:
146
SDL_AudioDeviceID mAudioDevice;
// SDL2 audio device ID for recording
147
unsigned
int
mSampleRate;
148
Uint16 mAudioFormat;
149
unsigned
int
mBytesPerSample;
150
unsigned
int
mSamples;
151
152
counted_ptr<AudioInfo>
mWave;
// Now using reference-counted pointer
153
};
154
155
156
#endif
157
#endif
NULL
#define NULL
Definition
BinReloc.cpp:317
PEBLObjectBase
Definition
PEBLObject.h:73
Variant
Definition
Variant.h:67
counted_ptr
Definition
rc_ptrs.h:66
PEBLObjects::RecordToBuffer
Variant RecordToBuffer(Variant v)
Definition
PEBLObjects.cpp:1325
AudioInfo
Definition
PlatformAudio.h:33
AudioInfo::recordpos
Uint32 recordpos
Definition
sdl/PlatformAudioOut.h:80
AudioInfo::bytesPerSample
unsigned int bytesPerSample
Definition
sdl/PlatformAudioOut.h:79
AudioInfo::audio
Uint8 * audio
Definition
PlatformAudio.h:35
AudioInfo::name
const char * name
Definition
sdl/PlatformAudioOut.h:85
AudioInfo::spec
SDL_AudioSpec spec
Definition
PlatformAudio.h:34
AudioInfo::audiolen
Uint32 audiolen
Definition
PlatformAudio.h:36
AudioInfo::AudioInfo
AudioInfo()
Definition
sdl/PlatformAudioOut.h:47
AudioInfo::audiopos
int audiopos
Definition
PlatformAudio.h:37
AudioInfo::~AudioInfo
virtual ~AudioInfo()
Definition
sdl/PlatformAudioOut.h:61
AudioInfo::volume
int volume
Definition
PlatformAudio.h:38
AudioInfo::counter
Uint32 counter
Definition
sdl/PlatformAudioOut.h:81
src
platforms
sdl
PlatformAudioIn.h
Generated by
1.9.8