Exemplo n.º 1
0
    LIP ConvertToLIP(string data)
    {
        List <Frame> frames = new List <Frame>();;
        dynamic      json   = JObject.Parse(data);

        float line_duration = 0;

        foreach (dynamic word in json.words)
        {
            line_duration = Math.Max(line_duration, float.Parse(word.end.ToString()));
            float wordStart   = float.Parse(word.start.ToString());
            float accumulated = 0f;
            foreach (dynamic phoneme in word.phones)
            {
                // Get the duration and phoneme type
                float  duration = float.Parse(phoneme.duration.ToString());
                string phone    = phoneme.phone.ToString().Split('_')[0].ToUpper();

                // Calculate the start time of the phoneme in the audio file
                float phonemeStart = wordStart + accumulated;

                // Add the duration of the phoneme to the accumulated time
                accumulated += duration;

                // Check if we know this phoneme (we should know them all...)
                if (!phoneme_map.ContainsKey(phone))
                {
                    Debug.Log("Did not find phoneme " + phone);
                    continue;
                }

                // Create a frame
                Frame frame = new Frame()
                {
                    time = phonemeStart, shape = phoneme_map[phone]
                };
                frames.Add(frame);
            }
        }

        frames.Add(new Frame()
        {
            time = clip.length, shape = 0
        });

        LIP lip = new LIP();

        lip.fileType    = 542132556; // "LIP " converted to Int32
        lip.fileVersion = 808333654; // "V1.0" converted to Int32
        lip.entryCount  = frames.Count;
        lip.length      = clip.length;

        lip.frames = frames.ToArray();

        Debug.Log("Created " + lip.frames.Length + " frames for line of length " + lip.length);

        return(lip);
    }
Exemplo n.º 2
0
    async void SaveFile()
    {
        await ForcedAlignment();

        LIP lip = ConvertToLIP(data);

        string saveLoc = EditorUtility.SaveFilePanel("Save LIP File", "", "", "");

        using (Stream stream = new FileStream(saveLoc, FileMode.CreateNew))
        {
            lip.WriteToStream(stream);
        }
    }
Exemplo n.º 3
0
        public static LIP LoadLipSync(string resref)
        {
            // Load the stream
            Stream stream = data.GetStream(resref, ResourceType.LIP);

            if (stream == null)
            {
                UnityEngine.Debug.LogWarning("Missing lip: " + resref);
                return(null);
            }

            // Read the lip file
            LIP lip = new LIP();
            Dictionary <string, Stream> other = new Dictionary <string, Stream>
            {
                { "lip", stream }
            };

            lip.Load(stream, other);

            return(lip);
        }
Exemplo n.º 4
0
    void LoadFile()
    {
        string loadLoc = EditorUtility.OpenFilePanel("Load LIP file", "", "");

        using (FileStream stream = new FileStream(loadLoc, FileMode.Open))
        {
            lipData = new LIP();
            lipData.Load(stream, new Dictionary <string, Stream>(), 0, 0);
            Debug.Log(lipData.frames.Length);
        }

        string audioLoc = EditorUtility.OpenFilePanel("Load Audio File", "", "");

        using (FileStream stream = new FileStream(audioLoc, FileMode.Open))
        {
            // Using the KotOR-Unity VO loading code, might as well!
            WAVObject wav = new WAVObject(stream);

            audioClip = AudioClip.Create("clip", wav.data.Length / wav.channels, wav.channels, wav.sampleRate, false);
            audioClip.SetData(wav.data, 0);

            audioTexture = PaintWaveformSpectrum(audioClip, 1, WIDTH, HEIGHT, Color.yellow);
        }
    }