How to use WAVEHDR - c

Where can I find information about what data should be in the lpData buffer for the WAVEHDR structure?
MSDN simply says:
lpData
Pointer to the waveform buffer.
typedef struct wavehdr_tag {
LPSTR lpData;
DWORD dwBufferLength;
DWORD dwBytesRecorded;
DWORD_PTR dwUser;
DWORD dwFlags;
DWORD dwLoops;
struct wavehdr_tag *lpNext;
DWORD_PTR reserved;
} WAVEHDR, *LPWAVEHDR;
Thanks

I found this tutorial by David Overton very helpful.
Basically, when you call waveOutOpen, you pass in a format structure. Here's from his code:
WAVEFORMATEX wfx; /* look this up in your documentation */
wfx.nSamplesPerSec = 44100; /* sample rate */
wfx.wBitsPerSample = 16; /* sample size */
wfx.nChannels = 2; /* channels*/
Then your data in lpData is just 2 bytes per sample (signed short int), left, right, left, etc.

lpData is like an old DOS DMA buffer.
So you can write a piece of track on It like a single block loop.
So in C you declare some proper array ...char myarray[porpersize].
and then you point it ->>> myhdrstruc.lpData=&myarray[0]

CCRMA has a decent overview of the wave file format.

It's vague because the data can be in a variety for formats. The format is typically specified by a WAVEFORMATEX.

Related

need support utilizing FM_1808(F-RAM Memory) for the data extraction using structure pointers

I am working on field oriented control for PMSM. I'm using customized hardware, and unfortunetly i can't see the speed responses and current signal straight on the oscilliscope through pins. So i need to save data in FM_1808(F-RAM Memory) and then later uses FM_1808(F-RAM Memory) to plot the signals. I'm using structure to keep all data in one function with the pointer, pointing to that function. unfortunetly i can't utilized the FRAM quite well and unable to extract the data. i need help to find where i'm doing wrong that make problem in extracting the data. thanks your help will be highly appreciated. I have attached some lines of code for where i define structure, starting address and function to extract data.
define.c
#pragma DATA_SECTION(FRAMAdr,"FRAMData");
volatile struct FRAMAddress FRAMAdr;
linker cmd
page1
{
EXTFRAM : origin = 0x200000, length = 0x008000
}
Sections
{
FRAMData: > EXTFRAM PAGE = 1
}
define.h
struct FRAMAddress {
int16 Dat[32768]; // FRAM 16bit data & 16bit memory
};
extern volatile struct FRAMAddress FRAMAdr;
Uint16 *FRAMStartAdr = (Uint16 *)0x200000;
pheripheral.c
void FSave(Uint16 FAdr, int16 FD1, int16 FD2)
{
FRamAdr.Dat[FAdr] = FD1;
DELAY_US(0.2L);
FRamAdr.Dat[FAdr+5000] = FD2;
}
main.c
extern void FSave(Uint16 FAdr1, int16 FD1, int16 FD2);
{
if(FIndex<=5000)
{
FSave(FIndex, (int16)spd, (int16)(id));
FIndex++;
}
else
{
FSaveflag=0;
}
}
i would like to seek help regarding data value at memory addresses using FM_1808(F-RAM Memory).
I suspect you probably want something like this, assuming your FRAM is mapped at address 0x200000.
struct FRAM {
uint16_t Dat[32768];
};
struct FRAM * FRAMData = (struct FRAM *)0x200000;
void FSave(Uint16 FAdr, int16 FD1, int16 FD2)
{
FRAMData->Dat[FAdr] = FD1;
DELAY_US(0.2L);
FRAMData->Dat[FAdr+5000] = FD2;
}
Note that the datasheet I found online suggests the FM1808 has 32K 8-bit values, not 16-bit values, so you should look into that.
Edit: Also not sure your delay is needed or makes sense. What is the type of 0.2L? What parameter type does DELAY_US() take?

How to merge/feed two uint8_t buffers?

Hey sorry for this novice question but I think im just missing something obvious... Would be very happy with the some guidance on this:
Inline docu of esp_camera.h:
/**
* #brief Data structure of camera frame buffer
*/
typedef struct {
uint8_t * buf; /*!< Pointer to the pixel data */
size_t len; /*!< Length of the buffer in bytes */
size_t width; /*!< Width of the buffer in pixels */
size_t height; /*!< Height of the buffer in pixels */
pixformat_t format; /*!< Format of the pixel data */
} camera_fb_t;
plus extract of demo code:
from esp32 code:
//replace this with your own function
display_image(fb->width, fb->height, fb->pixformat, fb->buf, fb->len);
code getting framebuffer
camera_fb_t * fb = NULL;
esp_err_t res = ESP_OK;
fb = esp_camera_fb_get(); // framebuffer in grayscale
and feed fb buffer into imagebuffer
int w, h;
int i, count;
uint8_t *imagebuffer = quirc_begin(qr, &w, &h);
//Feed 'fb' into 'imagebuffer' somehow?
//-------------------------------
// ----- DUMMY CODE?! not the proper way? ----
imagebuffer = fb->buf; //fb's own buf field, holding the pixel data
//Comment from quirc below:
/* Fill out the image buffer here.
* 'imagebuffer' is a pointer to a w*h bytes.
* One byte per pixel, w pixels per line, h lines in the buffer.
*/
//
quirc_end(qr);
Inline comment docu of quirc below:
/* These functions are used to process images for QR-code recognition.
* quirc_begin() must first be called to obtain access to a buffer into
* which the input image should be placed. Optionally, the current
* width and height may be returned.
*
* After filling the buffer, quirc_end() should be called to process
* the image for QR-code recognition. The locations and content of each
* code may be obtained using accessor functions described below.
*/
uint8_t *quirc_begin(struct quirc *q, int *w, int *h);
void quirc_end(struct quirc *q);
https://github.com/dlbeer/quirc
I've looked through the code, source files etc, but as i'm a novice ive no clue how to merge or feed the one into the other.
Could anyone point me into the right direction here? Am not dirty of looking through heaps of code but my inexperience with C is the issue here :S Thanks!
Author of the library was kind enough to explain it,
posting the code answer here as it may help others:
int w, h;
int i, count;
uint8_t *buff = quirc_begin(qr, &w, &h);
//
int total_pixels = w * h;
for (int i = 0; i < total_pixels; i++) {
// grab a pixel from your source image at element i
// convert it somehow, then store it
buff[i] = fb->buf[i]; //?
}
//
quirc_end(qr);
count = quirc_count(qr);
Serial.println("count found codes:");
Serial.println(count);
github issue with lib author's explaination

OVERLAPPED STRUCTURES and LARGE_INTEGER

I working through exercises from Windows System Programming and I'm not fully comprehending LARGE_INTEGER and OVERLAPPED Structures. For example I have the following Structures defined in main. The first structure is used to keep track of the number of records. The second is used for the record data. The author defines and uses two overlapped structure to keep track of the record file offset.
typedef struct _HEADER {
DWORD numRecords;
DWORD numNonEmptyRecords;
} HEADER; /* 8bytes */
typedef struct _RECORD {
DWORD referenceCount;
SYSTEMTIME recordCreationTime;
SYSTEMTIME recordLastRefernceTime;
SYSTEMTIME recordUpdateTime;
TCHAR dataString[STRING_SIZE];
} RECORD; /* 308bytes */
LARGE_INTEGER currentPtr;
OVERLAPPED ov = {0, 0, 0, 0, NULL}, ovZero = {0, 0, 0, 0, NULL};
After the records are created. The user can be prompted Read, Write, or Delete a record. The record entered by the user is stored in recNo.
currentPtr.QuadPart = (LONGLONG)recNo * sizeof(RECORD) + sizeof(HEADER);
ov.Offset = currentPtr.LowPart;
ov.OffsetHigh = currentPtr.HighPart;
Can someone please explain how the values for the LARGE_INTEGR currentPtr are calculated? What is a Union? I have looked at the example in windbg and I don't understand how the currentPtr.LowPart and currentPtr.HighPart are calculated. Below is an example of file read operation being called with the OVERLAPPED Structure.
ReadFile (hFile, &record, sizeof (RECORD), &nXfer, &ov)
A union gives different names and types to the same location in memory. So if a LARGE_INTEGER union was stored at location 0x1000, and since X86 is little endian:
LARGE_INTEGER.QuadPart is 64 bit integer at 0x1000
LARGE_INTEGER.LowPart is the lower 32 bits of the 64 bit integer at 0x1000.
LARGE_INTEGER.HighPart is the upper 32 bits of the 64 bit integer at 0x1004.
OVERLAPPED is used for asynchronous I/O. A read or write call in overlapped mode will return immediately, and the event specified in the OVERLAPPED structure will be signaled when the I/O completes.
MSDN article for OVERLAPPED structure:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms684342(v=vs.85).aspx
In 32 bit mode, Offset would share memory with Pointer, in 64 bit mode, Offset and OffsetHigh would share memory with Pointer. Offset and OffsetHigh are inputs, while Pointer is used internally. InternalHigh is poorly named since it now reports number of bytes transferred, and may change yet again. Internal is now a status.

In Win32, what does the size member (cb) name actually mean?

In quite a few Win32 structures you have to give the size of the structure to one of its members, in quite a few cases, the member that stores this is called cb or prefixed with this.
DISPLAY_DEVICE has a cb member for size.
It's also used in names of certain types of messages, such as CB_GETCURSEL. Perhaps in this case its for ComboBox.
In other places in Win32, the cb acronym (I assume?) is used as part of member names.
Such as WNDCLASS which have cbWndExtra and cbClsExtra.
In STARTUPINFO you have it:
typedef struct _STARTUPINFO {
DWORD cb;
LPTSTR lpReserved;
LPTSTR lpDesktop;
LPTSTR lpTitle;
DWORD dwX;
DWORD dwY;
DWORD dwXSize;
DWORD dwYSize;
DWORD dwXCountChars;
DWORD dwYCountChars;
DWORD dwFillAttribute;
DWORD dwFlags;
WORD wShowWindow;
WORD cbReserved2;
LPBYTE lpReserved2;
HANDLE hStdInput;
HANDLE hStdOutput;
HANDLE hStdError;
} STARTUPINFO, *LPSTARTUPINFO;
The documentation says this:
cb
The size of the structure, in bytes.
I wondered if anyone knows what cb stands for or means? If it does have meaning at all.
Perhaps someone knows the history of this, which may explain it.
It probably stands for count bytes.
For example in the STARTUPINFO it should be initialized by you to sizeof(STARTUPINFO). That way, the Windows internals will know which version of the struct you are using, as it has grown over the time.
In the other cases is simply a number of bytes.
Except in the ComboBoxes, there it stands for Combo Box.
cb in this case stands for
Count of bytes.
It's a Microsoft flavor of so called Hungarian notation. In case of cb it's a count of bytes, as other answers already mentioned.
IInspectable is very right! cb means count of bytes and cch means count of charactors. see StringCbPrintf and StringCchPrintf

Loading an 8bpp grayscale BMP in C

I can't make sense of the BMP format, I know its supposed to be simple, but somehow I'm missing something. I thought it was 2 headers followed by the actual bytes defining the image, but the numbers do not add up.
For instance, I'm simply trying to load this BMP file into memory (640x480 8bpp grayscale) and just write it back to a different file. From what I understand, there are two different headers BITMAPFILEHEADER and BITMAPINFOHEADER. The BITMAPFILEHEADER is 14 bytes, and the BITMAPINFOHEADER is 40 bytes (this one depends on the BMP, how can I tell that's another story). Anyhow, the BITMAPFILEHEADER, through its parameter bfOffBits says that the bitmap bits start at offset 1078. This means that there are 1024 ( 1078 - (40+14) ) other bytes, carrying more information. What are those bytes, and how do I read them, this is the problem. Or is there a more correct way to load a BMP and write it to disk ?
For reference here is the code I used ( I'm doing all of this under windows btw.)
#include <windows.h>
#include <iostream>
#include <stdio.h>
HANDLE hfile;
DWORD written;
BITMAPFILEHEADER bfh;
BITMAPINFOHEADER bih;
int main()
hfile = CreateFile("image.bmp",GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
ReadFile(hfile,&bfh,sizeof(bfh),&written,NULL);
ReadFile(hfile,&bih,sizeof(bih),&written,NULL);
int imagesize = bih.biWidth * bih.biHeight;
image = (unsigned char*) malloc(imagesize);
ReadFile(hfile,image,imagesize*sizeof(char),&written,NULL);
CloseHandle(hfile);
I'm then doing the exact opposite to write to a file,
hfile = CreateFile("imageout.bmp",GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
WriteFile(hfile,&bfh,sizeof(bfh),&written,NULL);
WriteFile(hfile,&bih,sizeof(bih),&written,NULL);
WriteFile(hfile,image,imagesize*sizeof(char),&written,NULL);
CloseHandle(hfile);
Edit --- Solved
Ok so I finally got it right, it wasn't really complicated after all. As Viktor pointed out, these 1024 bytes represent the color palette.
I added the following to my code:
RGBQUAD palette[256];
// [...] previous declarations [...] int main() [...] then read two headers
ReadFile(hfile,palette,sizeof(palette),&written,NULL);
And then when I write back I added the following,
WriteFile(hfile,palette,sizeof(palette),&written,NULL);
"What are those bytes, and how do I read them, this is the problem."
Those bytes are Palette (or ColorTable in .BMP format terms), as Retired Ninja mentioned in the comment. Basically, it is a table which specifies what color to use for each 8bpp value encountered in the bitmap data.
For greyscale the palette is trivial (I'm not talking about color models and RGB -> greyscale conversion):
for(int i = 0 ; i < 256 ; i++)
{
Palette[i].R = i;
Palette[i].G = i;
Palette[i].B = i;
}
However, there's some padding in the ColorTable's entries, so it takes 4 * 256 bytes and not 256 * 3 needed by you. The fourth component in the ColorTable's entry (RGBQUAD Struct) is not the "alpha channel", it is just something "reserved". See the MSDN on RGBQUAD (MSDN, RGBQUAD).
The detailed format description can be found on the wikipedia page:Wiki, bmp format
There's also this linked question on SO with RGBQUAD structure: Writing BMP image in pure c/c++ without other libraries
As Viktor says in his answer, those bits are the pallete. As for how should you read them, take a look at this header-only bitmap class. In particular look at references to ColorTable for how it treats the pallette bit depending on the type of BMP is it was given.

Resources