Advertisement
asurkis

Untitled

Jul 30th, 2019
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.39 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <SFML/Audio.hpp>
  5. #include <SFML/Graphics.hpp>
  6.  
  7. using namespace std;
  8. using namespace sf;
  9.  
  10. class MyRecorder : public SoundRecorder
  11. {
  12.     virtual bool onStart() override
  13.     {
  14.         return true;
  15.     }
  16.  
  17.     virtual void onStop() override
  18.     {
  19.     }
  20.  
  21.     virtual bool onProcessSamples(Int16 const* samples, size_t sampleCount) override
  22.     {
  23.         Lock _lock(*(this->mutex));
  24.         for (size_t i = 0; i < sampleCount; i++)
  25.         {
  26.             this->samples->push_back(samples[i]);
  27.         }
  28.         return true;
  29.     }
  30.  
  31.     Mutex* mutex;
  32.     vector<Int16>* samples;
  33.  
  34. public:
  35.     MyRecorder(Mutex &mutex, vector<Int16> &samples) :
  36.         mutex(&mutex),
  37.         samples(&samples)
  38.     {}
  39. };
  40.  
  41. int main()
  42. {
  43.     vector<string> devices = SoundRecorder::getAvailableDevices();
  44.     for (size_t i = 0; i < devices.size(); i++)
  45.     {
  46.         cout << i + 1 << ". " << devices[i] << endl;
  47.     }
  48.  
  49.     int selected = 0;
  50.     do {
  51.         cout << "Select microphone: ";
  52.         cin >> selected;
  53.     } while (selected <= 0 || selected > devices.size());
  54.     selected--;
  55.  
  56.     cout << "Microphone selected: " << devices[selected] << endl;
  57.  
  58.     Mutex mutex;
  59.     vector<Int16> samples;
  60.  
  61.     MyRecorder recorder(mutex, samples);
  62.     if (!recorder.setDevice(devices[selected]))
  63.     {
  64.         return -1;
  65.     }
  66.  
  67.     RenderWindow mainWindow(VideoMode(640, 480), "Window1");
  68.     recorder.start();
  69.  
  70.     Int16 lastSample = 0;
  71.  
  72.     bool shouldRun = true;
  73.     while (shouldRun)
  74.     {
  75.         Event evt;
  76.         while (mainWindow.pollEvent(evt))
  77.         {
  78.             if (evt.type == Event::Closed)
  79.             {
  80.                 shouldRun = false;
  81.             }
  82.  
  83.             if (evt.type == Event::KeyPressed)
  84.             {
  85.                 if (evt.key.code == Keyboard::Escape)
  86.                 {
  87.                     shouldRun = false;
  88.                 }
  89.             }
  90.         }
  91.  
  92.         int r = (lastSample >> 10) & 0x1F;
  93.         int g = (lastSample >>  5) & 0x1F;
  94.         int b = (lastSample >>  0) & 0x1F;
  95.         mainWindow.clear(Color(r, g, b));
  96.         mainWindow.display();
  97.  
  98.         sleep(milliseconds(1));
  99.         Lock _lock(mutex);
  100.  
  101.         if (!samples.empty())
  102.         {
  103.             lastSample = samples.back();
  104.             samples.clear();
  105.         }
  106.     }
  107.  
  108.     recorder.stop();
  109.     return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement