fork download
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. #include <GL/glut.h>
  5. #include <GL/glu.h>
  6. #include <unistd.h>
  7. #include <fcntl.h>
  8. #include <stropts.h>
  9. #include </usr/lib/oss/include/sys/soundcard.h>
  10.  
  11. void display(void);
  12. void keyboard(unsigned char, int, int);
  13. void mouse(int, int, int, int);
  14. void changeSize(int, int);
  15.  
  16. using namespace std;
  17.  
  18. int win;
  19. int fd;
  20.  
  21. int main(int argc, char **argv)
  22. {
  23. fd = open("/dev/dsp", O_RDWR | O_EXCL);
  24.  
  25. if(fd == -1)
  26. {
  27. std::cout << "failed to open audio device.\n";
  28. }
  29.  
  30. int frag = (2 << 16) | (7); // 2 X 128 byte fragment buffer
  31. if(ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &frag) == -1)
  32. {
  33. std::cout << "failed to set fragment_size.\n";
  34. }
  35.  
  36. int format = AFMT_S16_LE;
  37. if(ioctl(fd, SNDCTL_DSP_SETFMT, &format) == -1)
  38. {
  39. std::cout << "failed to set sample format.\n";
  40. }
  41.  
  42. int rate = 44100;
  43. if(ioctl(fd, SNDCTL_DSP_SPEED, &rate) == -1)
  44. {
  45. std::cout << "failed to set sample rate.\n";
  46. }
  47.  
  48. int channels = 1;
  49. if(ioctl(fd, SNDCTL_DSP_CHANNELS, &channels) == -1)
  50. {
  51. std::cout << "failed to set to mono.\n";
  52. }
  53.  
  54. glutInit(&argc, argv);
  55. glutInitDisplayMode(GLUT_DEPTH | GLUT_RGBA | GLUT_DOUBLE);
  56. glutInitWindowSize(1024,768);
  57. win = glutCreateWindow("Oscilloscope");
  58. glutDisplayFunc(display);
  59. glutIdleFunc(display);
  60. glutKeyboardFunc(keyboard);
  61. glutMouseFunc(mouse);
  62. glutReshapeFunc(changeSize);
  63. glClearColor(0.0,0.0,0.0,0.0);
  64.  
  65. glutMainLoop();
  66.  
  67. return 0;
  68. }
  69.  
  70. //at 44100Hz 8ms of waveform will be displayed on the screen
  71. #define SAMPLE_SIZE 704
  72. signed short samples[SAMPLE_SIZE];
  73. bool pos = true, neg = true;
  74. void display(void)
  75. {
  76. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  77. glEnable(GL_DEPTH_TEST);
  78.  
  79. glLoadIdentity();
  80.  
  81. read(fd, (char *)samples, sizeof(samples));
  82.  
  83. bool trigger = false;
  84. bool been_neg = false;
  85. bool falling_edge = false;
  86. int x = 0;
  87. glBegin(GL_LINE_STRIP);
  88. for(int i = 0; i < sizeof(samples) / sizeof(signed short); i++)
  89. {
  90. if(samples[i] < 1000 && pos == true)
  91. {
  92. neg = true;
  93. pos = false;
  94. }
  95.  
  96. if(samples[i] > 1000 && neg == true)
  97. {
  98. neg = false;
  99. pos = true;
  100. }
  101.  
  102. if(neg)
  103. {
  104. been_neg = true;
  105. }
  106.  
  107. if(pos && been_neg)
  108. {
  109. trigger = true;
  110. }
  111.  
  112. if(trigger && neg && !falling_edge)
  113. {
  114. falling_edge = true;
  115. std::cout << "Falling Edge: " << (1.0 / 44100 * x) << "\n";
  116. }
  117.  
  118. if(trigger)
  119. {
  120. glVertex3f(x / (SAMPLE_SIZE * 1.0), samples[i] / pow(2,15), 0.0);
  121. x++;
  122. }
  123. }
  124. glEnd();
  125.  
  126. glutSwapBuffers();
  127. glutPostRedisplay();
  128. }
  129.  
  130. void keyboard(unsigned char key, int x, int y)
  131. {
  132. if(key == 'x')
  133. {
  134. glutDestroyWindow(win);
  135. exit(0);
  136. }
  137. }
  138.  
  139. void mouse(int button, int state, int x, int y)
  140. {
  141. std::cout << "Time: " << (x / 1024.0) * (1.0 / 44100 * SAMPLE_SIZE) << "\n";
  142. }
  143.  
  144. void changeSize(int w, int h)
  145. {
  146. if (h == 0)
  147. h = 1;
  148.  
  149. float ratio = w * 1.0 / h;
  150.  
  151. // Use the Projection Matrix
  152. glMatrixMode(GL_PROJECTION);
  153.  
  154. // Reset Matrix
  155. glLoadIdentity();
  156.  
  157. // Set the viewport to be the entire window
  158. glViewport(0, 0, w, h);
  159.  
  160. // Set the correct perspective.
  161. //gluPerspective(45.0f, ratio, 0.1f, 100.0f);
  162. glOrtho(0.0, 1.0, -1.0, 1.0, 1.0, -1.0);
  163.  
  164. // Get Back to the Modelview
  165. glMatrixMode(GL_MODELVIEW);
  166. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:4:21: error: GL/glut.h: No such file or directory
prog.cpp:5:20: error: GL/glu.h: No such file or directory
prog.cpp:9:48: error: /usr/lib/oss/include/sys/soundcard.h: No such file or directory
prog.cpp: In function ‘int main(int, char**)’:
prog.cpp:31: error: ‘SNDCTL_DSP_SETFRAGMENT’ was not declared in this scope
prog.cpp:36: error: ‘AFMT_S16_LE’ was not declared in this scope
prog.cpp:37: error: ‘SNDCTL_DSP_SETFMT’ was not declared in this scope
prog.cpp:43: error: ‘SNDCTL_DSP_SPEED’ was not declared in this scope
prog.cpp:49: error: ‘SNDCTL_DSP_CHANNELS’ was not declared in this scope
prog.cpp:54: error: ‘glutInit’ was not declared in this scope
prog.cpp:55: error: ‘GLUT_DEPTH’ was not declared in this scope
prog.cpp:55: error: ‘GLUT_RGBA’ was not declared in this scope
prog.cpp:55: error: ‘GLUT_DOUBLE’ was not declared in this scope
prog.cpp:55: error: ‘glutInitDisplayMode’ was not declared in this scope
prog.cpp:56: error: ‘glutInitWindowSize’ was not declared in this scope
prog.cpp:57: error: ‘glutCreateWindow’ was not declared in this scope
prog.cpp:58: error: ‘glutDisplayFunc’ was not declared in this scope
prog.cpp:59: error: ‘glutIdleFunc’ was not declared in this scope
prog.cpp:60: error: ‘glutKeyboardFunc’ was not declared in this scope
prog.cpp:61: error: ‘glutMouseFunc’ was not declared in this scope
prog.cpp:62: error: ‘glutReshapeFunc’ was not declared in this scope
prog.cpp:63: error: ‘glClearColor’ was not declared in this scope
prog.cpp:65: error: ‘glutMainLoop’ was not declared in this scope
prog.cpp: In function ‘void display()’:
prog.cpp:76: error: ‘GL_COLOR_BUFFER_BIT’ was not declared in this scope
prog.cpp:76: error: ‘GL_DEPTH_BUFFER_BIT’ was not declared in this scope
prog.cpp:76: error: ‘glClear’ was not declared in this scope
prog.cpp:77: error: ‘GL_DEPTH_TEST’ was not declared in this scope
prog.cpp:77: error: ‘glEnable’ was not declared in this scope
prog.cpp:79: error: ‘glLoadIdentity’ was not declared in this scope
prog.cpp:87: error: ‘GL_LINE_STRIP’ was not declared in this scope
prog.cpp:87: error: ‘glBegin’ was not declared in this scope
prog.cpp:88: warning: comparison between signed and unsigned integer expressions
prog.cpp:120: error: ‘glVertex3f’ was not declared in this scope
prog.cpp:124: error: ‘glEnd’ was not declared in this scope
prog.cpp:126: error: ‘glutSwapBuffers’ was not declared in this scope
prog.cpp:127: error: ‘glutPostRedisplay’ was not declared in this scope
prog.cpp:81: warning: ignoring return value of ‘ssize_t read(int, void*, size_t)’, declared with attribute warn_unused_result
prog.cpp: In function ‘void keyboard(unsigned char, int, int)’:
prog.cpp:134: error: ‘glutDestroyWindow’ was not declared in this scope
prog.cpp:135: error: ‘exit’ was not declared in this scope
prog.cpp: In function ‘void changeSize(int, int)’:
prog.cpp:152: error: ‘GL_PROJECTION’ was not declared in this scope
prog.cpp:152: error: ‘glMatrixMode’ was not declared in this scope
prog.cpp:155: error: ‘glLoadIdentity’ was not declared in this scope
prog.cpp:158: error: ‘glViewport’ was not declared in this scope
prog.cpp:162: error: ‘glOrtho’ was not declared in this scope
prog.cpp:165: error: ‘GL_MODELVIEW’ was not declared in this scope
prog.cpp:149: warning: unused variable ‘ratio’
stdout
Standard output is empty