DLL
Børre Stenseth
Moduler>Assemblies>Lyd

Frøken Ur

Hva
Skisse til en alarmklokke

Hensikten er å lage en assembly, DLL, som sier tiden for de tidspunktene vi ønsker. Egentlig er biblioteket mer fleksibelt enn som så. Vi kan i prinsipp si et hvilket som helst tall innenfor rimelighetens grenser.

MERK:

Lydene som inngår som ressurser i biblioteket er utviklet i et kommersielt prosjekt og må bare brukes i undervisnings- og treningsøyemed.

De kan være et godt utgangspunkt for å spille inn dine egne lyder.

Bibioteket har en klasse: saynumber med følgende tilgjengelige metoder:

public static void Say(int i)
{
    sndList.Clear();
    doInt(i);
    PlaySoundList();
}
public static void Say(double d)
{
    sndList.Clear();
    doDouble(d);
}
public static void Say(float f)
{
    Say((double)f);
}
public static void SayNumberSequence(String s)
{
    sndList.Clear();
    // any number of :-separated numbers
    // typically a timestring hh:mm:ss
    String[] pieces = s.Split(':');
    for (int ix = 0; ix < pieces.Length; ix++)
    {
        try
        {
            int t = Convert.ToInt32(pieces[ix]);
            doInt(t);
        }
        catch (Exception ex)
        {
            try
            {
                double t = Convert.ToDouble(pieces[ix]);
                doDouble(t);
            }
            catch (Exception ex2)
            {
                // error, bad format
                SayError();
            }
        }
        SaySpace();
    }
    PlaySoundList();
}

Implementasjonen er bygget på to statiske variable:

protected static SoundPlayer snd = new SoundPlayer();
protected static ArrayList sndList = new ArrayList(10);

De interne metodene er disse:

protected static void SayError()
{
    sndList.Add(Properties.Resources.BUSYFONE);
}
protected static void SayEtt()
{
    sndList.Add(Properties.Resources._1TT);
}
protected static void SayNull()
{
    sndList.Add(Properties.Resources._0);
}
protected static void Say1000000()
{
    sndList.Add(Properties.Resources._1000000);
}
protected static void Say1000()
{
    sndList.Add(Properties.Resources._1000);
}
protected static void Say100()
{
    sndList.Add(Properties.Resources._100);
}
protected static void SayAnd()
{
    sndList.Add(Properties.Resources.OG);
}
protected static void SayComma()
{
    sndList.Add(Properties.Resources.KOMMA);
}
protected static void SayMinus()
{
    sndList.Add(Properties.Resources.MINUS);
}
protected static void SayPlus()
{
    sndList.Add(Properties.Resources.PLUSS);
}
protected static void SaySpace()
{
    sndList.Add(Properties.Resources.SPACE500);
}
protected static void PlaySoundList()
{
    for (int ix = 0; ix < sndList.Count; ix++)
    {
        snd.Stream = (Stream)sndList[ix];
        snd.LoadAsync();
        snd.PlaySync();
    }
}
protected static void doDouble(double d)
{
    // handle negative number
    if (d < 0)
    {
        SayMinus();
        d = -d;
    }
    // split in decimal and integer part
    String s = d.ToString();
    s = s.Replace(',', '.');
    if (s.StartsWith("."))
        s = "0" + s;
    String[] pieces = s.Split('.');
    for (int ix = 0; ix < pieces.Length; ix++)
    {
        try
        {
            int t = Convert.ToInt32(pieces[ix]);
            doInt(t);
        }
        catch (Exception ex)
        {
            // error, bad format
            SayError();
        }
        if ((ix != pieces.Length - 1) && (pieces.Length > 1))
            SayComma();
    }
}
protected static void doInt(int i)
{
    // handle negative number
    if (i < 0)
    {
        SayMinus();
        i = -i;
    }
    if (i > 1000000)
    {
        doInt(i / 1000000);
        Say1000000();
        if (i % 1000000 > 0)
            doInt(i % 1000000);
        return;
    }
    if (i > 1000)
    {
        doInt(i / 1000);
        Say1000();
        if (i % 1000 > 0)
            doInt(i % 1000);
        return;
    }
    if (i > 100)
    {
        doInt(i / 100);
        Say100();
        if (i % 100 > 0)
            SayAnd();
        if (i % 100 > 0)
            doInt(i % 100);
        return;
    }
    Say2digitInt(i);
}
protected static void Say2digitInt(int n)
{
    // say an integer
    if (n == 1)
    {
        SayEtt();
        return;
    }
    if (n == 0)
    {
        SayNull();
        return;
    }
    Stream s = null;
    if ((n > 9) && (n < 20))
    {
        switch (n)
        {
            case 11: s = Properties.Resources._11; break;
            case 12: s = Properties.Resources._12; break;
            case 13: s = Properties.Resources._13; break;
            case 14: s = Properties.Resources._14; break;
            case 15: s = Properties.Resources._15; break;
            case 16: s = Properties.Resources._16; break;
            case 17: s = Properties.Resources._17; break;
            case 18: s = Properties.Resources._18; break;
            case 19: s = Properties.Resources._19; break;
            default: s = Properties.Resources._10; break;
        }
        sndList.Add(s);
        return;
    }
    int d1, d2;
    if (n > 9)
    {
        d1 = n / 10;
        d2 = n % 10;
    }
    else
    {
        d1 = -999;
        d2 = n;
    }
    switch (d1)
    {
        case 0: s = Properties.Resources._0; break;
        case 1: s = Properties.Resources._10; break;
        case 2: s = Properties.Resources._20; break;
        case 3: s = Properties.Resources._30; break;
        case 4: s = Properties.Resources._40; break;
        case 5: s = Properties.Resources._50; break;
        case 6: s = Properties.Resources._60; break;
        case 7: s = Properties.Resources._70; break;
        case 8: s = Properties.Resources._80; break;
        case 9: s = Properties.Resources._90; break;
        default: s = null; break;
    }
    if (s != null)
        sndList.Add(s);
    s = null;
    switch (d2)
    {
        case 0: if (d1 != -999)
                s = Properties.Resources._0; break;
        case 1: s = Properties.Resources._1; break;
        case 2: s = Properties.Resources._2; break;
        case 3: s = Properties.Resources._3; break;
        case 4: s = Properties.Resources._4; break;
        case 5: s = Properties.Resources._5; break;
        case 6: s = Properties.Resources._6; break;
        case 7: s = Properties.Resources._7; break;
        case 8: s = Properties.Resources._8; break;
        case 9: s = Properties.Resources._9; break;
        default: s = null; break;
    }
    if (s != null)
        sndList.Add(s);
}

For å studere implementasjonen nærmere må du åpne Saynumber-prosjektet.

Referanser
  • DLL-prosjektet:
    https://svn.hiof.no/svn/psource/Csharpspikes/saynumber
Vedlikehold

B.Stenseth, revidert desember 2006

(Velkommen) Moduler>Assemblies>Lyd (Arkitektur)