videocapture_microphone.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include <opencv2/core.hpp>
  2. #include <opencv2/videoio.hpp>
  3. #include <opencv2/highgui.hpp>
  4. #include <iostream>
  5. using namespace cv;
  6. using namespace std;
  7. int main(int, char**)
  8. {
  9. Mat frame;
  10. vector<Mat> audioData;
  11. VideoCapture cap;
  12. vector<int> params { CAP_PROP_AUDIO_STREAM, 0,
  13. CAP_PROP_VIDEO_STREAM, -1 };
  14. cap.open(0, CAP_MSMF, params);
  15. if (!cap.isOpened())
  16. {
  17. cerr << "ERROR! Can't to open microphone" << endl;
  18. return -1;
  19. }
  20. const int audioBaseIndex = (int)cap.get(CAP_PROP_AUDIO_BASE_INDEX);
  21. const int numberOfChannels = (int)cap.get(CAP_PROP_AUDIO_TOTAL_CHANNELS);
  22. cout << "CAP_PROP_AUDIO_DATA_DEPTH: " << depthToString((int)cap.get(CAP_PROP_AUDIO_DATA_DEPTH)) << endl;
  23. cout << "CAP_PROP_AUDIO_SAMPLES_PER_SECOND: " << cap.get(CAP_PROP_AUDIO_SAMPLES_PER_SECOND) << endl;
  24. cout << "CAP_PROP_AUDIO_TOTAL_CHANNELS: " << numberOfChannels << endl;
  25. cout << "CAP_PROP_AUDIO_TOTAL_STREAMS: " << cap.get(CAP_PROP_AUDIO_TOTAL_STREAMS) << endl;
  26. const double cvTickFreq = getTickFrequency();
  27. int64 sysTimeCurr = getTickCount();
  28. int64 sysTimePrev = sysTimeCurr;
  29. while ((sysTimeCurr-sysTimePrev)/cvTickFreq < 10)
  30. {
  31. if (cap.grab())
  32. {
  33. for (int nCh = 0; nCh < numberOfChannels; nCh++)
  34. {
  35. cap.retrieve(frame, audioBaseIndex+nCh);
  36. audioData.push_back(frame);
  37. sysTimeCurr = getTickCount();
  38. }
  39. }
  40. else
  41. {
  42. cerr << "Grab error" << endl;
  43. break;
  44. }
  45. }
  46. int numberOfSamles = 0;
  47. for (auto item : audioData)
  48. numberOfSamles+=item.cols;
  49. cout << "Number of samples: " << numberOfSamles << endl;
  50. return 0;
  51. }