/// <summary> /// Write .w3speech format to BinaryWriter. /// </summary> /// <param name="id">The id to use which usually is CPSW.</param> /// <param name="version">The version to use, which is usually either 163 or 162.</param> /// <param name="inputs">Wem and cr2w pairs to write to the stream.</param> /// <param name="language_key">The language key to use for the format.</param> /// <param name="bw">Stream to write the w3speech format to.</param> /// <exception cref="Exception">Something happened.</exception> public static void Encode(String id, UInt32 version, IEnumerable <WemCr2wInputPair> inputs, W3LanguageKey language_key, BinaryWriter bw) { var inputList = inputs.ToList(); bw.Write(System.Text.Encoding.ASCII.GetBytes(id.Substring(0, 4).PadLeft(4))); bw.Write(version); bw.Write((UInt16)(language_key.value >> 16)); var bytes_written_count = bw.WriteBit6(inputList.Count); UInt64 pos = 4 + 4 + 2 + bytes_written_count + ((UInt64)inputList.Count * 10 * 4) + 2; var itemInfos = inputList.Select(pair => { var wave_offset = (UInt32)pos + 4; UInt32 cr2w_offset = wave_offset + pair.wem_size + 8; pos = cr2w_offset + pair.cr2w_size; return(new SpeechEntry(null, pair.id, pair.id_high, wave_offset, pair.wem_size, cr2w_offset, pair.cr2w_size, pair.duration)); }).ToList(); byte[] zeros = new byte[] { 0, 0, 0, 0 }; itemInfos.ForEach(info => { bw.Write((UInt32)info.id.value); bw.Write((UInt32)info.id_high); bw.Write((UInt32)info.wem_offs - 4); bw.Write(zeros); bw.Write((UInt32)info.wem_size + 12); bw.Write(zeros); bw.Write((UInt32)info.cr2w_offs); bw.Write(zeros); bw.Write((UInt32)info.cr2w_size); bw.Write(zeros); }); bw.Write((UInt16)(language_key.value & 0xFFFF)); inputList.ForEach(pair => { var wem = pair.wem.Invoke(); bw.Write((UInt32)pair.wem_size); StreamTools.Transfer(pair.wem_size, wem, bw.BaseStream); wem.Close(); bw.Write(pair.duration); bw.Write((UInt32)4); var cr2w = pair.cr2w.Invoke(); StreamTools.Transfer(pair.cr2w_size, cr2w, bw.BaseStream); cr2w.Close(); }); }
/// <summary> /// Unpacks the w3speech file into the directory. /// </summary> /// <param name="directory">The directory where the new wem and cr2w files will be unpacked into.</param> /// <param name="w3speech_file">The w3speech file to unpack.</param> /// <param name="languages">Information about the known languages. The right one will be selected automatically.</param> /// <exception cref="Exception">Something happened.</exception> public static void UnpackW3SpeechFile(String directory, String w3speech_file, IEnumerable <W3Language> languages) { var input = new BinaryReader(new FileStream(w3speech_file, FileMode.Open)); var info = Coder.Decode(new W3Speech(w3speech_file), input); var language = languages.First(lang => lang.Key.value == info.language_key.value); var dir = directory.EndsWith("\\") ? directory : directory + "\\"; info.item_infos.ToList().ForEach(item => { var neutral_id = LanguageTools.Convert(item.id, language); var wem_file_name = neutral_id.value.ToString("D10") + "[" + item.duration.ToString("R", CultureInfo.InvariantCulture) + "](" + item.id_high.ToString("D10") + ").wem"; var wem_output = new BinaryWriter(new FileStream(dir + wem_file_name, FileMode.Create)); input.BaseStream.Seek((int)item.wem_offs, SeekOrigin.Begin); StreamTools.Transfer(item.wem_size, input.BaseStream, wem_output.BaseStream); wem_output.Close(); var cr2w_file_name = neutral_id.value.ToString("D10") + "(" + item.id_high.ToString("D10") + ").cr2w"; var cr2w_output = new BinaryWriter(new FileStream(dir + cr2w_file_name, FileMode.Create)); input.BaseStream.Seek((int)item.cr2w_offs, SeekOrigin.Begin); StreamTools.Transfer(item.cr2w_size, input.BaseStream, cr2w_output.BaseStream); cr2w_output.Close(); }); input.Close(); }