This project has moved. For the latest updates, please go here.

Error converting file using VS 2005 project to VS 2008 NAudio.dll

Nov 29, 2010 at 5:00 PM

Mark,

 I believe I discovered 2 bugs when compiling under “x86” and “Any CPU” as a DLL. I am using your NAudio to perform 2 functions:

  • Detect a file’s sound format
  • Convert the file to mu-law 8-bit, 8000K, mono

 My environment:

  • Microsoft Windows 7, 64-bit
  • Microsoft Visual Studio 2005 project in Visual Basic
  • Referencing NAudio.dll

 The situation:

  • File Detection
    • If I compile as “x86”, link the NAudio.dll to the Visual Studio 2005 project, then attempt to detect a sound file format I get an error: Could not load file or assembly 'NAudio, Version=1.3.8.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.
    • If I compile as “Any CPU”, link the NAudio.dll to the Visual Studio 2005 project, then attempt to detect a sound file format I get no error.
  • File Conversion
    • If I compile as “Any CPU”, link the NAudio.dll to the Visual Studio 2005 project, then attempt to convert a PCM file to mu-law I get an error: InvalidParameter calling acmStreamPrepareHeader
    • If I compile as “x86”, link the NAudio.dll to the Visual Studio 2005 project, then attempt to convert a PCM file to mu-law I get an error: Could not load file or assembly 'NAudio, Version=1.3.8.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.
  • The Situation
    • Your notes say to use “x86” for 64-bit processing but I get 2 errors.
    • Under “Any CPU” I get 1 error.
Coordinator
Nov 29, 2010 at 5:03 PM

you probably need to get a newer build of NAudio, as various improvements have been made for x64 support. Are you able to get the latest code out and build it (sln file is VS2010)

Mark

Nov 29, 2010 at 7:06 PM

Okay,

I have downloaded and am using the VS2010 codebase, and have set all of the builds to "Any CPU".  When attempting to convert PCM files to mu-law, I get mixed results:

  • Successful conversion to mu-law:
    • PCM 8000 Sample Rate, 16000 Bytes per second, 16 Bits per sample, 1 channel, 2 block align
  • Failed conversion to mu-law:
    • PCM 16000 Sample Rate, 32000 Bytes per second, 16 Bits per sample, 1 channel, 2 block align
    • PCM 48000 Sample Rate, 96000 Bytes per second, 16 Bits per sample, 1 channel, 2 block align
  • Error:  AcmNotPossible calling acmStreamOpen.  StackTrace:  at NAudio.MmException.Try(MmResult result, String function) in C:\DataTest\Sound\NAudio64\NAudio\Wave\MmeInterop\MmException.cs:line 40 at NAudio.Wave.Compression.AcmStream..ctor(WaveFormat sourceFormat, WaveFormat destFormat) in C:\DataTest\Sound\NAudio64\NAudio\Wave\Compression\AcmStream.cs:line 44 at NAudio.Wave.WaveFormatConversionStream..ctor(WaveFormat targetFormat, WaveStream sourceStream) in C:\DataTest\Sound\NAudio64\NAudio\Wave\WaveStreams\WaveFormatConversionStream.cs:line 43 at CallCommand.Utilities.SharedFunctions.App.Media.Naudios.Functions.ConvertWaveToMulaw(String InputFile, String OutputFile) in C:\DataSubversion\Utilities\SharedFunctions\clsMediaNaudio.vb:line 165
  • Code being used to convert to mu-law:
    • NAudio.Wave.WaveStream stream = new NAudio.Wave.WaveFileReader(InputFile);
    • NAudio.Wave.WaveFormat TargetFormat = NAudio.Wave.WaveFormat.CreateMuLawFormat(8000, 1);
    • using (NAudio.Wave.WaveStream ConvertedStream = new NAudio.Wave.WaveFormatConversionStream(TargetFormat, stream))
    • {
    • NAudio.Wave.WaveFileWriter.CreateWaveFile(OutputFile, ConvertedStream);
    • }
Coordinator
Nov 29, 2010 at 8:47 PM

the ACM mu-law codec can't do sample rate conversion. you have to do that as a separate step. One WaveFormatConversionStream to go to 8kHz 16 bit mono, then a second to convert to mu law

Mark

Nov 29, 2010 at 9:31 PM

Thanks Mark.  Would you be able to provide me that code piece (or something close) to get from a higher PCM format to the lower PCM format?  I am still learning your codebase.

John

Coordinator
Nov 29, 2010 at 9:49 PM

it's very similar to what you have already got, just an extra initial step (obviously you don't need it when the input stream is already 8kHz)

 

WaveFormat downsampled = new WaveFormat(8000, 1);

using (WaveStream ConvertedStream = new WaveFormatConversionStream(downsampled, stream)

Mark

Apr 5, 2011 at 3:17 PM

Can you provide a sample of your Visual Basic code? I am trying to use VS2010 for development. Thanks!

Apr 7, 2011 at 3:16 PM
I had to write in C#, do you still want it?



On Apr 5, 2011, at 10:17 AM, "webideas"<notifications@codeplex.com> wrote:

From: webideas

Can you provide a sample of your Visual Basic code? I am trying to use VS2010 for development. Thanks!

Apr 7, 2011 at 4:10 PM

Yes, if you do not mind sharing it.

What was the reason for changing the project from Visual Basic to C#?

Thanks!

Apr 12, 2011 at 1:43 PM

I was directed to migrate to the C# codebase. This is the core library to detect and convert and merge what we use so far. There are more audio formats I would like to add as time permits.

using System;

using System.Collections;

using System.Collections.Generic;

using System.Data;

using System.Diagnostics;

namespace CallCommand.Messaging.Voice.Audio

{

public class Formats

{

public enum enumSoundFileFormat

{

//-----Mu-law 8-bit 8000k 1 channel

esft_MuLaw8000k8bitMono

}

}

public class Functions

{

public static System.Boolean IsFileValidMuLawFormatForVoiceCalling(

string sFileAndPath)

{

try

{

//-----validate file exists

if (System.IO.File.Exists(sFileAndPath) == false)

{

return false;

}

//-----validate file format, and validate file length > 0

using (NAudio.Wave.WaveFileReader objFile = new NAudio.Wave.WaveFileReader(sFileAndPath))

{

if (objFile.WaveFormat.Encoding == NAudio.Wave.WaveFormatEncoding.MuLaw

&& objFile.WaveFormat.SampleRate == 8000

&& objFile.WaveFormat.AverageBytesPerSecond == 8000

&& objFile.WaveFormat.BitsPerSample == 8

&& objFile.WaveFormat.Channels == 1

&& objFile.WaveFormat.BlockAlign == 1

&& objFile.Length > 0)

{

return true;

}

}

//-----default

return false;

}

catch (Exception)

{

return false;

}

}

public static System.Data.DataTable GetAudioPropertiesLongFormat(

string sFileAndPath)

{

try

{

using (System.Data.DataTable dtResult = new System.Data.DataTable())

{

//-----build return data structure

dtResult.Columns.Add(ColumnLabel.FileName, typeof(string));

dtResult.Columns.Add(ColumnLabel.ValidDialing, typeof(string));

dtResult.Columns.Add(ColumnLabel.EncodingLabel, typeof(string));

dtResult.Columns.Add(ColumnLabel.EncodingID, typeof(string));

dtResult.Columns.Add(ColumnLabel.SampleRate, typeof(string));

dtResult.Columns.Add(ColumnLabel.BytesPerSecond, typeof(string));

dtResult.Columns.Add(ColumnLabel.BitsPerSample, typeof(string));

dtResult.Columns.Add(ColumnLabel.Channels, typeof(string));

dtResult.Columns.Add(ColumnLabel.BlockAlign, typeof(string));

dtResult.Columns.Add(ColumnLabel.FileLength, typeof(string));

//-----create row

System.Data.DataRow drNewRow = dtResult.NewRow();

drNewRow[ColumnLabel.FileName] = Convert.ToString(sFileAndPath + "");

//-----process file

try

{

using (NAudio.Wave.WaveFileReader objFile = new NAudio.Wave.WaveFileReader(sFileAndPath))

{

//-----add data

if (objFile.WaveFormat.Encoding == NAudio.Wave.WaveFormatEncoding.MuLaw

&& objFile.WaveFormat.SampleRate == 8000

&& objFile.WaveFormat.AverageBytesPerSecond == 8000

&& objFile.WaveFormat.BitsPerSample == 8

&& objFile.WaveFormat.Channels == 1

&& objFile.WaveFormat.BlockAlign == 1

&& objFile.Length > 0)

{

drNewRow[ColumnLabel.ValidDialing] = "True";

}

else

{

drNewRow[ColumnLabel.ValidDialing] = "False";

}

drNewRow[ColumnLabel.EncodingLabel] = Convert.ToString(objFile.WaveFormat.Encoding.ToString() + "");

drNewRow[ColumnLabel.EncodingID] = Convert.ToString(Convert.ToInt32(objFile.WaveFormat.Encoding));

drNewRow[ColumnLabel.SampleRate] = Convert.ToString(objFile.WaveFormat.SampleRate + "");

drNewRow[ColumnLabel.BytesPerSecond] = Convert.ToString(objFile.WaveFormat.AverageBytesPerSecond + "");

drNewRow[ColumnLabel.BitsPerSample] = Convert.ToString(objFile.WaveFormat.BitsPerSample + "");

drNewRow[ColumnLabel.Channels] = Convert.ToString(objFile.WaveFormat.Channels + "");

drNewRow[ColumnLabel.BlockAlign] = Convert.ToString(objFile.WaveFormat.BlockAlign + "");

try

{

//-----convert from milliseconds to seconds

drNewRow[ColumnLabel.FileLength] = Convert.ToString(Convert.ToInt32(objFile.Length / 1000) + "");

}

catch {}

}

}

catch

{

drNewRow[ColumnLabel.ValidDialing] = "False";

}

finally

{

try

{

//-----set default Encoding label, if needed

if (string.IsNullOrEmpty(Convert.ToString(drNewRow[ColumnLabel.EncodingLabel] + "")))

{

drNewRow[ColumnLabel.EncodingLabel] = "(Unknown)";

}

}

catch {}

try

{

//-----attempt to calculate file length off of framework library call instead of sound library

if (Parse.IsNumeric(drNewRow[ColumnLabel.FileLength]) == false)

{

System.IO.FileInfo objFileInfo = new System.IO.FileInfo(sFileAndPath);

drNewRow[ColumnLabel.FileLength] = objFileInfo.Length;

}

}

catch {}

}

//-----add row to datatable

dtResult.Rows.Add(drNewRow);

//-----save data

dtResult.AcceptChanges();

//-----return data

return dtResult;

}

}

catch (Exception ex)

{

throw new Exception("Error in " + System.Reflection.MethodInfo.GetCurrentMethod().ReflectedType.ToString() + "." + System.Reflection.MethodInfo.GetCurrentMethod().Name + ": " + ex.Message);

}

}

public static System.String GetAudioPropertiesShortFormat(

string sFileAndPath)

{

try

{

//-----Build short format from long format

using (System.Data.DataTable dtFile = CallCommand.Messaging.Voice.Audio.Functions.GetAudioPropertiesLongFormat(sFileAndPath))

{

return Convert.ToString(dtFile.Rows[0][ColumnLabel.EncodingLabel] + "")

+ "/"

+ Convert.ToString(dtFile.Rows[0][ColumnLabel.SampleRate] + "")

+ "/"

+ Convert.ToString(dtFile.Rows[0][ColumnLabel.BytesPerSecond] + "")

+ "/"

+ Convert.ToString(dtFile.Rows[0][ColumnLabel.BitsPerSample] + "")

+ "/"

+ Convert.ToString(dtFile.Rows[0][ColumnLabel.Channels] + "")

+ "/"

+ Convert.ToString(dtFile.Rows[0][ColumnLabel.BlockAlign] + "");

}

}

catch (Exception ex)

{

throw new Exception("Error in " + System.Reflection.MethodInfo.GetCurrentMethod().ReflectedType.ToString() + "." + System.Reflection.MethodInfo.GetCurrentMethod().Name + ": " + ex.Message);

}

}

public static System.Int32 GetAudioPropertyAudioLength(

string sFileAndPath)

{

try

{

//-----Use NAudio reference to pull file properties

System.Data.DataTable dtProperties = CallCommand.Messaging.Voice.Audio.Functions.GetAudioPropertiesLongFormat(sFileAndPath);

//-----Determine if successful

if (dtProperties.Rows.Count > 0

&& Parse.IsNumeric(dtProperties.Rows[0][ColumnLabel.FileLength] + ""))

{

//-----set initial value

return Convert.ToInt32(dtProperties.Rows[0][ColumnLabel.FileLength]);

}

else

{

//-----unsuccessful, return default

return -1;

}

}

catch (Exception ex)

{

throw new Exception("Error in " + System.Reflection.MethodInfo.GetCurrentMethod().ReflectedType.ToString() + "." + System.Reflection.MethodInfo.GetCurrentMethod().Name + ": " + ex.Message);

}

}

public static void ConvertWaveToMulaw(

string inputFile,

string outputFile)

{

//-----temp file, sometimes needed

string tempConvertFile1 = inputFile + ".tempConvert1.wav";

string tempOutputFile2 = outputFile + ".tempOutput2.wav";

try

{

//-----capture original file paths

string origInputFile = inputFile;

string origOutputFile = outputFile;

//-----check if file already in proper format

if (CallCommand.Messaging.Voice.Audio.Functions.IsFileValidMuLawFormatForVoiceCalling(inputFile))

{

//-----file is already in correct format

//-----determine if output file is same destination as input file

if (inputFile.ToUpper() == outputFile.ToUpper())

{

//-----same file, nothing more to do

return;

}

else

{

//-----simply copy input file to output file

System.IO.File.Copy(inputFile, outputFile, true);

//-----exit

return;

}

}

//-----get file properties

using (System.Data.DataTable dtFile = CallCommand.Messaging.Voice.Audio.Functions.GetAudioPropertiesLongFormat(inputFile))

{

//-----validate current file state

if (dtFile.Rows.Count == 0)

{

throw new Exception("Unable to get file sound properties for file: " + inputFile);

}

//-----validate file length

if (Parse.IsNumeric(dtFile.Rows[0][ColumnLabel.FileLength]) == false

|| Convert.ToInt32(dtFile.Rows[0][ColumnLabel.FileLength]) == 0) {

throw new Exception("File is empty.");

}

//-----validate Encoding ID

if (Parse.IsNumeric(dtFile.Rows[0][ColumnLabel.EncodingID]) == false) {

throw new Exception("Unknown Encoding ID for this record: " + Convert.ToString(dtFile.Rows[0][ColumnLabel.EncodingID] + ""));

}

//-----check if input file and output file (overlay)

if (inputFile.ToUpper() == outputFile.ToUpper())

{

//-----must capture separate temp file for output data

outputFile = tempOutputFile2;

}

//-----get the Encoding ID enum

NAudio.Wave.WaveFormatEncoding iEncodingId = (NAudio.Wave.WaveFormatEncoding)Enum.Parse(typeof(NAudio.Wave.WaveFormatEncoding), Convert.ToString(dtFile.Rows[0][ColumnLabel.EncodingID]));

//-----determine encoding type

switch (iEncodingId)

{

case NAudio.Wave.WaveFormatEncoding.Pcm:

//-----determine if sample rate higher than 8000

if (Convert.ToInt32(dtFile.Rows[0][ColumnLabel.SampleRate]) > 8000)

{

//-----must first convert to lower level before converting to mu-law

//-----convert from higher PCM to lower PCM

using (NAudio.Wave.WaveStream waveStream = new NAudio.Wave.WaveFileReader(inputFile))

{

NAudio.Wave.WaveFormat targetFormat = new NAudio.Wave.WaveFormat(8000, 1);

using (NAudio.Wave.WaveStream ConvertedStream = new NAudio.Wave.WaveFormatConversionStream(targetFormat, waveStream))

{

NAudio.Wave.WaveFileWriter.CreateWaveFile(tempConvertFile1, ConvertedStream);

}

//-----point the new input file as the temp file

inputFile = tempConvertFile1;

}

}

//-----now we can convert from lower PCM to Mu-Law

using (NAudio.Wave.WaveStream waveStream = new NAudio.Wave.WaveFileReader(inputFile))

{

NAudio.Wave.WaveFormat targetFormat = NAudio.Wave.WaveFormat.CreateMuLawFormat(8000, 1);

using (NAudio.Wave.WaveStream ConvertedStream = new NAudio.Wave.WaveFormatConversionStream(targetFormat, waveStream))

{

NAudio.Wave.WaveFileWriter.CreateWaveFile(outputFile, ConvertedStream);

}

}

break;

default:

throw new Exception("This function is not yet coded to convert this file format.");

}

}

//-----

System.Threading.Thread.Sleep(0);

//-----confirm conversion took place

if (CallCommand.Messaging.Voice.Audio.Functions.IsFileValidMuLawFormatForVoiceCalling(outputFile) == false)

{

throw new Exception("Input File not properly converted: " + inputFile);

}

//-----determine if temp2 file needs to be copied as final output file

if (System.IO.File.Exists(tempOutputFile2))

{

System.IO.File.Copy(tempOutputFile2, origOutputFile, true);

}

}

catch (Exception ex)

{

throw new Exception("Error in " + System.Reflection.MethodInfo.GetCurrentMethod().ReflectedType.ToString() + "." + System.Reflection.MethodInfo.GetCurrentMethod().Name + ": " + ex.Message);

}

finally

{

try

{

//-----delete temp file, if exists

System.IO.File.Delete(tempConvertFile1);

}

catch {}

try

{

//-----delete temp file, if exists

System.IO.File.Delete(tempOutputFile2);

}

catch {}

}

}

public static void CombineTwoFilesToMulaw(

string inputFile1,

string inputFile2,

string outputFile)

{

try

{

//-----validate both files to merge exist

if (!System.IO.File.Exists(inputFile1))

{

throw new Exception("Parameter InputFile1 not found: " + inputFile1);

}

if (!System.IO.File.Exists(inputFile2))

{

throw new Exception("Parameter InputFile2 not found: " + inputFile2);

}

//-----validate Input File 1 is Mu-Law format

if (CallCommand.Messaging.Voice.Audio.Functions.IsFileValidMuLawFormatForVoiceCalling(inputFile1) == false)

{

throw new Exception("Parameter InputFile1 is not the proper sound format to combine: " + inputFile1);

}

//-----validate Input File 2 is Mu-Law format

if (CallCommand.Messaging.Voice.Audio.Functions.IsFileValidMuLawFormatForVoiceCalling(inputFile2) == false)

{

throw new Exception("Parameter InputFile2 is not the proper sound format to combine: " + inputFile2);

}

//-----create temp output file to store the work

string tempOutputFile = outputFile + ".temp.wav";

//-----delete previous temp file, if exists

if (System.IO.File.Exists(tempOutputFile))

{

try

{

System.IO.File.Delete(tempOutputFile);

}

catch (Exception ex)

{

throw new Exception("Unable to delete previous tempOutputFile: " + tempOutputFile + " : " + ex.Message);

}

}

//-----

//-----Combine

//-----

int buffSize = 1024;

byte[] buffer = new byte[buffSize];

//-----Build mu-Law output file

using (NAudio.Wave.WaveFileWriter waveFileWriter = new NAudio.Wave.WaveFileWriter(tempOutputFile, NAudio.Wave.WaveFormat.CreateMuLawFormat(8000, 1)))

{

//-----Append Input File 1

using (NAudio.Wave.WaveFileReader reader = new NAudio.Wave.WaveFileReader(inputFile1))

{

//-----first read

int read = reader.Read(buffer, 0, buffer.Length);

while (read > 0)

{

//-----write

waveFileWriter.WriteData(buffer, 0, read);

//-----next read

read = reader.Read(buffer, 0, buffer.Length);

}

}

//-----Append Input File 2

using (NAudio.Wave.WaveFileReader reader = new NAudio.Wave.WaveFileReader(inputFile2))

{

//-----first read

int read = reader.Read(buffer, 0, buffer.Length);

while (read > 0)

{

//-----write

waveFileWriter.WriteData(buffer, 0, read);

//-----next read

read = reader.Read(buffer, 0, buffer.Length);

}

}

}

//-----Confirm outputfile was built properly

if (CallCommand.Messaging.Voice.Audio.Functions.IsFileValidMuLawFormatForVoiceCalling(tempOutputFile) == false)

{

throw new Exception("Variable tempOutputFileInputFile2 was not created in the proper sound format: " + tempOutputFile);

}

//-----copy temp output to final output file

try

{

System.IO.File.Copy(tempOutputFile, outputFile, true);

}

catch (Exception ex)

{

throw new Exception("Unable to copy tempOutputFile as the final outputFile: " + ex.Message);

}

//-----delete temp output file

try

{

System.IO.File.Delete(tempOutputFile);

}

catch (Exception ex)

{

throw new Exception("Unable to delete tempOutputFile: " + ex.Message);

}

}

catch (Exception ex)

{

throw new Exception("Error in " + System.Reflection.MethodInfo.GetCurrentMethod().ReflectedType.ToString() + "." + System.Reflection.MethodInfo.GetCurrentMethod().Name + ": " + ex.Message);

}

}

}

public class ColumnLabel

{

public static string FileName = "FileName";

public static string ValidDialing = "ValidDialing";

public static string EncodingLabel = "EncodingLabel";

public static string EncodingID = "EncodingID";

public static string SampleRate = "SampleRate";

public static string BytesPerSecond = "BytesPerSecond";

public static string BitsPerSample = "BitsPerSample";

public static string Channels = "Channels";

public static string BlockAlign = "BlockAlign";

public static string FileLength = "FileLength";

}

public class Parse

{

public static bool IsNumeric(object data)

{

try

{

System.Double.Parse(Convert.ToString(data + ""));

return true;

}

catch

{

return false;

}

}

}

}

From: webideas [email removed]
Sent: Thursday, April 07, 2011 11:11 AM
To: john_schellenberger@hotmail.com
Subject: Re: Error converting file using VS 2005 project to VS 2008 NAudio.dll [naudio:236410]

From: webideas

Yes, if you do not mind sharing it.

What was the reason for changing the project from Visual Basic to C#?

Thanks!

May 9, 2011 at 3:11 PM

Could not get it to work. Can you upload the whole VS 2010 project here and not just the code snippet?

Thanks!

May 9, 2011 at 4:22 PM

The entire project would need to be zipped, and I don’t see where a zip attachment would post to this blog site.

From: webideas [email removed]
Sent: Monday, May 09, 2011 10:12 AM
To: john_schellenberger@hotmail.com
Subject: Re: Error converting file using VS 2005 project to VS 2008 NAudio.dll [naudio:236410]

From: webideas

Could not get it to work. Can you upload the whole VS 2010 project here and not just the code snippet?

Thanks!

May 9, 2011 at 6:57 PM
Edited Nov 9, 2011 at 6:13 PM

Please e-mail it to me.

Thanks!

May 9, 2011 at 6:58 PM
I did, and it was rejected.


From: [email removed]
To: [email removed]
Date: Mon, 9 May 2011 10:57:29 -0700
Subject: Re: Error converting file using VS 2005 project to VS 2008 NAudio.dll [naudio:236410]

From: webideas
Please e-mail it to me: webideas (at) gmail.com
Thanks!
Read the full discussion online.
To add a post to this discussion, reply to this email (naudio@discussions.codeplex.com)
To start a new discussion for this project, email naudio@discussions.codeplex.com
You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.
Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com