当前位置:   article > 正文

C/C++ 实现录音和播放_c++录音

c++录音
  1. #include <iostream>
  2. #include <windows.h>
  3. #include <Mmsystem.h>
  4. #pragma comment(lib, "winmm.lib")
  5. static BYTE* file = (BYTE*)malloc(sizeof(BYTE) * 512);
  6. static DWORD hasRecorded = 0;
  7. static BOOL recurr = TRUE;
  8. class USER
  9. {
  10. public:
  11. int a = 0;
  12. char b = 'a';
  13. };
  14. void CALLBACK callback(HWAVEIN hwi, // 设备句柄
  15. UINT uMsg, // 消息
  16. DWORD_PTR dwInstance, // 对象
  17. DWORD_PTR dwParam1, // 参数1
  18. DWORD_PTR dwParam2); // 参数2
  19. int main()
  20. {
  21. HWAVEIN hWaveIn; //输入设备
  22. HWAVEOUT hWaveOut; //输出设备
  23. WAVEFORMATEX waveform; //定义音频流格式
  24. BYTE* pBuffer1, * pBuffer2; //输入音频缓冲区(左右声道)
  25. WAVEHDR whdr_i1, whdr_i2; //输入音频头
  26. WAVEHDR whdr_o; //输出音频头
  27. USER* user = new USER(); //定义用户
  28. // 设备数量
  29. int count = waveInGetNumDevs();
  30. printf("\n音频输入数量:%d\n", count);
  31. // 设备名称
  32. WAVEINCAPS waveIncaps;
  33. MMRESULT mmResult = waveInGetDevCaps(0, &waveIncaps, sizeof(WAVEINCAPS));//2
  34. std::cout <<"设备名称:"<< waveIncaps.szPname << std::endl;
  35. // 设置音频流格式
  36. waveform.nSamplesPerSec = 44100; // 采样率
  37. waveform.wBitsPerSample = 16; // 采样精度
  38. waveform.nChannels = 2; // 声道个数
  39. waveform.cbSize = 0; // 额外空间
  40. waveform.wFormatTag = WAVE_FORMAT_PCM; // 音频格式
  41. waveform.nBlockAlign = (waveform.wBitsPerSample * waveform.nChannels) / 8; // 块对齐
  42. waveform.nAvgBytesPerSec = waveform.nBlockAlign * waveform.nSamplesPerSec; // 传输速率
  43. //分配内存
  44. pBuffer1 = new BYTE[1024 * 10000];
  45. pBuffer2 = new BYTE[1024 * 10000];
  46. memset(pBuffer1, 0, 1024 * 10000); // 内存置0
  47. memset(pBuffer2, 0, 1024 * 10000); // 内存置0
  48. // 设置音频头
  49. whdr_i1.lpData = (LPSTR)pBuffer1; // 指向buffer
  50. whdr_i1.dwBufferLength = 1024 * 10000; // buffer大小
  51. whdr_i1.dwBytesRecorded = 0; // buffer存放大小
  52. whdr_i1.dwUser = 0;
  53. whdr_i1.dwFlags = 0;
  54. whdr_i1.dwLoops = 1;
  55. whdr_i2.lpData = (LPSTR)pBuffer1; // 指向buffer
  56. whdr_i2.dwBufferLength = 1024 * 10000; // buffer大小
  57. whdr_i2.dwBytesRecorded = 0; // buffer存放大小
  58. whdr_i2.dwUser = 0;
  59. whdr_i2.dwFlags = 0;
  60. whdr_i2.dwLoops = 1;
  61. // 开启录音
  62. MMRESULT mRet = waveInOpen(&hWaveIn, WAVE_MAPPER, &waveform, (DWORD_PTR)callback, (DWORD_PTR)user, CALLBACK_FUNCTION);
  63. waveInPrepareHeader(hWaveIn, &whdr_i1, sizeof(WAVEHDR));//准备buffer
  64. waveInPrepareHeader(hWaveIn, &whdr_i2, sizeof(WAVEHDR));//准备buffer
  65. waveInAddBuffer(hWaveIn, &whdr_i1, sizeof(WAVEHDR)); //添加buffer
  66. waveInAddBuffer(hWaveIn, &whdr_i2, sizeof(WAVEHDR)); //添加buffer
  67. waveInStart(hWaveIn);
  68. getchar();
  69. recurr = FALSE;
  70. //waveInStop(hWaveIn);
  71. waveInReset(hWaveIn);
  72. waveInClose(hWaveIn);
  73. HANDLE wait = CreateEvent(NULL, 0, 0, NULL);
  74. waveOutOpen(&hWaveOut, WAVE_MAPPER, &waveform, (DWORD_PTR)wait, 0L, CALLBACK_EVENT);
  75. // 播放录音
  76. whdr_o.lpData = (LPSTR)file; // 指向buffer
  77. whdr_o.dwBufferLength = hasRecorded; // buffer大小
  78. whdr_o.dwBytesRecorded = hasRecorded;
  79. whdr_o.dwFlags = 0;
  80. whdr_o.dwLoops = 1;
  81. ResetEvent(wait);
  82. waveOutPrepareHeader(hWaveOut, &whdr_o, sizeof(WAVEHDR));
  83. waveOutWrite(hWaveOut, &whdr_o, sizeof(WAVEHDR));
  84. /*Sleep(5000);*/
  85. DWORD dw = WaitForSingleObject(wait, INFINITE);
  86. if (dw == WAIT_OBJECT_0)
  87. {
  88. std::cout << "jieshu" << std::endl;
  89. return 0;
  90. }
  91. }
  92. void CALLBACK callback(HWAVEIN hwi, // 设备句柄
  93. UINT uMsg, // 消息
  94. DWORD_PTR dwInstance, // 对象
  95. DWORD_PTR dwParam1, // 参数1
  96. DWORD_PTR dwParam2) // 参数2
  97. {
  98. // 获取对象
  99. USER* user2 = (USER*)dwInstance;
  100. // 获取音频头
  101. PWAVEHDR pwhdr = (PWAVEHDR)dwParam1;
  102. // 处理消息
  103. switch (uMsg)
  104. {
  105. case WIM_OPEN: // 打开录音设备
  106. printf("成功打开设备..\n");
  107. break;
  108. case WIM_DATA: // 缓冲区已满
  109. {
  110. printf("缓冲池已满..\n");
  111. printf("a:%d , b:%c \n",user2->a,user2->b);
  112. // 缓冲池信息
  113. DWORD buflen = pwhdr->dwBufferLength;
  114. DWORD bytrecd = pwhdr->dwBytesRecorded;
  115. hasRecorded += bytrecd;
  116. // 缓冲扩增
  117. file = (BYTE*)realloc(file, hasRecorded * sizeof(BYTE));
  118. // 存储新内容
  119. if (file)
  120. {
  121. memcpy(&file[hasRecorded-bytrecd], pwhdr->lpData, bytrecd);
  122. printf("已存储:%d byte\n",hasRecorded);
  123. }
  124. // 循环
  125. if (recurr)
  126. {
  127. // 加入缓存
  128. waveInAddBuffer(hwi, pwhdr, sizeof(WAVEHDR));
  129. }
  130. }
  131. break;
  132. case WIM_CLOSE: // 关闭录音设备
  133. {
  134. printf("停止录音..\n");
  135. }
  136. break;
  137. default:
  138. break;
  139. }
  140. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/742509
推荐阅读
相关标签
  

闽ICP备14008679号