Writing multiple wav files from NAudio asioOut - c#

I'm trying to record 3-4 streams of audio and write them to separate wav files. I'm using NAudio's asioOut to capture the audio. At first I was just writing a 3 or 4 channel wav file. So I searched questions here and thought I founded an answer, but I still can't figure it out. Now I can write the separate wav files but they have multiple channels in each file.
channel_1.wav --------> has channel 1 and 2
channel_2.wav --------> has channel 1 and 3
channel_3.wav --------> has channel 2 and 3
I think there is a problem when I try to parse the GetAsInterleavedSamples().
This is my asioOut_AudioAvailable()
void asioOut_AudioAvailable(object sender, AsioAudioAvailableEventArgs e)
{
float[] samples = new float[2 * 44100 * GetUserSpecifiedChannelCount()];
samples = e.GetAsInterleavedSamples();
int offset = 0;
while (offset < samples.Length)
{
for (int n = 0; n < this.writers.Length; n++)
{
this.writers[n].WriteSamples(samples, offset, 2);
offset += 2;
}
}
}
I'm new at this so I'm just learning as a go, any help would be greatly appreciated.
Thanks.

If I understand you correctly, you only want to write one sample to each writer, so the third parameter for WriteSamples should be 1, and offset should be incremented by 1. You've probably copied the +=2 from an example that is dealing with 16 bit audio in a byte array.

Related

Why is the last byte of a WasapiLoopbackCapture sample always 3B,3C,3D,3E,BC,BC or BE

I am writing a program to display audio samples on a time/amplitude graph.
I want to use the real-time audio playing on the computer and am using WasapiLoopbackCapture from the CSCore library, but the data shows on my graph always seems erroneous.
when I try to read the audio stream buffer with a bit depth of 32, the last byte of the array written to debug in
Debug.WriteLine(BitConverter.ToString(_sample));
is always 3B,3C,3D,3E,BC,BC or BE
I also found that reading the buffer as 32 bit (capture.WaveFormat.BitsPerSample returns 32) appears to show completely erroneous amplitudes on the graph whereas manually setting a bit depth
of 16 and reading the byte array with ReadInt16BigEndian appears to represent each sample somewhat accurately.
capture = new WasapiLoopbackCapture();
_bitDepth = capture.WaveFormat.BitsPerSample;
_byteDepth = bitDepth / 8;
Debug.WriteLine(bitDepth);
capture.DataAvailable += (s, a) =>
{
//for loop where i steps through each sample (each sample is multiple bytes)
for(int i = 0; i < a.Buffer.Length; i += _byteDepth)
{
//creates new byte array with 4 bytes / 32 bits
var _sample = new byte[_byteDepth];
//copies 1 sample from the buffer into the _sample byte array
Buffer.BlockCopy(a.Buffer, i, _sample,0,_byteDepth);
Debug.WriteLine(BitConverter.ToString(_sample));
//reads the byte array to an int
var _intSample = BinaryPrimitives.ReadInt32BigEndian(_sample);
currentData.Add(_intSample);
}
};
WASAPI uses 32 bit floating point (float) as a sample format. It's not a 32 bit integer.

How to read file bytes from byte offset?

If I am given a .cmp file and a byte offset 0x598, how can I read a file from this offset?
I can ofcourse read file bytes like this
byte[] fileBytes = File.ReadAllBytes("upgradefile.cmp");
But how can I read it from byte offset 0x598
To explain a bit more, actually from this offset the actual data starts that I have to read and before this byte offset it is just header data, so basically I have to read file from that offset till end.
Try code like this:
using (BinaryReader reader = new BinaryReader(File.Open("upgradefile.cmp", FileMode.Open)))
{
long offset = 0x598;
if (reader.BaseStream.Length > offset)
{
reader.BaseStream.Seek(offset, SeekOrigin.Begin);
byte[]fileBytes = reader.ReadBytes((int) (reader.BaseStream.Length - offset));
}
}
If you are not familiar with Streams, Linq, or whatever, I have simplest solution for you:
Read entire file into memory (I hope you deal with small files):
byte[] fileBytes = File.ReadAllBytes("upgradefile.cmp");
Calculate how many bytes are present in array after given offset:
long startOffset = 0x598; // this is just hexadecimal representation for human, it can be decimal or whatever
long howManyBytesToRead = fileBytes.Length - startOffset;
Then just copy data to new array:
byte[] newArray = new byte[howManyBytesToRead];
long pos = 0;
for (int i = startOffset; i < fileBytes.Length; i++)
{
newArray[pos] = fileBytes[i];
pos = pos + 1;
}
If you understand how it works you can look at Array.Copy method in Microsoft documentation.
By not using ReadAllBytes.
Get a stream, move to potition, read rest of files.
You basically complain that a convenience method made to allow a one line read of a whole file is not what you want - ignoring that it is just that, a convenience method. The normal way to deal with files is opening them and using a Stream.

Audio file reading for FFT

first time asking though i have been visiting for some time.
Here's the problem:
I'm currently trying to isolate the base frequenciy of a signal contained in a WAVE data file with these properties:
PCM Audio Format i.e Liner Quantization
8000 Hz Sample Rate
16 Bits Per Sample
16000 Byte Rate
One Channel only there is no interleaving.
Getting the byte value:
System.IO.FileStream WaveFile = System.IO.File.OpenRead(#"c:\tet\fft.wav");
byte[] data = new byte[WaveFile.Length];
WaveFile.Read(data,0,Convert.ToInt32(WaveFile.Length));
Converting it to an Array of Doubles:
for (int i = 0; i < 32768; i++)//this is only for a relatively small chunk of the file
{
InReal[i] =BitConverter.ToDouble(data, (i + 1) * 8 + 44);
}
and finanly passing it to a Transform Function.
FFT FftObject = new FFT();
FftObject.Transform(InReal, InImg, 0, 32768, out outReal, out outImg, false);
Now the first question, as i understand the PCM values of the wav file should be in the boundaries of
-1 and 1, but when converting to Double i get this values:
2.65855908666825E-235
2.84104982662944E-285
-1.58613492930337E+235
-1.25617351166869E+264
1.58370933499389E-242
6.19284549187335E-245
-2.92969500042228E+254
-5.90042665390976E+226
3.11954507295188E-273
3.06831908609091E-217
NaN
2.77113146323761E-302
6.76597919848376E-306
-1.55843653898344E+291
These are the firs few of the array in those limits is the rest of array too.
My conclusion of this is that i have some sort of code malfunction but i can seem to be able to find it.
Any help would be appreciated.
And the second question, because i'm only providing real data to the FFT algorithm in the response vector should i expect only Real part data too??
Thank you very much.
I was finally able to find out what was going wrong it seems that i didn't accounted for the pulse code modulation of the signal in the data representation, and because i found many unanswered questions here on wave file preparing for Fourier transformation here is the code in a function that prepares the wave file.
public static Double[] prepare(String wavePath, out int SampleRate)
{
Double[] data;
byte[] wave;
byte[] sR= new byte[4];
System.IO.FileStream WaveFile = System.IO.File.OpenRead(wavePath);
wave = new byte[WaveFile.Length];
data = new Double[(wave.Length - 44) / 4];//shifting the headers out of the PCM data;
WaveFile.Read(wave,0,Convert.ToInt32(WaveFile.Length));//read the wave file into the wave variable
/***********Converting and PCM accounting***************/
for (int i = 0; i < data.Length - i * 4; i++)
{
data[i] = (BitConverter.ToInt32(wave, (1 + i) * 4)) / 65536.0;
//65536.0.0=2^n, n=bits per sample;
}
/**************assigning sample rate**********************/
for (int i = 24; i < 28; i++)
{
sR[i-24]= wave[i];
}
SampleRate = BitConverter.ToInt32(sR,0);
return data;
}
all you need to do now is to send the sample rate and the returned result to your FFT algorithm.
The code is not handled so do your own handling as needed.
I has been tested for phone recordings, of busy, ringing and speech, it functions correctly.

Processing Huge Files In C#

I have a 4Gb file that I want to perform a byte based find and replace on. I have written a simple program to do it but it takes far too long (90 minutes+) to do just one find and replace. A few hex editors I have tried can perform the task in under 3 minutes and don't load the entire target file into memory. Does anyone know a method where I can accomplish the same thing? Here is my current code:
public int ReplaceBytes(string File, byte[] Find, byte[] Replace)
{
var Stream = new FileStream(File, FileMode.Open, FileAccess.ReadWrite);
int FindPoint = 0;
int Results = 0;
for (long i = 0; i < Stream.Length; i++)
{
if (Find[FindPoint] == Stream.ReadByte())
{
FindPoint++;
if (FindPoint > Find.Length - 1)
{
Results++;
FindPoint = 0;
Stream.Seek(-Find.Length, SeekOrigin.Current);
Stream.Write(Replace, 0, Replace.Length);
}
}
else
{
FindPoint = 0;
}
}
Stream.Close();
return Results;
}
Find and Replace are relatively small compared with the 4Gb "File" by the way. I can easily see why my algorithm is slow but I am not sure how I could do it better.
Part of the problem may be that you're reading the stream one byte at a time. Try reading larger chunks and doing a replace on those. I'd start with about 8kb and then test with some larger or smaller chunks to see what gives you the best performance.
There are lots of better algorithms for finding a substring in a string (which is basically what you are doing)
Start here:
http://en.wikipedia.org/wiki/String_searching_algorithm
The gist of them is that you can skip a lot of bytes by analyzing your substring. Here's a simple example
4GB File starts with: A B C D E F G H I J K L M N O P
Your substring is: N O P
You skip the length of the substring-1 and check against the last byte, so compare C to P
It doesn't match, so the substring is not the first 3 bytes
Also, C isn't in the substring at all, so you can skip 3 more bytes (len of substring)
Compare F to P, doesn't match, F isn't in substring, skip 3
Compare I to P, etc, etc
If you match, go backwards. If the character doesn't match, but is in the substring, then you have to do some more comparing at that point (read the link for details)
Instead of reading file byte by byte read it by buffer:
buffer = new byte[bufferSize];
currentPos = 0;
length = (int)Stream .Length;
while ((count = Stream.Read(buffer, currentPos, bufferSize)) > 0)
{
currentPos += count;
....
}
Another, easier way of reading more than one byte at a time:
var Stream = new BufferedStream(new FileStream(File, FileMode.Open, FileAccess.ReadWrite));
Combining this with Saeed Amiri's example of how to read into a buffer, and one of the better binary find/replace algorithms should give you better results.
You should try using memory-mapped files. C# supports them starting with version 4.0.
A memory-mapped file contains the contents of a file in virtual memory.
Persisted files are memory-mapped files that are associated with a source file on a disk. When the last process has finished working with the file, the data is saved to the source file on the disk. These memory-mapped files are suitable for working with extremely large source files.

how to split wave signal into frames

I'm working a project about chord recognition. I'm using someone's journal as a reference but I still have little grasp in field of DSP. In her reference, first thing is I need to split the signal from wav file into number of frames. In my case, I need to split into 65 ms each frame, with 2866 sample per frame.
I have searched how to split signal into frames but I don't find them clear enough for me to understand.
So far these are some of my codes in WavProcessing class:
public void SetFileName(String fileNameWithPath) //called first in the form, to get the FileStream
{
_fileNameWithPath = fileNameWithPath;
strm = File.OpenRead(_fileNameWithPath);
}
public double getLengthTime(uint wavSize, uint sampleRate, int bitRate, int channels)
{
wavTimeLength = ((strm.Length - 44) / (sampleRate * (bitRate / 8))) / channels;
return wavTimeLength;
}
public int getNumberOfFrames() //return number of frames, I just divided total length time with interval time between frames. (in my case, 3000ms / 65 ms = 46 frames)
{
numOfFrames = (int) (wavTimeLength * 1000 / _sampleFrameTime);
return numOfFrames;
}
public int getSamplePerFrame(UInt32 sampleRate, int sampleFrameTime) // return the sample per frame value (in my case, it's 2866)
{
_sampleRate = sampleRate;
_sampleFrameTime = sampleFrameTime;
sFr = (int)(sampleRate * (sampleFrameTime / 1000.0 ));
return sFr;
}
I just still don't get the idea how to split the signal into 65 ms per frame in C#.
Do I need to split the FileStream and break them into frames and save them into array? Or anything else?
with NAudio you would do it like this:
using (var reader = new AudioFileReader("myfile.wav"))
{
float[] sampleBuffer = new float[2866];
int samplesRead = reader.Read(sampleBuffer, 0, sampleBuffer.Length);
}
As others have commented, the number of samples you read ought to be a power of 2 if you plan to pass it into an FFT. Also, if the file is stereo, you will have left and right samples interleaved, so your FFT will need to be able to cope with this.

Categories