I had a program in Cc++ that using usb audio card to record some data. It work's perfectly under Windows XP. Now I have to move it to Windows7.
Win7 doesn't get any data from that card so I installed the ASIO driver to solve this problem. So I tried to get some data with Cooledit Pro and it works. But in my program the input data is corrupted.
I'm using fllowing function to get some data:
void __fastcall AudioIn :: onMessage(TMessage & message) {
if(message.Msg == MM_WIM_DATA && ! Terminated) {
if(callback)
callback(((WAVEHDR *)message.LParam) -> lpData,
((WAVEHDR *)message.LParam) -> dwBytesRecorded / 2);}
Unfortunately ((WAVEHDR *)message.LParam) -> lpData consist something like ЂЂЂЂЂЂЂЂЂЂЂЂЂЂЂЂЂЂЂЂЂЂЂЂЂЂЂЂЂЃ. Where is my mistake?
Adding additional code:
1) Found my usb device
for(int i = 0; deviceId == -1 && i < devNum; i ++)
if(AudioDev :: getDevCaps(i).UpperCase().Pos(L"USB"))
deviceId = i;
zvvi -> initDevice(deviceId);
2) Init this device
initDevice(int deviceId) {
if(wiState != wisClosed) closeDevice();
wiDevice = deviceId;
thread = new AudioIn(true);
thread -> FreeOnTerminate = true;
thread -> setCallback(callback);
thread -> Start();}
3)Prepare WAVEHDR
void __fastcall AudioIn :: prepareHeaders(int bCount, int bSize) {
if(prepared)
return;
prepared = true;
wiBuffCount = bCount;
wiBuffSize = bSize;
wavehdr = new WAVEHDR[wiBuffCount];
buff = new char *[wiBuffCount];
for(int i = 0; i < wiBuffCount; i ++)
buff[i] = new char[wiBuffSize];
for(int i = 0; i < wiBuffCount; i ++) {
wavehdr[i].lpData = (char *)buff[i];
wavehdr[i].dwBufferLength = wiBuffSize * sizeof(* buff[i]);
wavehdr[i].dwLoops = 0;
wavehdr[i].dwFlags = 0;
check(waveInPrepareHeader(wiHandle, & wavehdr[i], sizeof(wavehdr[i])), L"wiPrepareHeader");
}
}
4)Also i have function that configuring device
openDevice() {
if(wiState != wisClosed)
return;
const samplerate = 44100;
tWAVEFORMATEX format;
format.wFormatTag = WAVE_FORMAT_PCM;
format.nChannels = 1;
format.nSamplesPerSec = samplerate;
format.wBitsPerSample = 8;
format.nBlockAlign = (format.nChannels * format.wBitsPerSample) / 8;
format.nAvgBytesPerSec = (format.wBitsPerSample / 8) * samplerate;
format.cbSize = 0;
5) Started thread where my function get data "AudioIn :: onMessage"
Related
I am currently making a small discord bot that can play music to improve my skill. That's why i don't use any discord lib.
I want the music as smooth as possible, but when i played some piece of music, the music produced is very choppy.
here is my code:
concurrency::task<void> play(std::string id) {
auto shared_token = std::make_shared<concurrency::cancellation_token*>(&p_token);
auto shared_running = std::make_shared<bool*>(&running);
return concurrency::create_task([this, id, shared_token] {
audio* source = new audio(id); // create a s16le binary stream using FFMPEG
speak(); // sending speak packet
printf("creating opus encoder\n");
const unsigned short FRAME_MILLIS = 20;
const unsigned short FRAME_SIZE = 960;
const unsigned short SAMPLE_RATE = 48000;
const unsigned short CHANNELS = 2;
const unsigned int BITRATE = 64000;
#define MAX_PACKET_SIZE FRAME_SIZE * 5
int error;
OpusEncoder* encoder = opus_encoder_create(SAMPLE_RATE, CHANNELS, OPUS_APPLICATION_AUDIO, &error);
if (error < 0) {
throw "failed to create opus encoder: " + std::string(opus_strerror(error));
}
error = opus_encoder_ctl(encoder, OPUS_SET_BITRATE(BITRATE));
if (error < 0) {
throw "failed to set bitrate for opus encoder: " + std::string(opus_strerror(error));
}
if (sodium_init() == -1) {
throw "libsodium initialisation failed";
}
int num_opus_bytes;
unsigned char* pcm_data = new unsigned char[FRAME_SIZE * CHANNELS * 2];
opus_int16* in_data;
std::vector<unsigned char> opus_data(MAX_PACKET_SIZE);
class timer_event {
bool is_set = false;
public:
bool get_is_set() { return is_set; };
void set() { is_set = true; };
void unset() { is_set = false; };
};
timer_event* run_timer = new timer_event();
run_timer->set();
//this is the send loop
concurrency::create_task([run_timer, this, shared_token] {
while (run_timer->get_is_set()) {
speak();
int i = 0;
while (i < 15) {
utils::sleep(1000);
if (run_timer->get_is_set() == false) {
std::cout << "Stop sending speak packet due to turn off\n";
concurrency::cancel_current_task();
return;
}
if ((*shared_token)->is_canceled()) {
std::cout << "Stop sending speak packet due to cancel\n";
concurrency::cancel_current_task();
return;
}
}
}});
std::deque<std::string>* buffer = new std::deque<std::string>();
auto timer = concurrency::create_task([run_timer, this, buffer, FRAME_MILLIS, shared_token] {
while (run_timer->get_is_set() || buffer->size() > 0) {
utils::sleep(5 * FRAME_MILLIS); //std::this_thread::sleep_for
int loop = 0;
int sent = 0;
auto start = boost::chrono::high_resolution_clock::now();
while (buffer->size() > 0) {
if (udpclient.send(buffer->front()) != 0) { //send frame
//udpclient.send ~ winsock sendto
std::cout << "Stop sendding voice data due to udp error\n";
return;
}
buffer->pop_front();
if ((*shared_token)->is_canceled()) {
std::cout << "Stop sending voice data due to cancel\n";
concurrency::cancel_current_task();
}
sent++; //count sent frame
//calculate next time point we should (in theory) send next frame and store in *delay*
long long next_time = (long long)(sent+1) * (long long)(FRAME_MILLIS) * 1000 ;
auto now = boost::chrono::high_resolution_clock::now();
long long mcs_elapsed = (boost::chrono::duration_cast<boost::chrono::microseconds>(now - start)).count(); // elapsed time from start loop
long long delay = std::max((long long)0, (next_time - mcs_elapsed));
//wait for next time point
boost::asio::deadline_timer timer(context_io);
timer.expires_from_now(boost::posix_time::microseconds(delay));
timer.wait();
}
}
});
unsigned short _sequence = 0;
unsigned int _timestamp = 0;
while (1) {
if (buffer->size() >= 50) {
utils::sleep(FRAME_MILLIS);
}
if (source->read((char*)pcm_data, FRAME_SIZE * CHANNELS * 2) != true)
break;
if ((*shared_token)->is_canceled()) {
std::cout << "Stop encoding due to cancel\n";
break;
}
in_data = (opus_int16*)pcm_data;
num_opus_bytes = opus_encode(encoder, in_data, FRAME_SIZE, opus_data.data(), MAX_PACKET_SIZE);
if (num_opus_bytes <= 0) {
throw "failed to encode frame: " + std::string(opus_strerror(num_opus_bytes));
}
opus_data.resize(num_opus_bytes);
std::vector<unsigned char> packet(12 + opus_data.size() + crypto_secretbox_MACBYTES);
packet[0] = 0x80; //Type
packet[1] = 0x78; //Version
packet[2] = _sequence >> 8; //Sequence
packet[3] = (unsigned char)_sequence;
packet[4] = _timestamp >> 24; //Timestamp
packet[5] = _timestamp >> 16;
packet[6] = _timestamp >> 8;
packet[7] = _timestamp;
packet[8] = (unsigned char)(ssrc >> 24); //SSRC
packet[9] = (unsigned char)(ssrc >> 16);
packet[10] = (unsigned char)(ssrc >> 8);
packet[11] = (unsigned char)ssrc;
_sequence++;
_timestamp += SAMPLE_RATE / 1000 * FRAME_MILLIS; //48000Hz / 1000 * 20(ms)
unsigned char nonce[crypto_secretbox_NONCEBYTES];
memset(nonce, 0, crypto_secretbox_NONCEBYTES);
for (int i = 0; i < 12; i++) {
nonce[i] = packet[i];
}
crypto_secretbox_easy(packet.data() + 12, opus_data.data(), opus_data.size(), nonce, key.data());
packet.resize(12 + opus_data.size() + crypto_secretbox_MACBYTES);
std::string msg;
msg.resize(packet.size(), '\0');
for (unsigned int i = 0; i < packet.size(); i++) {
msg[i] = packet[i];
}
buffer->push_back(msg);
}
run_timer->unset();
timer.wait();
unspeak();
delete run_timer;
delete buffer;
opus_encoder_destroy(encoder);
delete[] pcm_data;
});
}
There are 3 possible causes:
I send packet late so server-end buffer run out, so the sound produced has some silence between each each 2 packets. Maybe the timer is not accurate so the sound is out of sync.
The encode process is wrong which causes lost data somehow.
Bad network (i have tested an open source bot written on java, it worked so i can assume that my network is good enough)
So i post this question, hope someone has experienced this situation show me what wrong and what should i do to correct it.
I figured out the problem myself. I want to post solution here for someone who need.
The problem is the timer is unstable so it's usually sleep more than it should, so it makes the music broken.
I changed it to an accurate sleep function which i found somewhere on the internet(i don't remember the source, sorry for that, if you know it please credit it bellow).
Function source code:
#include <math.h>
#include <chrono>
#include <window.h>
static void timerSleep(double seconds) {
using namespace std::chrono;
static HANDLE timer = CreateWaitableTimer(NULL, FALSE, NULL);
static double estimate = 5e-3;
static double mean = 5e-3;
static double m2 = 0;
static int64_t count = 1;
while (seconds - estimate > 1e-7) {
double toWait = seconds - estimate;
LARGE_INTEGER due;
due.QuadPart = -int64_t(toWait * 1e7);
auto start = high_resolution_clock::now();
SetWaitableTimerEx(timer, &due, 0, NULL, NULL, NULL, 0);
WaitForSingleObject(timer, INFINITE);
auto end = high_resolution_clock::now();
double observed = (end - start).count() / 1e9;
seconds -= observed;
++count;
double error = observed - toWait;
double delta = error - mean;
mean += delta / count;
m2 += delta * (error - mean);
double stddev = sqrt(m2 / (count - 1));
estimate = mean + stddev;
}
// spin lock
auto start = high_resolution_clock::now();
while ((high_resolution_clock::now() - start).count() / 1e9 < seconds);
}
Thank you for your support!
This function changes active frame of image after some time
But when i try to get frame count of the dimension it sets image->lasterror to Win32Error
Is there is any way to fix it?
(Windows 10,Visual Studio 2017 community)
void PlayImageAnim(Gdiplus::Image*&image, int delay,bool Looped)
{
using namespace Gdiplus;
if (Looped == true)
{
while (true)
{
UINT dcount = 0;
GUID *dimensionsIDs;
dcount = image->GetFrameDimensionsCount();
dimensionsIDs = new GUID[dcount];
UINT frame_count = image->GetFrameCount(&dimensionsIDs[0]);
int y = 0;
GUID pageGUID = FrameDimensionTime;
int size = image->GetPropertyItemSize(PropertyTagFrameDelay);
Gdiplus::PropertyItem*pr_item = (Gdiplus::PropertyItem*)malloc(size);
for (UINT i = 0; i <= frame_count; i++)
{
/*graphics.DrawImage(image, image_rect);*/
/*long delay = ((long*)pr_item->value)[i] * 10;*/
image->SelectActiveFrame(&pageGUID, i);
std::this_thread::sleep_for(std::chrono::milliseconds(delay));
}
image->SelectActiveFrame(&pageGUID, 0);
}
}
else
{
UINT dcount = 0;
GUID *dimensionsIDs;
dcount = image->GetFrameDimensionsCount();
dimensionsIDs = new GUID[dcount];
UINT frame_count = image->GetFrameCount(&dimensionsIDs[0]);
GUID pageGUID = FrameDimensionTime;
int size = image->GetPropertyItemSize(PropertyTagFrameDelay);
Gdiplus::PropertyItem*pr_item = (Gdiplus::PropertyItem*)malloc(size);
for (UINT i = 0; i <= frame_count; i++)
{
image->SelectActiveFrame(&pageGUID, i);
std::this_thread::sleep_for(std::chrono::milliseconds(delay));
}
image->SelectActiveFrame(&pageGUID, 0);
}
}
it has to similar parts for other parts of project
I'm currently working on a project that involves bringing information back from devices plugged in via true usb. To solve this, I'm attempting to use libusb in my C++ project. I'm on Windows 10, using C++ and QT Creator with QT version 5.2.1. I can get the vendor ID and product ID of my device with .idVendor and idProduct. However, my serial number is returning as 0 with .iSerialNumber. I'll post my code below and hopefully someone can help me figure out what I'm missing or suggest another library for me to try. Thanks!
bool CPumpFinder::pollTrueUSB(boolean &deviceFound, Device &newDevice)
{
boolean usbFound = FALSE;
boolean accuCheckFound = FALSE;
AccuGuideCom *acc = new AccuGuideCom();
libusb_device **devs; //pointer to pointer of device, used to retrieve a list of devices
libusb_context *ctx = NULL; //a libusb session
int r; //for return values
ssize_t cnt; //holding number of devices in list
r = libusb_init(&ctx); //initialize a library session
if(r < 0) {
return 1;
}
libusb_set_debug(ctx, 3); //set verbosity level to 3, as suggested in the documentation
cnt = libusb_get_device_list(ctx, &devs); //get the list of devices
if(cnt < 0) {
}
ssize_t i; //for iterating through the list
for(i = 0; i < cnt; i++) {
printdev(devs[i], usbFound, accuCheckFound); //print specs of this device
if(usbFound == TRUE)
{
if(accuCheckFound == TRUE)
{
newDevice.displayName = "ACCU-CHEK GUIDE";
newDevice.type = DEVICE_ACCUCHECKGUIDE;
newDevice.vendorID = accuVID;
newDevice.productID = accuPID;
newDevice.serial = accuSerial;
}
deviceFound = TRUE;
break;
}
}
libusb_free_device_list(devs, 1); //free the list, unref the devices in it
libusb_exit(ctx); //close the session
return deviceFound;
}
boolean CPumpFinder::printdev(libusb_device *dev, boolean &usbFound, boolean
&accuChekFound) {
libusb_device_descriptor desc;
int r = libusb_get_device_descriptor(dev, &desc);
if (r < 0) {
return usbFound;
}
uint16 vID = (uint16)desc.idVendor;
uint16 pID = (uint16)desc.idProduct; //desc.iSerialNumber
//#define ACCUCHEK_GUIDE_VENDORID (uint16)0x173A
//#define ACCUCHEK_GUIDE_PRODUCTID (uint16)0x21D5
if((vID == 5946) & (pID == 8661))
{
accuPID = pID;
accuVID = vID;
//Do serial?
accuSerial = "Test";
write_text_to_log_file("we did it");
accuChekFound = TRUE;
usbFound = TRUE;
}
if(usbFound == TRUE)
{
libusb_config_descriptor *config;
libusb_get_config_descriptor(dev, 0, &config);
const libusb_interface *inter;
const libusb_interface_descriptor *interdesc;
const libusb_endpoint_descriptor *epdesc;
for(int i=0; i<(int)config->bNumInterfaces; i++) {
inter = &config->interface[i];
for(int j=0; j<inter->num_altsetting; j++) {
interdesc = &inter->altsetting[j];
for(int k=0; k<(int)interdesc->bNumEndpoints; k++) {
epdesc = &interdesc->endpoint[k];
}
}
}
libusb_free_config_descriptor(config);
if(accuChekFound == TRUE)
{
return accuChekFound;
}
return usbFound;
}
return usbFound;
}
I'm developing a project where I need to convert PCM 16-bits 2 channels sound into a IEEE Float 32-bits 2 channels.
To do this I'm using the following code:
void CAudioConverter::ConvI16ToF32(BYTE* pcmFrom, BYTE* floatTo, int length)
{
short* src = reinterpret_cast<short*>(pcmFrom);
float* dst = reinterpret_cast<float*>(floatTo);
for (int n = 0; n < length; n++)
{
dst[n] = static_cast<float>(src[n]) / 32768.0f;
}
}
I have initialized the variable __pcm32_bytesPerFrame with:
WAVEFORMATEX* closestFormat;
ws->default_pb_dev->GetMixFormat(&closestFormat);
__pcm32_bytesPerFrame = closestFormat->nAvgBytesPerSec * (prm->samples_per_frame * 1000 / (prm->clock_rate * closestFormat->nChannels)) / 1000;
strm->pb_max_frame_count is:
hr = ws->default_pb_dev->GetBufferSize(&ws->pb_max_frame_count);
I have a while loop in a dedicated thread the does something like:
hr = strm->default_pb_dev->GetCurrentPadding(&padding);
incoming_frame = __pcm32_bytesPerFrame / 4;
frame_to_render = strm->pb_max_frame_count - padding;
if (frame_to_render >= incoming_frame)
{
frame_to_render = incoming_frame;
} else {
/* Don't get new frame because there's no space */
frame_to_render = 0;
}
if (frame_to_render > 0)
{
pjmedia_frame frame;
hr = strm->pb_client->GetBuffer(frame_to_render, &cur_pb_buf);
if (FAILED(hr)) {
continue;
}
void* destBuffer = (void*)malloc(strm->bytes_per_frame*frame_to_render*sizeof(pj_uint16_t));
if (strm->fmt_id == PJMEDIA_FORMAT_L16) {
/* PCM mode */
frame.type = PJMEDIA_FRAME_TYPE_AUDIO;
frame.size = strm->bytes_per_frame;
frame.timestamp.u64 = strm->pb_timestamp.u64;
frame.bit_info = 0;
frame.buf = destBuffer;
}
status = (*strm->pb_cb)(strm->user_data, &frame);
CAudioConverter* conv = new CAudioConverter();
conv->ConvI16ToF32((BYTE*)destBuffer, cur_pb_buf, frame_to_render);
hr = strm->pb_client->ReleaseBuffer(frame_to_render, 0);
(...)
But, to send the sound to the WASAPI capture buffer I need a BYTE*.
How can I fill my 'floatTo' argument?
Any ideas?
Thanks
What about this:
void CAudioConverter::ConvI16ToF32(BYTE* pcmFrom, BYTE* floatTo, int length)
{
short* src = reinterpret_cast<short*>(pcmFrom);
float* dst = reinterpret_cast<float*>(floatTo);
for (int n = 0; n < length; n++)
{
dst[n] = static_cast<float>(src[n]) / 32768.0f;
}
}
Additionally make sure length indicates the number of elments in pcmFrom and floatTo, and not the number of bytes allocated. In you case pcmFrom should have allocated length*2 bytes and floatTo needs room for length*4 bytes.
I am searching for a way to dynamically find the system path for HID (USB) devices.
On my Ubuntu machine it's /dev/usb/hiddevX, but I suppose there are distros where hiddevices get mounted elsewhere.
To be more specific: I need this in a C program to open a HID device - this should work on every system, no matter where the devices are mounted.
Current function:
int openDevice(int vendorId, int productId) {
char devicePath[24];
unsigned int numIterations = 10, i, handle;
for (i = 0; i < numIterations; i++) {
sprintf(devicePath, "/dev/usb/hiddev%d", i);
if((handle = open(devicePath, O_RDONLY)) >= 0) {
if(isDesiredDevice(handle, vendorId, productId))
break;
close(handle);
}
}
if (i >= numIterations) {
ThrowException(Exception::TypeError(String::New("Could not find device")));
}
return handle;
}
It works well but I don't like the hardcoded /dev/usb/hiddev
Edit: It turned out that some other programmers use fallbacks to /dev/usb/hid/hiddevX and /dev/hiddevX, so I built in those fallbacks too.
Updated method:
/**
* Returns the correct handle (file descriptor) on success
*
* #param int vendorId
* #param int productId
* #return int
*/
int IO::openDevice(int vendorId, int productId) {
char devicePath[24];
const char *devicePaths[] = {
"/dev/usb/hiddev\%d",
"/dev/usb/hid/hiddev\%d",
"/dev/hiddev\%d",
NULL
};
unsigned int numIterations = 15, i, j, handle;
for (i = 0; devicePaths[i]; i++) {
for (j = 0; j < numIterations; j++) {
sprintf(devicePath, devicePaths[i], j);
if((handle = open(devicePath, O_RDONLY)) >= 0) {
if(isDesiredDevice(handle, vendorId, productId))
return handle;
close(handle);
}
}
};
ThrowException(Exception::Error(String::New("Couldn't find device!")));
return 0;
};