-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyAudio.cpp
executable file
·118 lines (104 loc) · 2.23 KB
/
MyAudio.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//
// MyAudio.cpp
// stkExample
//
// Created by Jorge Herrera on 2011-10-26.
//
// ------------------
// Class to abstract audio stream
// IMPORTANT: it will quit the app if it can't setup or start!!!
#include "MyAudio.h"
#include <iostream>
#include <cstdlib>
// constructor
MyAudio::MyAudio( unsigned int nChann, float sr, unsigned int buffSize, RtAudioFormat format )
{
m_audio = NULL;
m_numChannels = nChann;
m_sampleRate = sr;
m_bufferSize = buffSize;
m_format = format;
}
// destructor
MyAudio::~MyAudio()
{
if( m_audio )
{
stop();
delete m_audio;
}
}
// RtAudio configuration
void MyAudio::setup( RtAudioCallback callback, void * userData )
{
if( !callback )
{
std::cerr << "No callback provided!" << std::endl;
exit(1);
}
try
{
m_audio = new RtAudio();
}
catch( RtError & err ) {
err.printMessage();
exit(1);
}
if( m_audio->getDeviceCount() < 1 )
{
// nopes
std::cout << "no audio devices found!" << std::endl;
exit(1);
}
// let RtAudio print messages to stderr.
m_audio->showWarnings( true );
// set input and output parameters
RtAudio::StreamParameters iParams, oParams;
iParams.deviceId = m_audio->getDefaultInputDevice();
iParams.nChannels = m_numChannels;
iParams.firstChannel = 0;
oParams.deviceId = m_audio->getDefaultOutputDevice();
oParams.nChannels = m_numChannels;
oParams.firstChannel = 0;
// set the callback and start stream
try
{
m_audio->openStream( &oParams,
&iParams,
m_format,
m_sampleRate,
&m_bufferSize,
callback,
userData);
std::cerr << "Buffer size defined by RtAudio: " << m_bufferSize << std::endl;
}
catch( RtError & err )
{
err.printMessage();
exit(1);
}
}
// start audio stream
void MyAudio::start()
{
try
{
m_audio->startStream();
// test RtAudio functionality for reporting latency.
std::cout << "stream latency: " << m_audio->getStreamLatency() << " frames" << std::endl;
}
catch( RtError & err )
{
err.printMessage();
exit(1);
}
}
// stop audio stream
void MyAudio::stop()
{
if( m_audio )
{
if ( m_audio->isStreamRunning() ) m_audio->stopStream();
if ( m_audio->isStreamOpen() ) m_audio->closeStream();
}
}