Skip to content

Commit

Permalink
Adds streaming versions and refactors code (#102)
Browse files Browse the repository at this point in the history
Adds streaming versions and refactors code significantly to avoid duplication
  • Loading branch information
StephenHodgson authored Nov 24, 2024
2 parents 8cb8f93 + eda0bdb commit c886ef6
Show file tree
Hide file tree
Showing 14 changed files with 791 additions and 275 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using Newtonsoft.Json;
using UnityEngine.Scripting;

namespace ElevenLabs
{
/// <summary>
/// Represents timing information for a single character in the transcript
/// </summary>
[Preserve]
public class TimestampedTranscriptCharacter
{
[Preserve]
[JsonConstructor]
internal TimestampedTranscriptCharacter(string character, double startTime, double endTime)
{
Character = character;
StartTime = startTime;
EndTime = endTime;
}

/// <summary>
/// The character being spoken
/// </summary>
[Preserve]
[JsonProperty("character")]
public string Character { get; }

/// <summary>
/// The time in seconds when this character starts being spoken
/// </summary>
[Preserve]
[JsonProperty("character_start_times_seconds")]
public double StartTime { get; }

/// <summary>
/// The time in seconds when this character finishes being spoken
/// </summary>
[Preserve]
[JsonProperty("character_end_times_seconds")]
public double EndTime { get; }
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using ElevenLabs.Voices;
using System;
using UnityEngine;
using UnityEngine.Scripting;

namespace ElevenLabs
{
/// <summary>
/// Represents timing information for a single character in the transcript
/// </summary>
[Preserve]
[Serializable]
public class TranscribedVoiceClip : VoiceClip
{
[Preserve]
internal TranscribedVoiceClip(TimestampedTranscriptCharacter[] timestampedTranscriptCharacters, string id, string text, Voice voice, AudioClip audioClip, string cachedPath)
: base(id, text, voice, audioClip, cachedPath)
{
TimestampedTranscriptCharacters = timestampedTranscriptCharacters;
}

[Preserve]
public TimestampedTranscriptCharacter[] TimestampedTranscriptCharacters { get; }
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace ElevenLabs
{
[Preserve]
[Serializable]
public sealed class VoiceClip : GeneratedClip
public class VoiceClip : GeneratedClip
{
[Preserve]
internal VoiceClip(string id, string text, Voice voice, AudioClip audioClip, string cachedPath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Networking;
using Utilities.WebRequestRest;
using Debug = UnityEngine.Debug;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using Newtonsoft.Json;
using UnityEngine.Scripting;

namespace ElevenLabs.TextToSpeech
{
[Preserve]
internal sealed class Alignment
{
[Preserve]
[JsonConstructor]
internal Alignment(
[JsonProperty("characters")] string[] characters,
[JsonProperty("character_start_times_seconds")] double[] startTimes,
[JsonProperty("character_end_times_seconds")] double[] endTimes)
{
Characters = characters;
StartTimes = startTimes;
EndTimes = endTimes;
}

[Preserve]
[JsonProperty("characters")]
public string[] Characters { get; }

[Preserve]
[JsonProperty("character_start_times_seconds")]
public double[] StartTimes { get; }

[Preserve]
[JsonProperty("character_end_times_seconds")]
public double[] EndTimes { get; }

[Preserve]
public static implicit operator TimestampedTranscriptCharacter[](Alignment alignment)
{
if (alignment == null) { return null; }
var characters = alignment.Characters;
var startTimes = alignment.StartTimes;
var endTimes = alignment.EndTimes;
var timestampedTranscriptCharacters = new TimestampedTranscriptCharacter[characters.Length];

for (var i = 0; i < characters.Length; i++)
{
timestampedTranscriptCharacters[i] = new TimestampedTranscriptCharacter(characters[i], startTimes[i], endTimes[i]);
}

return timestampedTranscriptCharacters;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c886ef6

Please sign in to comment.