-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
284 lines (241 loc) · 8.68 KB
/
main.c
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <math.h>
#include <string.h>
#include <limits.h>
#include "portaudio.h"
#include "sndfile.h"
/*
Data structure to pass to callback
includes the sound file, info about the sound file, and a position
cursor (where we are in the sound file)
*/
typedef struct SNDData {
SNDFILE *sndFile;
SF_INFO sfInfo;
int position;
} SNDData;
SNDData *sndData;
int loop;
int *cursor;
/*
Callback function for audio output
*/
int Callback(const void *input,
void *output,
unsigned long frameCount,
const PaStreamCallbackTimeInfo* paTimeInfo,
PaStreamCallbackFlags statusFlags,
void *userData) {
SNDData *data = (SNDData *)userData; /* we passed a data structure
into the callback so we have something to work with */
/* current pointer into the output */
int *out = (int *)output;
int thisSize = frameCount;
int thisRead;
cursor = out; /* set the output cursor to the beginning */
while (thisSize > 0) {
/* seek to our current file position */
sf_seek(data->sndFile, data->position, SEEK_SET);
/* are we going to read past the end of the file?*/
if (thisSize > (data->sfInfo.frames - data->position) && loop) {
/*if we are, only read to the end of the file*/
thisRead = data->sfInfo.frames - data->position;
/* and then loop to the beginning of the file */
data->position = 0;
} else {
/* otherwise, we'll just fill up the rest of the output buffer */
thisRead = thisSize;
/* and increment the file position */
data->position += thisRead;
}
/* since our output format and channel interleaving is the same as
sf_readf_int's requirements */
/* we'll just read straight into the output buffer */
sf_readf_int(data->sndFile, cursor, thisRead);
/* increment the output cursor*/
cursor += thisRead;
/* decrement the number of samples left to process */
thisSize -= thisRead;
}
return paContinue;
}
/*******************************************************************/
struct vertex {
double x;
double y;
double z;
};
struct lines {
struct vertex *vert1;
struct vertex *vert2;
};
struct lines **lines = NULL;
void init() // Called before main loop to set up the program
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
}
// Called at the start of the program, after a glutPostRedisplay() and during idle
// to display a frame
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
free(lines[0]->vert1);
free(lines[0]->vert2);
free(lines[0]);
for (int i=1; i<1000; i++) {
lines[i]->vert1->x -= .1f;
lines[i]->vert2->x -= .1f;
lines[i-1] = lines[i];
glBegin(GL_LINES);
glVertex3f(lines[i-1]->vert1->x, lines[i-1]->vert1->y, lines[i-1]->vert1->z);
glVertex3f(lines[i-1]->vert2->x,lines[i-1]->vert2->y, lines[i-1]->vert2->z);
glEnd();
}
lines[999] = malloc(sizeof(struct lines));
lines[999]->vert1 = malloc(sizeof(struct vertex));
lines[999]->vert1->x = 49.9;
lines[999]->vert1->y = lines[998]->vert2->y;
lines[999]->vert1->z = -250;
lines[999]->vert2 = malloc(sizeof(struct vertex));
lines[999]->vert2->x = 50.0;
if (cursor != NULL) {
lines[999]->vert2->y = ((double)*cursor)/(INT_MAX/10.0);
}
lines[999]->vert2->z = -250;
glutSwapBuffers();
}
// Called every time a window is resized to resize the projection matrix
void reshape(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-0.1, 0.1, -1 * (float)h/(10.0*(float)w), (float)h/(10.0*(float)w), 0.5, 1000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void glLoop()
{
// Update the positions of the objects
// ......
// ......
glutPostRedisplay();
glutTimerFunc(1, glLoop, 0);
}
int main(int argc, char **argv)
{
loop = 0;
char *filename;
int visualization = 0;
int verbose = 0;
double duration = 0.0f;
cursor = NULL;
for (int i=1; i<argc; i++) {
if (!strcmp(argv[i], "-w")) {
visualization = 1;
} else if (!strcmp(argv[i], "-l")) {
loop = 1;
} else if (!strcmp(argv[i], "-v")) {
verbose = 1;
} else {
filename = argv[i];
}
}
lines = malloc(1000*sizeof(struct lines*));
if (lines == NULL) {
return 1;
}
for (int i=0; i<1000; i++) {
lines[i] = malloc(sizeof(struct lines));
lines[i]->vert1 = malloc(sizeof(struct vertex));
lines[i]->vert1->x = 0.0;
lines[i]->vert1->y = 0.0;
lines[i]->vert1->z = -500.0;
lines[i]->vert2 = malloc(sizeof(struct vertex));
lines[i]->vert2->x = 0.0;
lines[i]->vert2->y = 0.0;
lines[i]->vert2->z = -500.0;
}
sndData = (SNDData *)malloc(sizeof(SNDData));
PaStream *stream;
PaError error;
PaStreamParameters outputParameters;
sndData->position = 0;
sndData->sfInfo.format = 0;
sndData->sndFile = sf_open(filename, SFM_READ, &sndData->sfInfo);
if (!sndData->sndFile) {
printf("error opening file\n");
return 1;
}
duration = ((double)sndData->sfInfo.frames/(double)sndData->sfInfo.samplerate);
/* start portaudio */
Pa_Initialize();
/* set the output parameters */
outputParameters.device = Pa_GetDefaultOutputDevice(); /* use the
default device */
outputParameters.channelCount = sndData->sfInfo.channels; /* use the
same number of channels as our sound file */
outputParameters.sampleFormat = paInt32; /* 32bit int format */
outputParameters.suggestedLatency = 0.01; /* 200 ms ought to satisfy
even the worst sound card */
outputParameters.hostApiSpecificStreamInfo = 0; /* no api specific data */
/* try to open the output */
error = Pa_OpenStream(&stream, /* stream is a 'token' that we need
to save for future portaudio calls */
0, /* no input */
&outputParameters,
sndData->sfInfo.samplerate, /* use the same
sample rate as the sound file */
paFramesPerBufferUnspecified, /* let
portaudio choose the buffersize */
paNoFlag, /* no special modes (clip off, dither off) */
Callback, /* callback function defined above */
sndData); /* pass in our data structure so the
callback knows what's up */
/* if we can't open it, then bail out */
if (error)
{
printf("error opening output, error code = %i\n", error);
Pa_Terminate();
return 1;
}
/* when we start the stream, the callback starts getting called */
Pa_StartStream(stream);
if (verbose) {
printf("Filename: %s\n", filename);
printf("Duration: %f seconds\n", duration);
printf("Sample Rate: %d\n", sndData->sfInfo.samplerate);
}
if (visualization) {
//glutInit(&argc, argv); // Initializes glut
// Sets up a double buffer with RGBA components and a depth component
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA);
// Sets the window size to 512*512 square pixels
glutInitWindowSize(512, 512);
// Sets the window position to the upper left
glutInitWindowPosition(0, 0);
// Creates a window using internal glut functionality
glutCreateWindow(filename);
// passes reshape and display functions to the OpenGL machine for callback
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutIdleFunc(display);
init();
glutTimerFunc(1, glLoop, 0);
// Starts the program.
glutMainLoop();
}
if (!visualization && !loop) {
Pa_Sleep(duration*1000.0f);
} else if (!visualization && loop) {
Pa_Sleep(LONG_MAX);
}
Pa_StopStream(stream);
return 0;
}