-
Notifications
You must be signed in to change notification settings - Fork 0
/
vinylVisualizer.c
1230 lines (1060 loc) · 37.6 KB
/
vinylVisualizer.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* ===========================================================================================
* Author: Harrison Zafrin (hzz200@nyu.edu)(harrison@bombsandbottles.com)
* Organization: Bombs and Bottles LLC. - www.bombsandbottles.com
*
* Title: Vinyl Visualizer
* CProgramming Final Project 2014
*
* Description: A Vinyl Inspired Audio Visualizer With
* Interactive Filtering and Sample Rate Modulation.
*
* Credits :
* Time Domain Based Filters Based Off - http://www.mega-nerd.com/Res/IADSPL/RBJ-filters.txt
* Sample Rate Modulation API - http://www.mega-nerd.com/SRC/api_full.html
* OpenGl Skeleton - Uri Nieto
* ===========================================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
#include <string.h> /* for memset */
#include <ncurses.h> /* This library is for getting input without hitting return */
#include <portaudio.h>
#include <sndfile.h>
#include <samplerate.h>
/* OpenGL */
#ifdef __MACOSX_CORE__
#include <GLUT/glut.h>
#else
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#endif
/* Platform-Dependent Sleep Routines. */
#if defined( __WINDOWS_ASIO__ ) || defined( __WINDOWS_DS__ )
#include <windows.h>
#define SLEEP( milliseconds ) Sleep( (DWORD) milliseconds )
#else // Unix variants
#include <unistd.h>
#define SLEEP( milliseconds ) usleep( (unsigned long) (milliseconds * 1000.0) )
#endif
//-----------------------------------------------------------------------------
// global variables and #defines
//-----------------------------------------------------------------------------
#define SAMPLING_RATE 44100
#define FRAMES_PER_BUFFER 1024
#define MONO 1
#define STEREO 2
#define ITEMS_PER_BUFFER (FRAMES_PER_BUFFER * 2)
#define FORMAT paFloat32
#define SAMPLE float
#define SRC_RATIO_INCREMENT 0.01
#define FILTER_CUTOFF_INCREMENT 100 //hz
#define RESONANCE_INCREMENT 1 // Q Factor
#define PI 3.14159265358979323846264338327950288
#define INITIAL_VOLUME 0.5
#define VOLUME_INCREMENT 0.1
#define cmp_abs(x) ( sqrt( (x).re * (x).re + (x).im * (x).im ) )
#define ROTATION_INCR .75f
#define INIT_WIDTH 1280
#define INIT_HEIGHT 720
/* Global Sound Data Struct */
typedef struct {
/* Audio File Members */
SNDFILE* inFile;
SF_INFO sfinfo1;
float amplitude;
/* Sample Rate Converter Members */
SRC_DATA src_data;
SRC_STATE* src_state;
int src_error;
int src_converter_type;
double src_ratio;
float src_inBuffer[FRAMES_PER_BUFFER * 16];
float src_outBuffer[FRAMES_PER_BUFFER * 2];
/* Low Pass Filter Members */
bool lpf_On;
int lpf_freq;
int lpf_res;
/* High Pass Filter Members */
bool hpf_On;
int hpf_freq;
int hpf_res;
/* Filter On/Off */
char* filterState[2];
/* OpenGL Members */
float gl_audioBuffer[ITEMS_PER_BUFFER];
} paData;
/* Global Data Initialized */
paData data;
PaStream *g_stream;
// WxH Of OpenGL Window
GLsizei g_width = INIT_WIDTH;
GLsizei g_height = INIT_HEIGHT;
GLsizei g_last_width = INIT_WIDTH;
GLsizei g_last_height = INIT_HEIGHT;
typedef double MY_TYPE;
typedef char BYTE; // 8-bit unsigned entity.
//Global Audio Vars
GLint g_buffer_size = FRAMES_PER_BUFFER;
//Threads Management
GLboolean g_ready = false;
// Fill Mode
GLenum g_fillmode = GL_FILL;
// light 0 position
GLfloat g_light0_pos[4] = { 2.0f, 1.2f, 4.0f, 1.0f };
// light 1 parameters
GLfloat g_light1_ambient[] = { .2f, .2f, .2f, 1.0f };
GLfloat g_light1_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat g_light1_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat g_light1_pos[4] = { -2.0f, 0.0f, -4.0f, 1.0f };
// fullscreen
GLboolean g_fullscreen = false;
// modelview stuff
GLfloat g_linewidth = 2.0f;
//DIANA Rotates
GLfloat g_inc = 0.0f;
GLfloat g_inc_y = 0.0;
GLfloat g_inc_x = 0.0;
bool g_key_rotate_y = false;
bool g_key_rotate_x = false;
//-----------------------------------------------------------------------------
// Function Prototypes
//-----------------------------------------------------------------------------
/* OpenGL Functions */
void idleFunc( );
void displayFunc( );
void reshapeFunc( int width, int height );
void keyboardFunc( unsigned char, int, int );
void specialKey( int key, int x, int y );
void specialUpKey( int key, int x, int y);
void initialize_graphics( );
void initialize_glut(int argc, char *argv[]);
void drawTimeDomain(SAMPLE *buffer, float R, float G, float B);
void rotateView();
void drawCircle(float r, int num_segments, float* buffer, bool scalar);
/* Audio Processing Functions */
void initialize_src_type();
void initialize_audio(const char* inFile);
void stop_portAudio();
void initialize_SRC_DATA();
void initialize_Filters();
static void lowPassFilter(float *inBuffer, int numChannels);
static void highPassFilter(float *inBuffer, int numChannels);
float computeRMS(float *buffer);
void brickwall(float *buffer, int numChannels);
/* Command Line Prints */
void help();
void printGUI();
//-----------------------------------------------------------------------------
// Name: Main
// Desc: ...
//-----------------------------------------------------------------------------
int main( int argc, char *argv[] )
{
/* Check Arguments */
if ( argc != 2 ) {
printf("Usage: %s: Input Audio\n", argv[0]);
return EXIT_FAILURE;
}
/* Initialize SRC Algorithm */
initialize_src_type();
/* Initialize Glut */
initialize_glut(argc, argv);
/* Initialize PortAudio */
initialize_audio(argv[1]);
/* Start Curses Mode */
initscr();
cbreak(); // Line Buffering Disabled
noecho(); // Comment This Out if You Want to Show Characters When They Are Typed
curs_set(0); // Make ncurses Cursor Invisible
/* Print Help Menu and GUI */
help();
printGUI();
/* Main Interactive Loop, Quits With 'q' */
glutMainLoop();
return EXIT_SUCCESS;
}
//-----------------------------------------------------------------------------
// name: initialize_src_type
// desc: Initializes SRC Algorithm, Stores Value in data.src_converter_type
//-----------------------------------------------------------------------------
void initialize_src_type()
{
bool valid = false;
char buffer[4];
int srcType;
/* Print User Menu */
printf("\nChoose The Quality of Sample Rate Conversion\n"
"Best Quality = 0\n"
"Medium Quality = 1\n"
"Fastest Quality = 2\n"
"Zero Order Hold = 3\n"
"Linear Processing = 4\n");
printf("Enter a Number Between 0 and 4 : ");
/* Read From stdin, Convert to int */
while (!valid)
{
if (fgets(buffer, sizeof(buffer), stdin) != NULL)
{
srcType = atoi(buffer);
}
if (srcType > -1 && srcType < 5)
{
valid = true;
}
else
valid = false;
if (!valid)
{
printf("Error: Please Enter a Number Between 0 and 4 : ");
}
}
/* Intialize SRC Algorithm */
if (valid == true)
{
data.src_converter_type = srcType;
}
}
//-----------------------------------------------------------------------------
// name: help()
// desc: Prints Command Line Usage
//-----------------------------------------------------------------------------
void help()
{
/* Print Command Line Instructions */
mvprintw(0,0,
"----------------------------------------------------\n"
"Vinyl Visualizer\n"
"----------------------------------------------------\n"
"'f' - Toggle Fullscreen\n"
"'j/k' - Increase/Decrease LPF Freq. Cutoff by 100hz\n"
"'i/o' - Increase/Decrease LPF Resonance by 1.0 Q Factor\n"
"'s/d' - Increase/Decrease HPF Freq. Cutoff by 100hz\n"
"'w/e' - Increase/Decrease HPF Resonance by 1.0 Q Factor\n"
"'-/=' - Increase/Decrease Speed/Pitch\n"
"'m' - To Mute Output Audio\n"
"'r' - Reset All Parameters\n"
"'CURSOR ARROWS' - Rotate Visuals\n"
"'q' - Quit\n"
"----------------------------------------------------\n"
"\n" );
refresh();
}
//-----------------------------------------------------------------------------
// Name: paCallback( )
// Desc: Callback from PortAudio
//-----------------------------------------------------------------------------
static int paCallback( const void *inputBuffer,
void *outputBuffer, unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags, void *userData )
{
/* Cast Appropriate Types and Define Some Useful Variables */
(void) inputBuffer;
float* out = (float*)outputBuffer;
paData *data = (paData*)userData;
int i, numberOfFrames, numInFrames;
/* This if Statement Ensures Smooth VariSpeed Output */
if (fmod((double)framesPerBuffer, data->src_data.src_ratio) == 0)
{
numInFrames = framesPerBuffer;
}
else
numInFrames = (framesPerBuffer/data->src_data.src_ratio) + 2;
/* Read FramesPerBuffer Amount of Data from inFile into buffer[] */
numberOfFrames = sf_readf_float(data->inFile, data->src_inBuffer, numInFrames);
/* Looping of inFile if EOF is Reached */
if (numberOfFrames < framesPerBuffer/data->src_data.src_ratio)
{
sf_seek(data->inFile, 0, SEEK_SET);
}
/* Inform SRC Data How Many Input Frames To Process */
data->src_data.input_frames = numberOfFrames;
data->src_data.end_of_input = 0;
/* Perform SRC Modulation, Processed Samples are in src_outBuffer[] */
if ((data->src_error = src_process (data->src_state, &data->src_data))) {
printf ("\nError : %s\n\n", src_strerror (data->src_error)) ;
exit (1);
}
/* Perform Lowpass Filtering */
if (data->lpf_On == true) {
lowPassFilter(data->src_outBuffer, data->sfinfo1.channels);
}
/* Perform Highpass Filtering */
if (data->hpf_On == true) {
highPassFilter(data->src_outBuffer, data->sfinfo1.channels);
}
/* Prevent Blowing Out the Speakers Hopefully */
brickwall(data->src_outBuffer, data->sfinfo1.channels);
// printf("%ld, %ld\n",data->src_data.output_frames_gen, data->src_data.input_frames_used);
/* Write Processed SRC Data to Audio Out and Visual Out */
for (i = 0; i < framesPerBuffer * data->sfinfo1.channels; i++)
{
out[i] = data->src_outBuffer[i] * data->amplitude;
}
g_ready = true;
return 0;
}
//-----------------------------------------------------------------------------
// Name: initialize_audio()
// Desc: Initializes PortAudio With Globals
//-----------------------------------------------------------------------------
void initialize_audio(const char* inFile)
{
PaStreamParameters outputParameters;
PaError err;
/* Open the audio file */
if (( data.inFile = sf_open( inFile, SFM_READ, &data.sfinfo1 )) == NULL )
{
printf("Error, Couldn't Open The File\n");
exit (1);
}
/* Check for Compatibility */
if (data.sfinfo1.channels > 2)
{
printf("Error, File Must be Stereo or Mono\n");
exit (1);
}
/* Print info about audio file */
printf("\nAudio File: %s\nFrames: %d\nSamples: %d\nChannels: %d\nSampleRate: %d\n",
inFile, (int)data.sfinfo1.frames, (int)data.sfinfo1.frames * (int)data.sfinfo1.channels,
(int)data.sfinfo1.channels, (int)data.sfinfo1.samplerate);
/* Initialize PortAudio */
Pa_Initialize();
/* Set output stream parameters */
outputParameters.device = Pa_GetDefaultOutputDevice();
outputParameters.channelCount = data.sfinfo1.channels;
outputParameters.sampleFormat = paFloat32;
outputParameters.suggestedLatency =
Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
outputParameters.hostApiSpecificStreamInfo = NULL;
/* Initialize SRC */
if ((data.src_state = src_new (data.src_converter_type, data.sfinfo1.channels, &data.src_error )) == NULL)
{
printf ("Error, SRC Initialization Failed\n");
exit (1);
}
/* Sets Up The SRC_DATA Struct */
initialize_SRC_DATA();
/* Sets Up Filters */
initialize_Filters();
/* Set Initial Amplitude */
data.amplitude = INITIAL_VOLUME;
/* Open audio stream */
err = Pa_OpenStream( &g_stream,
NULL,
&outputParameters,
data.sfinfo1.samplerate,
FRAMES_PER_BUFFER,
paNoFlag,
paCallback,
&data );
if (err != paNoError) {
printf("PortAudio error: open stream: %s\n", Pa_GetErrorText(err));
}
/* Start audio stream */
err = Pa_StartStream( g_stream );
if (err != paNoError) {
printf( "PortAudio error: start stream: %s\n", Pa_GetErrorText(err));
}
}
//-----------------------------------------------------------------------------
// Name: void stop_portAudio()
// Desc: //Stops Port Audio
//-----------------------------------------------------------------------------
void stop_portAudio()
{
PaError err;
/* Stop audio stream */
err = Pa_StopStream( g_stream );
if (err != paNoError) {
printf( "PortAudio error: stop stream: %s\n", Pa_GetErrorText(err));
}
/* Close audio stream */
err = Pa_CloseStream(g_stream);
if (err != paNoError) {
printf("PortAudio error: close stream: %s\n", Pa_GetErrorText(err));
}
/* Terminate audio stream */
err = Pa_Terminate();
if (err != paNoError) {
printf("PortAudio error: terminate: %s\n", Pa_GetErrorText(err));
}
}
//-----------------------------------------------------------------------------
// Name: initialize_SRC_DATA()
// Desc: Sets Up The SRC_DATA Struct to Pass to src_process(SRC_STATE *state, SRC_DATA *data)
//-----------------------------------------------------------------------------
void initialize_SRC_DATA()
{
data.src_ratio = 1; //Sets Default Playback Speed
/*---------------*/
data.src_data.input_frames = 0; //Start with Zero to Force Load
data.src_data.data_in = data.src_inBuffer; //Point to SRC inBuffer
data.src_data.data_out = data.src_outBuffer; //Point to SRC OutBuffer
data.src_data.output_frames = FRAMES_PER_BUFFER;//Number of Frames to Write Out
data.src_data.src_ratio = data.src_ratio; //Sets Default Playback Speed
}
//-----------------------------------------------------------------------------
// Name: initialize_Filters()
// Desc: Sets Default Filter Settings
//-----------------------------------------------------------------------------
void initialize_Filters()
{
/* Lowpass Filter */
data.lpf_On = false; //Default Off
data.lpf_freq = 20000; //Max Open
data.lpf_res = 1; //No Resonance
/* Highpass Filter */
data.hpf_On = false; //Default Off
data.hpf_freq = 20; //Max Closed
data.hpf_res = 1; //No Resonance
/* Define Off/On */
data.filterState[0] = "Off";
data.filterState[1] = "On";
}
//-----------------------------------------------------------------------------
// Name: lowPassFilter(float *inBuffer)
// Desc: Applies 2 Pole lowPassFilter based on http://www.mega-nerd.com/Res/IADSPL/RBJ-filters.txt
//-----------------------------------------------------------------------------
static void lowPassFilter(float *inBuffer, int numChannels)
{
// Difference Equation
/* y[n] = (b0/a0)*x[n] + (b1/a0)*x[n-1] + (b2/a0)*x[n-2] - (a1/a0)*y[n-1] - (a2/a0)*y[n-2] */
float processed_sample;
int i;
float alpha, omega, cs;
float a0, a1, a2, b0, b1, b2;
/* Account for Transient Response of Filter */
static float lx1 = 0, lx2 = 0, ly1 = 0, ly2 = 0;
static float rx1 = 0, rx2 = 0, ry1 = 0, ry2 = 0;
/* First Compute a Few Intermediate Variables */
omega = 2.0 * PI * data.lpf_freq / data.sfinfo1.samplerate;
alpha = sin(omega) / (2.0 * data.lpf_res);
cs = cos(omega);
/* Calcuate Filter Coefficients */
b0 = (1.0 - cs) / 2.0;
b1 = 1.0 - cs;
b2 = (1.0 - cs) / 2.0;
a0 = 1.0 + alpha;
a1 = -2.0 * cs;
a2 = 1.0 - alpha;
/* Lowpass the Chunk of Data for Mono */
if (numChannels == MONO)
{
for (i = 0; i < FRAMES_PER_BUFFER; i++)
{
/* Run a Sample Through the Difference Equation */
processed_sample = (b0/a0) * inBuffer[i] + (b1/a0) * lx1 + (b2/a0) * lx2 -
(a1/a0) * ly1 - (a2/a0) * ly2;
/***********/
/* Shift the Samples in the Equation So That n-1 == n */
lx2 = lx1;
lx1 = inBuffer[i];
/* Feedback Loop for Poles, Also Shift Samples So That n-1 == n */
ly2 = ly1;
ly1 = processed_sample;
/***********/
/* Return Lowpassed Sample into outBuffer[] */
inBuffer[i] = processed_sample;
}
}
/* Lowpass the Chunk of Data for Stereo */
else if (numChannels == STEREO)
{
/* Left Channel */
for (i = 0; i < FRAMES_PER_BUFFER; i++)
{
processed_sample = (b0/a0) * inBuffer[2*i] + (b1/a0) * lx1 + (b2/a0) * lx2 -
(a1/a0) * ly1 - (a2/a0) * ly2;
/***********/
lx2 = lx1;
lx1 = inBuffer[2*i];
ly2 = ly1;
ly1 = processed_sample;
/***********/
/* Return Lowpassed Sample into outBuffer[] */
inBuffer[2*i] = processed_sample;
}
/* Right Channel */
for (i = 0; i < FRAMES_PER_BUFFER; i++)
{
processed_sample = (b0/a0) * inBuffer[2*i+1] + (b1/a0) * rx1 + (b2/a0) * rx2 -
(a1/a0) * ry1 - (a2/a0) * ry2;
/***********/
rx2 = rx1;
rx1 = inBuffer[2*i+1];
ry2 = ry1;
ry1 = processed_sample;
/***********/
/* Return Lowpassed Sample into outBuffer[] */
inBuffer[2*i+1] = processed_sample;
}
}
}
//-----------------------------------------------------------------------------
// Name: highPassFilter(float *inBuffer)
// Desc: Applies 2 Pole highPassFilter based on http://www.mega-nerd.com/Res/IADSPL/RBJ-filters.txt
//-----------------------------------------------------------------------------
static void highPassFilter(float *inBuffer, int numChannels)
{
// Difference Equation
/* y[n] = (b0/a0)*x[n] + (b1/a0)*x[n-1] + (b2/a0)*x[n-2] - (a1/a0)*y[n-1] - (a2/a0)*y[n-2] */
float processed_sample;
int i;
float alpha, omega, cs;
float a0, a1, a2, b0, b1, b2;
/* Account for Transient Response of Filter */
static float lx1 = 0, lx2 = 0, ly1 = 0, ly2 = 0;
static float rx1 = 0, rx2 = 0, ry1 = 0, ry2 = 0;
/* First Compute a Few Intermediate Variables */
omega = 2.0 * PI * data.hpf_freq / data.sfinfo1.samplerate;
alpha = sin(omega) / (2.0 * data.hpf_res);
cs = cos(omega);
/* Calcuate Filter Coefficients */
b0 = (1.0 + cs) / 2.0 ;
b1 = -(1.0 + cs) ;
b2 = (1.0 + cs) / 2.0 ;
a0 = 1.0 + alpha ;
a1 = -2.0 * cs ;
a2 = 1.0 - alpha ;
/* Highpass the Chunk of Data for Mono */
if (numChannels == MONO)
{
for (i = 0; i < FRAMES_PER_BUFFER; i++)
{
/* Run a Sample Through the Difference Equation */
processed_sample = (b0/a0) * inBuffer[i] + (b1/a0) * lx1 + (b2/a0) * lx2 -
(a1/a0) * ly1 - (a2/a0) * ly2;
/***********/
/* Shift the Samples in the Equation So That n-1 == n */
lx2 = lx1;
lx1 = inBuffer[i];
/* Feedback Loop for Poles, Also Shift Samples So That n-1 == n */
ly2 = ly1;
ly1 = processed_sample;
/***********/
/* Return Highpassed Sample into outBuffer[] */
inBuffer[i] = processed_sample;
}
}
/* Highpass the Chunk of Data for Stereo */
else if (numChannels == STEREO)
{
/* Left Channel */
for (i = 0; i < FRAMES_PER_BUFFER; i++)
{
processed_sample = (b0/a0) * inBuffer[2*i] + (b1/a0) * lx1 + (b2/a0) * lx2 -
(a1/a0) * ly1 - (a2/a0) * ly2;
/***********/
lx2 = lx1;
lx1 = inBuffer[2*i];
ly2 = ly1;
ly1 = processed_sample;
/***********/
/* Return Highpassed Sample into outBuffer[] */
inBuffer[2*i] = processed_sample;
}
/* Right Channel */
for (i = 0; i < FRAMES_PER_BUFFER; i++)
{
processed_sample = (b0/a0) * inBuffer[2*i+1] + (b1/a0) * rx1 + (b2/a0) * rx2 -
(a1/a0) * ry1 - (a2/a0) * ry2;
/***********/
rx2 = rx1;
rx1 = inBuffer[2*i+1];
ry2 = ry1;
ry1 = processed_sample;
/***********/
/* Return Highpassed Sample into outBuffer[] */
inBuffer[2*i+1] = processed_sample;
}
}
}
//-----------------------------------------------------------------------------
// Name: keyboardFunc( )
// Desc: key event
//-----------------------------------------------------------------------------
void keyboardFunc( unsigned char key, int x, int y )
{
switch( key )
{
/* Fullscreen */
case 'f':
if( !g_fullscreen )
{
g_last_width = g_width;
g_last_height = g_height;
glutFullScreen();
}
else
glutReshapeWindow( g_last_width, g_last_height );
g_fullscreen = !g_fullscreen;
printGUI();
break;
/* Resets Normal Default Playback */
case 'r':
data.src_data.src_ratio = 1; //Sets Default Playback Speed
initialize_Filters();
data.amplitude = INITIAL_VOLUME;
printGUI();
break;
/* Change SRC Ratio */
case '-':
if (data.src_data.src_ratio <= 2.0)
{
data.src_data.src_ratio += SRC_RATIO_INCREMENT;
}
printGUI();
break;
case '=':
if (data.src_data.src_ratio >= 0.5 )
{
data.src_data.src_ratio -= SRC_RATIO_INCREMENT;
}
printGUI();
break;
/* Low Pass Filter Controls */
/****************************/
/* Engage/Disengage Filter */
case 'l':
if (data.lpf_On == false)
{
//Power On
data.lpf_On = true;
}
else if (data.lpf_On == true)
{
//Power Off
data.lpf_On = false;
}
printGUI();
break;
/* Increase/Decrease Lpf Freq Cutoff */
case 'j':
data.lpf_freq -= FILTER_CUTOFF_INCREMENT;
if (data.lpf_freq < 20)
{
data.lpf_freq = 20;
}
printGUI();
break;
case 'k':
data.lpf_freq += FILTER_CUTOFF_INCREMENT;
if (data.lpf_freq > 20000)
{
data.lpf_freq = 20000;
}
printGUI();
break;
/* Increase/Decrease Lpf Resonance */
case 'i':
data.lpf_res -= RESONANCE_INCREMENT;
if (data.lpf_res <= 1)
{
data.lpf_res = 1;
}
printGUI();
break;
case 'o':
data.lpf_res += RESONANCE_INCREMENT;
if (data.lpf_res >= 10)
{
data.lpf_res = 10;
}
printGUI();
break;
/* High Pass Filter Controls */
/*****************************/
/* Engage/Disengage Filter */
case 'a':
if (data.hpf_On == false)
{
//Power On
data.hpf_On = true;
}
else if (data.hpf_On == true)
{
//Power Off
data.hpf_On = false;
}
printGUI();
break;
/* Increase/Decrease Hpf Freq Cutoff */
case 's':
data.hpf_freq -= FILTER_CUTOFF_INCREMENT;
if (data.hpf_freq < 20)
{
data.hpf_freq = 20;
}
printGUI();
break;
case 'd':
data.hpf_freq += FILTER_CUTOFF_INCREMENT;
if (data.hpf_freq > 20000)
{
data.hpf_freq = 20000;
}
printGUI();
break;
/* Increase/Decrease Lpf Resonance */
case 'w':
data.hpf_res -= RESONANCE_INCREMENT;
if (data.hpf_res <= 1)
{
data.hpf_res = 1;
}
printGUI();
break;
case 'e':
data.hpf_res += RESONANCE_INCREMENT;
if (data.hpf_res >= 10)
{
data.hpf_res = 10;
}
printGUI();
break;
/* Amplitude Controls */
/*****************************/
/* Mute Output */
case 'm':
if (data.amplitude > 0 )
{
//Mute
data.amplitude = 0;
}
else if (data.amplitude == 0)
{
//UnMute
data.amplitude = INITIAL_VOLUME;
}
break;
/* Increase/Decrease Amplitude of Playbacl */
case ',':
data.amplitude -= VOLUME_INCREMENT;
if (data.amplitude < 0) {
data.amplitude = 0;
}
break;
case '.':
data.amplitude += VOLUME_INCREMENT;
if (data.amplitude > 1) {
data.amplitude = 1;
}
break;
/* Exit */
case 'q':
/* Close Stream Before Exiting */
stop_portAudio();
/* Cleanup SRC */
src_delete (data.src_state);
/* End Curses Mode */
endwin();
exit(0);
break;
}
}
//-----------------------------------------------------------------------------
// Name: initialize_glut( )
// Desc: Initializes Glut with the global vars
//-----------------------------------------------------------------------------
void initialize_glut(int argc, char *argv[]) {
// initialize GLUT
glutInit( &argc, argv );
// double buffer, use rgb color, enable depth buffer
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
// initialize the window size
glutInitWindowSize( g_width, g_height );
// set the window postion
glutInitWindowPosition( 400, 100 );
// create the window
glutCreateWindow( "Vinyl Visualizer");
// full screen
if( g_fullscreen )
glutFullScreen();
// set the idle function - called when idle
glutIdleFunc( idleFunc );
// set the display function - called when redrawing
glutDisplayFunc( displayFunc );
// set the reshape function - called when client area changes
glutReshapeFunc( reshapeFunc );
// set the keyboard function - called on keyboard events
glutKeyboardFunc( keyboardFunc );
// set window's to specialKey callback
glutSpecialFunc( specialKey );
// set window's to specialUpKey callback (when the key is up is called)
glutSpecialUpFunc( specialUpKey );
// do our own initialization
initialize_graphics( );
}
//-----------------------------------------------------------------------------
// Name: idleFunc( )
// Desc: callback from GLUT
//-----------------------------------------------------------------------------
void idleFunc( )
{
// render the scene
glutPostRedisplay( );
}
//-----------------------------------------------------------------------------
// Name: specialUpKey( int key, int x, int y)
// Desc: Callback to know when a special key is pressed
//-----------------------------------------------------------------------------
void specialKey(int key, int x, int y) {
// Check which (arrow) key is pressed
switch(key) {
case GLUT_KEY_LEFT : // Arrow key left is pressed
g_key_rotate_y = true;
g_inc_y = -ROTATION_INCR;
break;
case GLUT_KEY_RIGHT : // Arrow key right is pressed
g_key_rotate_y = true;
g_inc_y = ROTATION_INCR;
break;
case GLUT_KEY_UP : // Arrow key up is pressed
g_key_rotate_x = true;
g_inc_x = ROTATION_INCR;
break;
case GLUT_KEY_DOWN : // Arrow key down is pressed
g_key_rotate_x = true;
g_inc_x = -ROTATION_INCR;
break;
}
}
//-----------------------------------------------------------------------------
// Name: specialUpKey( int key, int x, int y)
// Desc: Callback to know when a special key is up
//-----------------------------------------------------------------------------
void specialUpKey( int key, int x, int y) {
// Check which (arrow) key is unpressed
switch(key) {
case GLUT_KEY_LEFT : // Arrow key left is unpressed
g_key_rotate_y = false;
break;
case GLUT_KEY_RIGHT : // Arrow key right is unpressed
g_key_rotate_y = false;
break;
case GLUT_KEY_UP : // Arrow key up is unpressed
g_key_rotate_x = false;
break;
case GLUT_KEY_DOWN : // Arrow key down is unpressed
g_key_rotate_x = false;
break;
}
}
//-----------------------------------------------------------------------------
// Name: void rotateView ()
// Desc: Rotates the current view by the angle specified in the globals
//-----------------------------------------------------------------------------
void rotateView ()
{
static GLfloat angle_x = 0;
static GLfloat angle_y = 0;
if (g_key_rotate_y) {
glRotatef ( angle_y += g_inc_y, 0.0f, 1.0f, 0.0f );
}
else {
glRotatef (angle_y, 0.0f, 1.0f, 0.0f );
}
if (g_key_rotate_x) {
glRotatef ( angle_x += g_inc_x, 1.0f, 0.0f, 0.0f );