Get file descriptor descriptor from FileStream

I use this library https://github.com/ahawker/NLibsndfile to convert audio files from libSndFile from C #, I made this method to convert (hides wav to aiff):

string sourcePath = "/User/Dev/Desktop/a.wav"; string targetPath = "/User/Dev/Desktop/b.aiff"; var file = File.Create(targetPath); file.Close(); file.Dispose(); IntPtr ptrTargetFile = file.SafeFileHandle.DangerousGetHandle(); LibsndfileInfo wavInfo = new LibsndfileInfo(); LibsndfileInfo aiffInfo = new LibsndfileInfo(); LibsndfileApi api = new LibsndfileApi(); var wavFile = api.Open(sourcePath, LibsndfileMode.Read, ref wavInfo); var aiffFile = api.OpenFileDescriptor((int)ptrTargetFile, LibsndfileMode.Rdwr, ref aiffInfo, 0); aiffInfo.Channels = wavInfo.Channels; aiffInfo.SampleRate = wavInfo.SampleRate; aiffInfo.Format = LibsndfileFormat.Aiff; short[] buffer = new short[512]; long countOfItemsWritten = 0; long countOfItemsReaded = 0; while ((countOfItemsReaded=api.ReadItems(wavFile, buffer, buffer.Length))>0) countOfItemsWritten = api.WriteItems(aiffFile, buffer, countOfItemsReaded); api.Close(wavFile); api.Close(aiffFile); 

The problem is to open the aiff file, api.OpenFileDescriptor method always returns a null pointer. Library method: internal static extern IntPtr sf_open_fd(int handle, LibsndfileMode mode, ref LibsndfileInfo info, int closeHandle);

Any idea why the operation is not working? or how to get a file descriptor descriptor from a FileStream?

+4
source share
1 answer

You can get the handle as follows:

First use FileStream.SafeFileHandle and use the value of this to access SafeHandle.DangerousGetHandle()

So: IntPtr handle = fileStream.SafeFileHandle.DangerousGetHandle();

+4
source

All Articles