forked from micknoise/Maximilian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.cpp
157 lines (129 loc) · 4.01 KB
/
player.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* player.cpp
* rtaudiotest
*
* Created by Chris on 23/08/2011.
* Copyright 2011 Goldsmiths Creative Computing. All rights reserved.
*
*/
#include "player.h"
#include "maximilian.h"
#include <iostream>
#ifdef MAXIMILIAN_PORTAUDIO
#include "portaudio.h"
//#include "pa_mac_core.h"
#elif defined(MAXIMILIAN_RT_AUDIO)
#if defined( __WIN32__ ) || defined( _WIN32 )
#include <dsound.h>
#endif
#include "RtAudio.h"
#endif
void setup();//use this to do any initialisation if you want.
void play(double *output);//run dac! Very very often. Too often in fact. er...
#ifdef MAXIMILIAN_PORTAUDIO
int routing(const void *inputBuffer,
void *outputBuffer,
unsigned long nBufferFrames,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags status,
void *userData ){
#elif defined(MAXIMILIAN_RT_AUDIO)
int routing (void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
double streamTime, RtAudioStreamStatus status, void *userData ) {
#endif
unsigned int i, j;
#ifdef MAXIMILIAN_PORTAUDIO
float *buffer = (float *) outputBuffer;
#elif defined(MAXIMILIAN_RT_AUDIO)
double *buffer = (double *) outputBuffer;
#endif
double *lastValues = (double *) userData;
// double currentTime = (double) streamTime; Might come in handy for control
if ( status )
std::cout << "Stream underflow detected!" << std::endl;
for ( i=0; i<nBufferFrames; i++ ) {
}
// Write interleaved audio data.
for ( i=0; i<nBufferFrames; i++ ) {
play(lastValues);
for ( j=0; j<maxiSettings::channels; j++ ) {
*buffer++=lastValues[j];
}
}
return 0;
}
//This is main()
int main()
{
setup();
#ifdef MAXIMILIAN_PORTAUDIO
PaStream *stream;
PaError err;
err = Pa_Initialize();
if( err != paNoError )
std::cout << "PortAudio error: " << Pa_GetErrorText( err ) << std::endl;
double data[maxiSettings::channels];
err = Pa_OpenDefaultStream( &stream,
0, /* no input channels */
maxiSettings::channels, /* stereo output */
paFloat32, /* 64 bit floating point output */
maxiSettings::sampleRate,
maxiSettings::bufferSize, /* frames per buffer, i.e. the number
of sample frames that PortAudio will
request from the callback. Many apps
may want to use
paFramesPerBufferUnspecified, which
tells PortAudio to pick the best,
possibly changing, buffer size.*/
&routing, /* this is your callback function */
&data ); /*This is a pointer that will be passed to
your callback*/
//PaAlsa_EnableRealtimeScheduling(stream,true);
err = Pa_StartStream( stream );
if( err != paNoError )
std::cout << "PortAudio error: " << Pa_GetErrorText( err ) << std::endl;
char input;
std::cout << "\nMaximilian is playing ... press <enter> to quit.\n";
std::cin.get( input );
err = Pa_Terminate();
if( err != paNoError )
std::cout << "PortAudio error: "<< Pa_GetErrorText( err ) << std::endl;
#elif defined(MAXIMILIAN_RT_AUDIO)
RtAudio dac(RtAudio::WINDOWS_DS);
if ( dac.getDeviceCount() < 1 ) {
std::cout << "\nNo audio devices found!\n";
char input;
std::cin.get( input );
exit( 0 );
}
RtAudio::StreamParameters parameters;
parameters.deviceId = dac.getDefaultOutputDevice();
parameters.nChannels = maxiSettings::channels;
parameters.firstChannel = 0;
unsigned int sampleRate = maxiSettings::sampleRate;
unsigned int bufferFrames = maxiSettings::bufferSize;
//double data[maxiSettings::channels];
vector<double> data(maxiSettings::channels,0);
try {
dac.openStream( ¶meters, NULL, RTAUDIO_FLOAT64,
sampleRate, &bufferFrames, &routing, (void *)&(data[0]));
dac.startStream();
}
catch ( RtError& e ) {
e.printMessage();
exit( 0 );
}
char input;
std::cout << "\nMaximilian is playing ... press <enter> to quit.\n";
std::cin.get( input );
try {
// Stop the stream
dac.stopStream();
}
catch (RtError& e) {
e.printMessage();
}
if ( dac.isStreamOpen() ) dac.closeStream();
#endif
return 0;
}