Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // labview3.cpp : Defines the exported functions for the DLL application.
- //
- #include "stdafx.h"
- #include <mmsystem.h>
- #include <fstream>
- #include <algorithm>
- #include <vector>
- #include <filesystem>
- #include <string>
- namespace fs = std::filesystem;
- //HWAVEIN hWaveIn;
- //WAVEHDR WaveInHdr;
- //MMRESULT result;
- //
- //WAVEFORMATEX format;
- //int waveIn[3000];
- //bool go = true;
- int k = 0;
- const int numpts = 44100;
- int sampleRate = 44100;
- short int waveIn[numpts];
- short int buffer[numpts * 3600];
- size_t sizeofWaveIn;
- HWAVEIN hWaveIn;
- WAVEHDR WaveInHdr;
- MMRESULT result;
- int recordTime = 5;
- int __declspec(dllexport)
- startRecording() {
- MMTIME mmTime = { 0 };
- mmTime.wType = TIME_BYTES;
- bool error = false;
- WAVEFORMATEX format;
- format.wFormatTag = WAVE_FORMAT_PCM;
- format.nChannels = 1;
- format.nSamplesPerSec = sampleRate;
- format.nAvgBytesPerSec = sampleRate * 2;
- format.nBlockAlign = 2;
- format.wBitsPerSample = 16;
- format.cbSize = 0;
- result = waveInOpen(&hWaveIn, WAVE_MAPPER, &format, 0L, 0L, WAVE_FORMAT_DIRECT);
- if (result) {
- return -1;
- }
- sizeofWaveIn = sizeof(waveIn);
- WaveInHdr.lpData = (LPSTR)waveIn;
- WaveInHdr.dwBufferLength = sizeofWaveIn;
- WaveInHdr.dwBytesRecorded = 0;
- WaveInHdr.dwUser = 0L;
- WaveInHdr.dwFlags = 0L;
- WaveInHdr.dwLoops = 0L;
- waveInPrepareHeader(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));
- result = waveInAddBuffer(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));
- if (result) {
- return -1;
- }
- result = waveInStart(hWaveIn);
- if (result)
- {
- return -1;
- }
- while (k < recordTime) {
- if (waveInUnprepareHeader(hWaveIn, &WaveInHdr, sizeof(WAVEHDR)) != WAVERR_STILLPLAYING) {
- memcpy(buffer + (k * numpts), waveIn, (numpts) * sizeof(waveIn[0]));
- waveInGetPosition(hWaveIn, &mmTime, sizeof(mmTime));
- waveInReset(hWaveIn);
- waveInUnprepareHeader(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));
- waveInPrepareHeader(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));
- result = waveInAddBuffer(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));
- result = waveInStart(hWaveIn);
- k++;
- }
- }
- //std::ofstream log("E:/labviewdll/labview3/log.txt");
- //format.wFormatTag = WAVE_FORMAT_PCM;
- //format.nChannels = 2;
- //format.nSamplesPerSec = 44100;
- //format.wBitsPerSample = 16;
- //format.nBlockAlign = format.wBitsPerSample* format.nChannels / 8;
- //format.nAvgBytesPerSec = format.nBlockAlign * format.nSamplesPerSec;
- //result = waveInOpen(&hWaveIn, WAVE_MAPPER, &format, 0L, 0L, WAVE_FORMAT_DIRECT);
- //log.write("enter", 6);
- //HWAVEIN wi;
- //waveInOpen(&wi, // fill our 'wi' handle
- // WAVE_MAPPER, // use default device (easiest)
- // &format, // tell it our format
- // NULL, NULL, // we don't need a callback for this example
- // CALLBACK_NULL | WAVE_FORMAT_DIRECT // tell it we do not need a callback
- //);
- //char buffers[2][44100 * 2 * 2 / 2];
- //WAVEHDR headers[2] = { {}, {} };
- //for (int i = 0; i < 2; i++) {
- // headers[i].lpData = buffers[i];
- // headers[i].dwBufferLength = 44100 * 2 * 2 / 2;
- // waveInPrepareHeader(wi, &headers[i], sizeof(headers[i]));
- // waveInAddBuffer(wi, &headers[i], sizeof(headers[i]));
- //
- //}
- //log.write("enter+header+addbuffer", 23);
- //std::string filename = "E:/labviewdll/labview3/record3.bin";
- // std::ofstream outfile(filename, std::ios_base::out | std::ios_base::binary);
- //outfile.open(filename);
- //waveInStart(wi);
- //int i = 0;
- //while (i < 10) {
- // for (auto& h : headers) {
- // if (h.dwFlags & WHDR_DONE) {
- // log.write("enteredhere",12);
- // outfile.write(h.lpData, h.dwBufferLength);
- // h.dwFlags = 0;
- // h.dwBytesRecorded = 0;
- // waveInPrepareHeader(wi, &h, sizeof(h));
- // waveInAddBuffer(wi, &h, sizeof(h));
- // }
- // }
- // Sleep(1000);
- // i++;
- //}
- //waveInStop(wi);
- //log.write("stop", 4);
- //for (auto& h : headers) {
- // waveInUnprepareHeader(wi, &h, sizeof(h));
- //}
- //waveInClose(wi);
- //return 1;
- }
- int __declspec(dllexport)
- stopRecording() {
- waveInUnprepareHeader(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));
- waveInClose(hWaveIn);
- WaveInHdr.lpData = (LPSTR)buffer;
- WaveInHdr.dwBufferLength = (k * sizeofWaveIn);
- int bitsPerSample = 16;
- int subchunk1size = 16;
- int nrChannels = 1;
- int subchunk2size = WaveInHdr.dwBufferLength*nrChannels;
- int chunksize = 36 + subchunk2size;
- int audioFormat = 1;
- int byteRate = sampleRate * nrChannels*bitsPerSample / 8;
- int blockAlign = nrChannels * bitsPerSample / 8;
- std::fstream out("E:/labviewdll/labview3/record3.wav", std::ios::out | std::ios::binary);
- out.seekp(0, std::ios::beg);
- out.write("RIFF", 4);
- out.write((char*)&chunksize, 4);
- out.write("WAVE", 4);
- out.write("fmt", 4);
- out.write((char*)&subchunk1size, 4);
- out.write((char*)&audioFormat, 2);
- out.write((char*)&nrChannels, 2);
- out.write((char*)&sampleRate, 4);
- out.write((char*)&byteRate, 4);
- out.write((char*)&blockAlign, 2);
- out.write((char*)&bitsPerSample, 2);
- out.write("data", 4);
- out.write((char*)&subchunk2size, 4);
- out.write((char *)buffer, WaveInHdr.dwBufferLength);
- return 1;
- }
- int __declspec(dllexport)
- getFileList(char* c) {
- std::vector<std::string> files;
- std::string path = "E:/labviewdll/labview3";
- std::string finalStr;
- int nr = 0;
- for (const auto &entry : fs::directory_iterator(path)) {
- fs::path path = entry.path().filename();
- std::string stuff = path.u8string();
- if (strstr(stuff.c_str(), ".wav") ) {
- nr++;
- finalStr.append("\n");
- finalStr.append(stuff);
- }
- }
- strcpy(c, finalStr.c_str());
- return strlen(c);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement