mp3 to sample array NAudio - mp3

How can I use NAudio library to get sample float array from mp3 file?
Here is my code:
float[] buffer = new float[2000];
AudioFileReader reader = new AudioFileReader(filePath);
reader.Read(buffer, 0, 2000);
After that buffer is always empty (only zeros inside).
You could also provide me another useful library in C# to realize this.

You're reading the first 2000 samples which is only going to be around 20ms of audio, so it's quite possible that your MP3 starts with a bit of silence. Have you tried reading further into the file?

Related

Convert Raw to Wav Streams in NodeJS

I am using a nodeJS library naudio —link— to record sound from a 2 microphones (total 4 channel audio with each microphone being stereo). This library spits out a .raw file in the following specs: 16 BIT, 48000Hz Sample Rate, Channel Count 4
// var portAudio = require('../index.js');
var portAudio = require('naudiodon');
var fs = require('fs');
//Create a new instance of Audio Input, which is a ReadableStream
var ai = new portAudio.AudioInput({
channelCount: 4,
sampleFormat: portAudio.SampleFormat16Bit,
sampleRate: 48000,
deviceId: 13
});
ai.on('error', console.error);
//Create a write stream to write out to a raw audio file
var ws = fs.createWriteStream('rawAudio_final.raw');
//Start streaming
ai.pipe(ws);
ai.start();
process.once('SIGINT', ai.quit);
Instead of the .raw file, I am trying to convert this to two individual .wav files. With the above encoding and information, what would be the best way to do so? I tried to dig around for easy ways to deinterleaving and getting .wav but seem to be hitting a wall.
The addon is a wrapper around a C++ library called portaudio which according to its documentation supports writing to a WAV file.
What you could do is extend the addon and bind a NodeJS function to the underlying C++ function that write to WAV.
This will give you a good performance if it is an issue.
If you want something easier you could look up utilities that do the conversion and call them from within your script using ex like this
Look similar to this question.
You may also take a look here to know how to create wav file from javascript.

Sending Binary File Data via Google Protobuf

I have my protobuf-message set up fine it seems, all other fields I have transmit correctly across the network and do not truncate. I only have one problem, when I read the binary data of a picture or file then send it through google protobuf as bytes array type, on the other side it only contains the first 4 elements of the array. If the picture is say 200kb, on the other end it comes out as 1kb(Basically only contains a header or identifier). This problem is kinda complex so I will try to give a run down. Sorry if I make this impossible to understand. I may be going about this completely the wrong way.
Example below contains conceptual work, and was written in class. It very well could contain small errors. The code compiles at home, and if it is a typo let me know and I can fix it.
FILE* file;
FILE* ofile;
file = fopen("red.png", "rb");
fseek(file, 0, SEEK_END);
long fSize = ftell(file);
rewind(file);
BYTE* ret = new BYTE[fSize];
fread(ret, 1, fSize, file);
fclose(file);
char dataStream[1024] //yes it is large enough
myPacket.set_file(ret);
//set other fields here
myPacket.SerializeToArray(dataStream,sizeof(dataStream));
//send through sockets below, works for all but file field.
I can include more when I get back home to my main work computer, sorry, was just hoping I could let this stew while at class. If this is not enough info feel free to give me the smack down, it's alright just looking for advice. I also know that certain image formats can be read certain ways, but I was able to copy a png and rewrite it through binary locally, just not over protobuf
Thanks for reading my pseudo book guys, I am finally trying to leap into improving my knowledge.
Edited quickly typed pointer error(&ret) to (ret). Also then should size of be sizeof(myPacket) rather.
You have written this:
char dataStream[1024] //yes it is large enough
But how could 1024 bytes buffer be large enough if you want to store 200 000 bytes into it?
Better allocate a bigger buffer on the heap, e.g.:
std::vector<char> dataStream(500000);
myPacket.SerializeToArray(&dataStream[0], dataStream.size());

Can I write bytes into audio output using Qt?

There are examples on Qt website regarding using audio API, but frankly I don't really understand them at all.
What I was imagining is writing array of values (bytes, integers...) into some audio buffer and have the sound card "play" them (actually DAC them).
Pseudocode:
// Square wave?
const int values[] = {255,255,255,255, 0,0,0,0, 255,255,255,255 ...};
// Create output that will buffer the bytes and put them on digi to analog converter
RawAudioOutput output(BIT_RATE_CONSTANT, ... some other parameters ...);
output.start();
output.writeBytes(values, sizeof(values));
Can I accomplish something like that? How would I go about it? I know I can model square wave in Audacity (doesn't sound nice), so I guess it's possible. How?
In Qt, if you want to write an array of values into an audio buffer, the class for that is QAudioOutput. The format of the array of values can vary, the PCM format should be supported by all platforms.
Qt ships with an example that demonstrates the usage of QAudioOutput, have a look at that. In the example, the Generator::generateData() function creates the array of values that are then later sent to the audio device.
Of course playing audio from an array of values is quite low-level. With QMediaPlayer, Qt also provides a high-level class to play sound files (.wav, .mp3), video files and even streams.

MoSync - Edit video

After making a small video recorder application and being able to play that video again. I would like to make the possibility to pick X seconds from the video and put that into a new .MP4 file (or overwrite the old one, that would be even better).
I am using the MoSync C++ Native UI and VideoViewer. I know I can get the position and that part is all fine, and according to the MoSync documentation.
char buf[BUFFER_SIZE];
maWidgetGetProperty(videoViewHandle,
MAW_VIDEO_VIEW_CURRENT_POSITION,
buf,
BUFFER_SIZE);
int seconds = 5;
//So here I need to make a new file ranging from buf to buf + seconds
However, I have absolutely no clue as to where to look for this. Should I use mp4 header files and create my own mp4 (how is this even done? and is this cross-compatible?).
Will appreciate any advice/help you can offer!

converting a binary stream into a png format

I will try to be clear ....
My project idea is as follow :
I took several compression algorithms which I implemented using C++, after that I took a text file and applied to it the compression algorithms which I implemented, then applied several encryption algorithms on the compressed files, now I am left with final step which is converting these encrypted files to any format of image ( am thinking about png since its the clearest one ).
MY QUESTION IS :
How could I transform a binary stream into a png format ?
I know the image will look rubbish.
I want the binary stream to be converted to a an png format so I can view it as an image
I am using C++, hope some one out there can help me
( my previous thread which was closed )
https://stackoverflow.com/questions/5773638/converting-a-text-file-to-any-format-of-images-png-etc-c
thanx in advance
Help19
If you really really must store your data inside a PNG, it's better to use a 3rd party library like OpenCV to do the work for you. OpenCV will let you store your data and save it on the disk as PNG or any other format that it supports.
The code to do this would look something like this:
#include <cv.h>
#include <highgui.h>
IplImage* out_image = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, bits_pr_pixel);
char* buff = new char[width * height * bpp];
// then copy your data to this buff
out_image->imageData = buff;
if (!cvSaveImage("fake_picture.png", out_image))
{
std::cout << "ERROR: Failed cvSaveImage" << std::endl;
}
cvReleaseImage(&out_image);
The code above it's just to give you an idea on how to do what you need using OpenCV.
I think you're better served with a bi-dimensional bar code instead of converting your blob of data into a png image.
One of the codes that you could use is the QR code.
To do what you have in mind (storing data in an image), you'll need a lossless image format. PNG is a good choice for this. libpng is the official PNG encoding library. It's written in C, so you should be able to easily interface it with your C++ code. The homepage I linked you to contains links to both the source code so you can compile libpng into your project as well as a manual on how to use it. A few quick notes on using libpng:
It uses setjmp and longjmp for error handling. It's a little weird if you haven't worked with C's long jump functionality before, but the manual provides a few good examples.
It uses zlib for compression, so you'll also have to compile that into your project.