I am trying to store the data of a wav file in an array of ints so far this is what I have
int BufferSize = 4096;//CW.NumSampels;
int* buffer = new int[BufferSize];
FILE InFile = fopen(pParent->m_StrWavFileName, "r");
size_t bytesRead = fread(&Riff, 1, sizeof(RiffWav), InFile);
Does this store all the data of a wav file into the buffer array of size 4096?
Related
I'm trying to transmitt a draco encoded point cloud via socket.io.
In the first iteration everything is fine but in every following iteration abort() is being called, because I get an out of range exception in _NODISCARD inline int stoi.
for (int i = 0; i < count; i++)
{
//get pointcloud from kinect source
cwipc* capturedPC = ts->get();
cwipcH.CopyPointsToStruct(capturedPC);
//create draco point cloud from captured pointcloud (this throws the out of range exception on the second iteration)
std::unique_ptr<draco::PointCloud> pc = dracoH.BuildPointCloud(capturedPC, cwipcH);
//Write the encoder buffer with position quantization, compression speed and decompression speed
eBuff = dracoH.GetEncoderBuffer(move(pc), 13, 10, 10);
std::cout << "Buffer size " << eBuff.size() << "\n";
//copy encoder buffer to new char*
size_t size = eBuff.size();
const char* eBuffData = eBuff.data();
char* eBuffCpy = new char[size];
strncpy_s(eBuffCpy, sizeof(eBuffData), eBuffData, size);
//create shared pointer for pointcloud payload
std::shared_ptr<std::string> payload = std::make_shared<std::string>(eBuffData,size);
//transmitt payload
client.socket()->emit("Encoder Buffer", payload);
//free memory
capturedPC->free();
eBuff.Clear();
payload.reset();
pc.reset();
}
I've tried copying the encoder Buffer to a new char*, but the error still persits.
After I have defined and filled the buffer from binary .exe data --
unsigned char *buffer ; /*buffer*/
buffer = malloc(300) ; /*allocate space on heap*/
fread(buffer, 300, 1, file) ;
Then how do I get bytes at position 121--124 of buffer
as a long value?
I have tried
long Hint = 0;
memcpy(Hint, buffer[121], 4);
printf("Hint=x%x\n", Hint);
but all I get is an abend on memcpy
Here is a simple way to do that (I put numbers in buffer for the example):
unsigned char *buffer ; /*buffer*/
buffer = (unsigned char*) malloc (300) ; /*allocate space on heap*/
for(int i=0;i<300;i++) /*initialize buffer with numbers for the demo*/
buffer[i] = i;
long Hint = 0;
long *h = (long *)&buffer[121];
Hint = *h;
printf("Hint=0x%x\n", Hint);
The output for this will be:
Hint=0x7c7b7a79
Which is the numbers 121-124 in hex.
I am working on C++ code to read and write .bmp image.
Below is my code.
However, I encountered some problems that I couldn't fix. enter image description here
I have googled a lot but none of them solve my problem.
Sorry if my coding style doesn't look good to you, I'm new to Xcode and C++.
Please help me.
I will be really appreciated.
#include <iostream>
#include <stdio.h>
#pragma pack(2)
typedef struct // BMP file header structure
{
unsigned short bfType ; // Magic number for file
unsigned int bfSize ; // Size of file
unsigned short bfReserved1 ; // Reserved, usually set to 0
unsigned short bfReserved2 ; // Reserved, usually set to 0
unsigned int bfoffBits ; // Offset to bitmap data
}BITMAPFILEHEADER;
#pragma pack()
typedef struct
{
unsigned int biSize ; // Size of info header
int biWidth ; // Width of image
int biHeight ; // Height of image
unsigned short biPlanes ; // Number of color planes
unsigned short biBitCount ; // Number of bits per pixel
unsigned int biCompression ; // Type of compression to use, 0 if there is no compression
unsigned int biSizeImage ; // Size of image data
int biXPelsPerMeter ; // X pixels per meter
int biYPelsPerMeter ; // Y pixels per meter
unsigned int biClrUsed ; // Number of color used
unsigned int biClrImportant ; // Number of important color
}BITMAPINFOHEADER;
unsigned char *ReadBitmapFile(const char *filename, BITMAPINFOHEADER *bitmapInfoHeader)
{
FILE* file ; //file pointer
BITMAPFILEHEADER bitmapFileHeader ; //bitmap file header
unsigned char *bitmapimage ; //store image data
int imageIdx = 0 ;
unsigned char tempRGB ; //swap
// open file in read binary mode
file = fopen(filename, "rb");
if (file == NULL)
return NULL;
// read the bitmap file header
fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, file);
// read the bitmap info header
fread(bitmapInfoHeader, sizeof(BITMAPINFOHEADER),1,file);
//move file point to the begging of bitmap data
fseek(file, bitmapFileHeader.bfoffBits, SEEK_SET);
//allocate enough memory for the bitmap image data
bitmapimage = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage);
//verify memory allocation
if(!bitmapimage)
{
free(bitmapimage);
fclose(file);
return NULL;
}
// read in the bitmap image data
fread(bitmapimage, bitmapInfoHeader->biSizeImage, 1, file);
//swap the r and b value to get RGB (bitmap is BGR)
for (imageIdx = 0; imageIdx < bitmapInfoHeader->biSizeImage; imageIdx+=3){
tempRGB = bitmapimage[imageIdx];
bitmapimage[imageIdx] = bitmapimage[imageIdx + 2];
bitmapimage[imageIdx + 2] = tempRGB;
}
//close file and return bitmap data
fclose(file);
return bitmapimage;
}
BITMAPINFOHEADER bitmapInfoHeader;
unsigned char *bitmapData = ReadBitmapFile("input1.bmp", &bitmapInfoHeader);
By the way, I am using Xcode8.3.3
I want to convert a PNG image found in a path to base64 for a html page in Windows phone7.1.How can it be done?
Stream imgStream;
imgStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("NewUIChanges.Htmlfile.round1.png");
byte[] data = new byte[(int)imgStream.Length];
int offset = 0;
while (offset < data.Length)
{
int bytesRead = imgStream.Read(data, offset, data.Length - offset);
if (bytesRead <= 0)
{
throw new EndOfStreamException("Stream wasn't as long as it claimed");
}
offset += bytesRead;
}
The fact that it's a PNG image is actually irrelevant - all you need to know is that you've got some bytes that you need to convert into base64.
Read the data from a stream into a byte array, and then use Convert.ToBase64String. Reading a byte array from a stream can be slightly fiddly, depending on whether the stream advertises its length or not. If it does, you can use:
byte[] data = new byte[(int) stream.Length];
int offset = 0;
while (offset < data.Length)
{
int bytesRead = stream.Read(data, offset, data.Length - offset);
if (bytesRead <= 0)
{
throw new EndOfStreamException("Stream wasn't as long as it claimed");
}
offset += bytesRead;
}
If it doesn't, the simplest approach is probably to copy it to a MemoryStream:
using (MemoryStream ms = new MemoryStream())
{
byte[] buffer = new byte[8 * 1024];
int bytesRead;
while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, bytesRead);
}
return ms.ToByteArray();
}
So once you've used either of those bits of code (or anything else suitable) to get a byte array, just use Convert.ToBase64String and you're away.
There are probably streaming solutions which will avoid ever having the whole byte array in memory - e.g. building up a StringBuilder of base64 data as it goes - but they would be more complicated. Unless you're going to deal with very large files, I'd stick with the above.
I am converting some custom files that I have into hadoop Sequence Files using the Java API.
I am reading byte arrays from a local file and append them to a sequence file as pairs of Index (Integer) - Data (Byte[]):
InputStream in = new BufferedInputStream(new FileInputStream(localSource));
FileSystem fs = FileSystem.get(URI.create(hDFSDestinationDirectory),conf);
Path sequenceFilePath = new Path(hDFSDestinationDirectory + "/"+ "data.seq");
IntWritable key = new IntWritable();
BytesWritable value = new BytesWritable();
SequenceFile.Writer writer = SequenceFile.createWriter(fs, conf,
sequenceFilePath, key.getClass(), value.getClass());
for (int i = 1; i <= nz; i++) {
byte[] imageData = new byte[nx * ny * 2];
in.read(imageData);
key.set(i);
value.set(imageData, 0, imageData.length);
writer.append(key, value);
}
IOUtils.closeStream(writer);
in.close();
I do exactly the opposite when I want to bring the files back to the initial format:
for (int i = 1; i <= nz; i++) {
reader.next(key, value);
int byteLength = value.getLength();
byte[] tempValue = value.getBytes();
out.write(tempValue, 0, byteLength);
out.flush();
}
I noticed that writting to SequenceFile takes almost an order of magnitude more than reading. I expect writting to be slower than reading but is this difference normal? Why?
More Info:
The byte arrays I read are 2MB size (nx=ny=1024 and nz=128)
I am testing in pseudo-distributed mode.
You are reading from local disk and writing to HDFS. When you write to HDFS your data is probably being replicated so it is physically written two or three times depending on what you have set for the replication factor.
So you are not only writing but writing two or three times the amount of data you are reading. And your writes are going over the network. Your reads are not.
Are nx and ny constants?
One reason you could be seeing this is that each iteration of your for loop creates a new byte array. This requires the JVM to allocate you some heap space. If the array is sufficiently large, this is going to be expensive, and eventually you're going to run into the GC. I'm not too sure on what HotSpot might do to optimize this out however.
My suggestion would be to create a single BytesWritable:
// use DataInputStream so you can call readFully()
DataInputStream in = new DataInputStream(new FileInputStream(localSource));
FileSystem fs = FileSystem.get(URI.create(hDFSDestinationDirectory),conf);
Path sequenceFilePath = new Path(hDFSDestinationDirectory + "/"+ "data.seq");
IntWritable key = new IntWritable();
// create a BytesWritable, which can hold the maximum possible number of bytes
BytesWritable value = new BytesWritable(new byte[maxPossibleSize]);
// grab a reference to the value's underlying byte array
byte byteBuf[] = value.getBytes();
SequenceFile.Writer writer = SequenceFile.createWriter(fs, conf,
sequenceFilePath, key.getClass(), value.getClass());
for (int i = 1; i <= nz; i++) {
// work out how many bytes to read - if this is a constant, move outside the for loop
int imageDataSize nx * ny * 2;
// read in bytes to the byte array
in.readFully(byteBuf, 0, imageDataSize);
key.set(i);
// set the actual number of bytes used in the BytesWritable object
value.setSize(imageDataSize);
writer.append(key, value);
}
IOUtils.closeStream(writer);
in.close();