Advertisement
Guest User

Untitled

a guest
May 26th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.54 KB | None | 0 0
  1. // labview3.cpp : Defines the exported functions for the DLL application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <mmsystem.h>
  6. #include <fstream>
  7. #include <algorithm>
  8. #include <vector>
  9. #include <filesystem>
  10. #include <string>
  11. namespace fs = std::filesystem;
  12.  
  13. //HWAVEIN      hWaveIn;
  14. //WAVEHDR      WaveInHdr;
  15. //MMRESULT result;
  16. //
  17. //WAVEFORMATEX format;
  18.  
  19. //int waveIn[3000];
  20. //bool go = true;
  21. int k = 0;
  22. const int numpts = 44100;
  23. int sampleRate = 44100;
  24. short int waveIn[numpts];
  25. short int buffer[numpts * 3600];
  26. size_t sizeofWaveIn;
  27.  
  28. HWAVEIN hWaveIn;
  29. WAVEHDR WaveInHdr;
  30. MMRESULT result;
  31. int recordTime = 5;
  32. int __declspec(dllexport)
  33. startRecording() {
  34.  
  35.     MMTIME mmTime = { 0 };
  36.     mmTime.wType = TIME_BYTES;
  37.     bool error = false;
  38.  
  39.     WAVEFORMATEX format;
  40.  
  41.     format.wFormatTag = WAVE_FORMAT_PCM;
  42.     format.nChannels = 1;
  43.     format.nSamplesPerSec = sampleRate;
  44.     format.nAvgBytesPerSec = sampleRate * 2;
  45.     format.nBlockAlign = 2;
  46.     format.wBitsPerSample = 16;
  47.     format.cbSize = 0;
  48.  
  49.     result = waveInOpen(&hWaveIn, WAVE_MAPPER, &format, 0L, 0L, WAVE_FORMAT_DIRECT);
  50.    
  51.     if (result) {
  52.         return -1;
  53.     }
  54.  
  55.     sizeofWaveIn = sizeof(waveIn);
  56.     WaveInHdr.lpData = (LPSTR)waveIn;
  57.     WaveInHdr.dwBufferLength = sizeofWaveIn;
  58.     WaveInHdr.dwBytesRecorded = 0;
  59.     WaveInHdr.dwUser = 0L;
  60.     WaveInHdr.dwFlags = 0L;
  61.     WaveInHdr.dwLoops = 0L;
  62.  
  63.     waveInPrepareHeader(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));
  64.     result = waveInAddBuffer(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));
  65.     if (result) {
  66.         return -1;
  67.     }
  68.  
  69.     result = waveInStart(hWaveIn);
  70.     if (result)
  71.  
  72.     {
  73.  
  74.         return -1;
  75.  
  76.     }
  77.     while (k < recordTime) {
  78.         if (waveInUnprepareHeader(hWaveIn, &WaveInHdr, sizeof(WAVEHDR)) != WAVERR_STILLPLAYING) {
  79.             memcpy(buffer + (k * numpts), waveIn, (numpts) * sizeof(waveIn[0]));
  80.             waveInGetPosition(hWaveIn, &mmTime, sizeof(mmTime));
  81.             waveInReset(hWaveIn);
  82.             waveInUnprepareHeader(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));
  83.             waveInPrepareHeader(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));
  84.             result = waveInAddBuffer(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));
  85.             result = waveInStart(hWaveIn);
  86.             k++;
  87.         }
  88.        
  89.     }
  90.     //std::ofstream  log("E:/labviewdll/labview3/log.txt");
  91.     //format.wFormatTag = WAVE_FORMAT_PCM;
  92.     //format.nChannels = 2;
  93.     //format.nSamplesPerSec = 44100;
  94.     //format.wBitsPerSample = 16;
  95.     //format.nBlockAlign = format.wBitsPerSample* format.nChannels / 8;
  96.     //format.nAvgBytesPerSec = format.nBlockAlign * format.nSamplesPerSec;
  97.     //result = waveInOpen(&hWaveIn, WAVE_MAPPER, &format, 0L, 0L, WAVE_FORMAT_DIRECT);
  98.  
  99.     //log.write("enter", 6);
  100.     //HWAVEIN wi;
  101.     //waveInOpen(&wi,            // fill our 'wi' handle
  102.     //  WAVE_MAPPER,    // use default device (easiest)
  103.     //  &format,           // tell it our format
  104.     //  NULL, NULL,      // we don't need a callback for this example
  105.     //  CALLBACK_NULL | WAVE_FORMAT_DIRECT   // tell it we do not need a callback
  106.     //);
  107.  
  108.     //char buffers[2][44100 * 2 * 2 / 2];
  109.     //WAVEHDR headers[2] = { {}, {} };
  110.     //for (int i = 0; i < 2; i++) {
  111.     //  headers[i].lpData = buffers[i];
  112.     //  headers[i].dwBufferLength = 44100 * 2 * 2 / 2;
  113.     //  waveInPrepareHeader(wi, &headers[i], sizeof(headers[i]));
  114.  
  115.     //  waveInAddBuffer(wi, &headers[i], sizeof(headers[i]));
  116.     // 
  117.     //}
  118.     //log.write("enter+header+addbuffer", 23);
  119.     //std::string filename = "E:/labviewdll/labview3/record3.bin";
  120.  // std::ofstream outfile(filename, std::ios_base::out | std::ios_base::binary);
  121.     //outfile.open(filename);
  122.     //waveInStart(wi);
  123.     //int i = 0;
  124.     //while (i < 10) {
  125.     //  for (auto& h : headers) {
  126.     //      if (h.dwFlags & WHDR_DONE) {
  127.     //          log.write("enteredhere",12);
  128.     //          outfile.write(h.lpData, h.dwBufferLength);
  129.     //          h.dwFlags = 0;
  130.     //          h.dwBytesRecorded = 0;
  131.     //          waveInPrepareHeader(wi, &h, sizeof(h));
  132.     //          waveInAddBuffer(wi, &h, sizeof(h));
  133.     //      }
  134.     //  }
  135.     //  Sleep(1000);
  136.     //  i++;
  137.     //}
  138.  
  139.     //waveInStop(wi);
  140.     //log.write("stop", 4);
  141.     //for (auto& h : headers) {
  142.     //  waveInUnprepareHeader(wi, &h, sizeof(h));
  143.     //}
  144.     //waveInClose(wi);
  145.     //return 1;
  146.  
  147. }
  148.  
  149. int __declspec(dllexport)
  150. stopRecording() {
  151.     waveInUnprepareHeader(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));
  152.     waveInClose(hWaveIn);
  153.     WaveInHdr.lpData = (LPSTR)buffer;
  154.     WaveInHdr.dwBufferLength = (k * sizeofWaveIn);
  155.  
  156.     int bitsPerSample = 16;
  157.     int subchunk1size = 16;
  158.     int nrChannels = 1;
  159.     int subchunk2size = WaveInHdr.dwBufferLength*nrChannels;
  160.     int chunksize = 36 + subchunk2size;
  161.     int audioFormat = 1;
  162.     int byteRate = sampleRate * nrChannels*bitsPerSample / 8;
  163.     int blockAlign = nrChannels * bitsPerSample / 8;
  164.     std::fstream out("E:/labviewdll/labview3/record3.wav", std::ios::out | std::ios::binary);
  165.     out.seekp(0, std::ios::beg);
  166.     out.write("RIFF", 4);
  167.     out.write((char*)&chunksize, 4);
  168.     out.write("WAVE", 4);
  169.     out.write("fmt", 4);
  170.     out.write((char*)&subchunk1size, 4);
  171.     out.write((char*)&audioFormat, 2);
  172.     out.write((char*)&nrChannels, 2);
  173.     out.write((char*)&sampleRate, 4);
  174.     out.write((char*)&byteRate, 4);
  175.     out.write((char*)&blockAlign, 2);
  176.     out.write((char*)&bitsPerSample, 2);
  177.     out.write("data", 4);
  178.     out.write((char*)&subchunk2size, 4);
  179.     out.write((char *)buffer, WaveInHdr.dwBufferLength);
  180.     return 1;
  181.  
  182. }
  183.  
  184. int __declspec(dllexport)
  185. getFileList(char* c) {
  186.     std::vector<std::string> files;
  187.     std::string path = "E:/labviewdll/labview3";
  188.     std::string finalStr;
  189.     int nr = 0;
  190.     for (const auto &entry : fs::directory_iterator(path)) {
  191.        
  192.         fs::path path = entry.path().filename();
  193.         std::string stuff = path.u8string();
  194.         if (strstr(stuff.c_str(), ".wav") ) {
  195.             nr++;
  196.             finalStr.append("\n");
  197.             finalStr.append(stuff);
  198.            
  199.         }
  200.     }
  201.     strcpy(c, finalStr.c_str());
  202.     return strlen(c);
  203.    
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement