private static void convertHTKToFloatBinary(string binaryFile, int dim, string htkFile) { try { HtkParameterFile htkParameterFile = new HtkParameterFile(); htkParameterFile.BigEndianLoad(htkFile); FileStream fs = new FileStream(binaryFile, FileMode.Create); //初始化FileStream对象 BinaryWriter bw = new BinaryWriter(fs); //创建BinaryWriter对象 //写入文件 for (int i = 0; i < htkParameterFile.Data.Length; i++) { for (int j = 0; j < htkParameterFile.Data[i].Length; j++) { bw.Write(htkParameterFile.Data[i][j]); } } bw.Close(); //关闭BinaryWriter对象 fs.Close(); //关闭文件流 } catch (Exception e) { Console.WriteLine("convertHTKToFloatBinary Error:" + e.Message); } }
private static void convertFloatBinaryToHTK(string binaryFile, int dim, string htkFile) { try { List <float> data = new List <float>(); using (BinaryReader binary = new BinaryReader(File.Open(binaryFile, FileMode.Open))) { try { while (true) { float x = binary.ReadSingle(); data.Add(x); } } catch (EndOfStreamException e) { Console.WriteLine(e.Message); Console.WriteLine("to the end"); } } if (data.Count % dim != 0) { Console.WriteLine("error!"); return; } int frames = data.Count / dim; HtkParameterFile htkParameterFile = new HtkParameterFile(); htkParameterFile.SamplePeriodInSecond = 0.001f * 5; htkParameterFile.ParameterKind = ParameterKinds.UserDefined; htkParameterFile.Data = new float[frames][]; for (int i = 0; i < frames; i++) { htkParameterFile.Data[i] = new float[dim]; for (int j = 0; j < dim; j++) { htkParameterFile.Data[i][j] = data[i * dim + j]; } } htkParameterFile.Save(htkFile); } catch (Exception e) { Console.WriteLine("convertFloatBinaryToHTK Error: " + e.Message); } }