Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <SFML/Audio.hpp>
- #include <SFML/Graphics.hpp>
- using namespace std;
- using namespace sf;
- class MyRecorder : public SoundRecorder
- {
- virtual bool onStart() override
- {
- return true;
- }
- virtual void onStop() override
- {
- }
- virtual bool onProcessSamples(Int16 const* samples, size_t sampleCount) override
- {
- Lock _lock(*(this->mutex));
- for (size_t i = 0; i < sampleCount; i++)
- {
- this->samples->push_back(samples[i]);
- }
- return true;
- }
- Mutex* mutex;
- vector<Int16>* samples;
- public:
- MyRecorder(Mutex &mutex, vector<Int16> &samples) :
- mutex(&mutex),
- samples(&samples)
- {}
- };
- int main()
- {
- vector<string> devices = SoundRecorder::getAvailableDevices();
- for (size_t i = 0; i < devices.size(); i++)
- {
- cout << i + 1 << ". " << devices[i] << endl;
- }
- int selected = 0;
- do {
- cout << "Select microphone: ";
- cin >> selected;
- } while (selected <= 0 || selected > devices.size());
- selected--;
- cout << "Microphone selected: " << devices[selected] << endl;
- Mutex mutex;
- vector<Int16> samples;
- MyRecorder recorder(mutex, samples);
- if (!recorder.setDevice(devices[selected]))
- {
- return -1;
- }
- RenderWindow mainWindow(VideoMode(640, 480), "Window1");
- recorder.start();
- Int16 lastSample = 0;
- bool shouldRun = true;
- while (shouldRun)
- {
- Event evt;
- while (mainWindow.pollEvent(evt))
- {
- if (evt.type == Event::Closed)
- {
- shouldRun = false;
- }
- if (evt.type == Event::KeyPressed)
- {
- if (evt.key.code == Keyboard::Escape)
- {
- shouldRun = false;
- }
- }
- }
- int r = (lastSample >> 10) & 0x1F;
- int g = (lastSample >> 5) & 0x1F;
- int b = (lastSample >> 0) & 0x1F;
- mainWindow.clear(Color(r, g, b));
- mainWindow.display();
- sleep(milliseconds(1));
- Lock _lock(mutex);
- if (!samples.empty())
- {
- lastSample = samples.back();
- samples.clear();
- }
- }
- recorder.stop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement