-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathScheduler.c
141 lines (131 loc) · 4.35 KB
/
Scheduler.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
#include "Granular.h"
#include <portaudio.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int writeIndex;
int samplesPassed;
int audioCallback( const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void *userData );
int audioCallback( const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void *userData ){
SAMPLE *out = (SAMPLE*)outputBuffer;
const SAMPLE *in = (const SAMPLE*)inputBuffer;
unsigned int i;
(void) timeInfo;
(void) statusFlags;
for(i=0; i<framesPerBuffer; i++)
{
int j;
//one grain scheduled per second
if(samplesPassed % (int)((float)SAMPLERATE/20.0) == 0){
for(j = 0; j < MAX_NUM_GRAINS; j++){
if(!grains[j].samplesRemaining){
int direction;
float speed;
float randomChance = (double)rand()/RAND_MAX;
if(randomChance > 0.6){
speed = 1;
} else if (randomChance > 0.3){
speed = 0.5;
} else{
speed = 2;
}
int durationInMs = 25 + (int)(((double)rand()/RAND_MAX)*1000);
int offsetInMs = (int)(((double)rand()/RAND_MAX)*1000);
if((double)rand()/RAND_MAX > 0.5){
direction = -1;
} else{
direction = 1;
}
//fprintf(stdout, "grains being initialized at index %d!\n", j);
initGrain(&(grains[j]), direction, speed, durationInMs, offsetInMs, writeIndex, HANNING);
break;
}
}
}
//copied twice for the two channels
int k;
for(k=0; k<2; k++){
writeIndex %= (DELAYLINE_SAMPLES);
//delayLine[writeIndex] = *in++;
*out = *in++;
delayLine[writeIndex] = *out;
for(j = 0; j<MAX_NUM_GRAINS; j++){
*out += synthesize(&grains[j]);
}
*out++;
writeIndex++;
samplesPassed++;
}
}
return paContinue;
}
int run(){
srand(time(NULL));
PaError err;
err = Pa_Initialize();
if(err != paNoError) goto error;
int numInputChannels, numOutputChannels;
int inputDeviceIndex = 7, outputDeviceIndex = 6;
PaStreamParameters inputParameters, outputParameters;
inputParameters.device = inputDeviceIndex;
outputParameters.device = outputDeviceIndex;
const PaDeviceInfo* inputDeviceInfo = Pa_GetDeviceInfo(inputDeviceIndex);
const PaDeviceInfo* outputDeviceInfo = Pa_GetDeviceInfo(outputDeviceIndex);
inputParameters.channelCount = inputDeviceInfo->maxInputChannels;
inputParameters.sampleFormat = paFloat32;
inputParameters.suggestedLatency = inputDeviceInfo->defaultLowInputLatency;
inputParameters.hostApiSpecificStreamInfo = NULL;
outputParameters.device = outputDeviceIndex;
if(outputParameters.device == paNoDevice){
fprintf(stderr,"Error: Input device is invalid.\n Maybe the device has been unplugged?");
goto error;
}
if(outputParameters.device == paNoDevice){
fprintf(stderr,"Error: Output device is invalid.\n Maybe the device has been unplugged?");
goto error;
}
outputParameters.channelCount = outputDeviceInfo->maxOutputChannels;
outputParameters.sampleFormat = paFloat32;
outputParameters.suggestedLatency = outputDeviceInfo->defaultLowOutputLatency;
outputParameters.hostApiSpecificStreamInfo = NULL;
numInputChannels = inputParameters.channelCount;
numOutputChannels = outputParameters.channelCount;
fprintf(stderr,"numInputChannels: %d \n", numInputChannels);
fprintf(stderr,"numOutputChannels: %d \n", numOutputChannels);
writeIndex = 0;
samplesPassed = 0;
PaStream* stream;
err = Pa_OpenStream(
&stream,
&inputParameters,
&outputParameters,
inputDeviceInfo->defaultSampleRate,
paFramesPerBufferUnspecified,
paClipOff,
audioCallback,
NULL);
fprintf(stderr, "Error: %d\n", err);
if(err != paNoError) goto error;
err = Pa_StartStream(stream);
//pause application
while(1){};
Pa_StopStream(stream);
Pa_CloseStream(stream);
err = Pa_Terminate();
if( err != paNoError ) goto error;
return 0;
error:
fprintf( stderr, "An error occured while using the portaudio stream\n" );
fprintf( stderr, "Error number: %d\n", err );
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
err = Pa_Terminate();
return -1;
}