Skip to content

Instantly share code, notes, and snippets.

@dpoeschl
Created December 12, 2013 06:56
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpoeschl/7924130 to your computer and use it in GitHub Desktop.
Save dpoeschl/7924130 to your computer and use it in GitHub Desktop.
Could be better, but I had to have one that showed the words as they're spoken. For the inspiration: https://gist.github.com/fekberg/bac40e784113dd75c03e
// Expanded on Filip Ekberg's gist: https://gist.github.com/fekberg/bac40e784113dd75c03e
// See his tweet @fekberg for more https://twitter.com/fekberg/status/410964845870071808
// Not perfect, but still amusing.
using System;
using System.Media;
using System.Speech.Synthesis;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
namespace Titanic
{
class Program
{
private const string fileLocation = "audio.wav";
private const int timeBetweenBeeps = 250;
static void Main(string[] args)
{
var words = new List<Word>()
{
new Word(Pitch.E, "E", "Ehhhhhhh"),
new Word(Pitch.E, "very ", "vree"),
new Word(Pitch.E, "night ", "niiiight"),
new Word(Pitch.E, "in ", "innnnnnnnnnnn"),
new Word(Pitch.Eb, "my ", "miiiiiigh"),
new Word(Pitch.E, "dreams,\n", "dreeeeeeeeeehhhhhhhhms"),
new Word(Pitch.E, "I ", "iiiiigh"),
new Word(Pitch.Eb, "see ", "seeeehhh"),
new Word(Pitch.E, "you, ", "yooooooooouu"),
new Word(Pitch.Gb, "I ", "iiiiiigh"),
new Word(Pitch.Ab, "feel ", "feeehhhhhhhhhhhhl"),
new Word(Pitch.Gb, "you.\n", "yooooouu"),
new Word(Pitch.E, "That ", "Thaaaaat "),
new Word(Pitch.E, "is ", "ihhhss"),
new Word(Pitch.E, "how ", "hoooowww"),
new Word(Pitch.E, "I ", "iiiiigh"),
new Word(Pitch.Eb, "know ", "knoooooow"),
new Word(Pitch.E, "you ", "yooooouu"),
new Word(Pitch.E, "go ", "goooooogh"),
new Word(Pitch.B, "on.\n", "ooonnnnn")
};
var times = new AudioCreator(fileLocation).CreateAudioAndTimingChart(words);
var player = new SoundPlayer(fileLocation);
Task t = Task.Run(() =>
{
player.PlaySync();
});
Task t2 = Task.Run(() =>
{
int i = 0;
foreach (var word in words)
{
Console.Write(word.Text);
Console.Beep((int)word.Pitch, Math.Max(0, times[i] - timeBetweenBeeps));
i++;
}
});
Task.WaitAll(t, t2);
Console.WriteLine();
Console.WriteLine();
}
}
enum Pitch
{
B = 988,
Eb = 1244,
E = 1318,
Gb = 1480,
Ab = 1661,
}
class Word
{
public Pitch Pitch { get; private set; }
public string Text { get; private set; }
public string Pronounciation { get; private set; }
public Word(Pitch pitch, string word, string pronounciation)
{
this.Pitch = pitch;
this.Text = word;
this.Pronounciation = pronounciation;
}
}
class AudioCreator
{
private int[] times;
private string fileLocation;
public AudioCreator(string fileLocation)
{
this.fileLocation = fileLocation;
}
public int[] CreateAudioAndTimingChart(List<Word> words, VoiceGender voiceGender = VoiceGender.Female, VoiceAge voiceAge = VoiceAge.Senior)
{
using (var synth = new SpeechSynthesizer())
{
synth.SetOutputToWaveFile(fileLocation);
synth.SelectVoiceByHints(voiceGender, voiceAge);
PromptBuilder builder = new PromptBuilder(new System.Globalization.CultureInfo("en-US"));
synth.BookmarkReached += new EventHandler<BookmarkReachedEventArgs>(BookmarkReached);
int i = 0;
foreach (var word in words)
{
builder.AppendText(word.Pronounciation);
builder.AppendBookmark(i.ToString());
i++;
}
times = new int[words.Count];
synth.Speak(builder);
for (i = words.Count - 1; i > 0; i--)
{
times[i] = times[i] - times[i - 1];
}
}
return times;
}
public void BookmarkReached(object sender, BookmarkReachedEventArgs e)
{
times[int.Parse(e.Bookmark)] = (int)e.AudioPosition.TotalMilliseconds;
}
}
}
@ThierrydeGroot
Copy link

nice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment