tutorials.de Buch-Aktion 05/2012
ERLEDIGT
NEIN
ANTWORTEN
5
ZUGRIFFE
2382
EMPFEHLEN
  • An Twitter übertragen
  • An Facebook übertragen
AUF DIESES THEMA
ANTWORTEN
  1. #1
    The_MACman The_MACman ist offline Mitglied Silber
    Registriert seit
    Aug 2004
    Ort
    Chemnitz
    Beiträge
    67
    hallo,
    ich möchte mit C# töne verschiedne tonarten erzeugen und ausgeben.

    könnt ihr mir nen einfachen ansatz geben ?
    einer funktion eine frequenz übergeben und da is der ton ?

    wenn ihr jetzt meint machs mit DirectX dann könnt ihr mir gleich erklären warum bei mir der namespace directX nicht geht.
    hab das mit dem directX schon gesehn und wollte ein bsp testen und
    "using Microsoft.DirectX.DirectSound; "
    geht nicht. nach Microsoft taucht kein DirectX auf.
    Hab dann auch gefunden das ich das DirectX SDK brauch ... habe da jetzt massen Versionen runtergeladen ... auch von microsoft Juni208... und es gibt immernoch kein DirectX bei mir.

    Also ich hoffe ihr könnt mir helfen.
    Wie gesagt nich kompliziertes nur einen einfachen ton.

    Danke Markus !
     

  2. #2
    Kyoko Kyoko ist offline Mitglied Gold
    Registriert seit
    Jun 2004
    Ort
    Wels (Oberösterreich) bzw. Mayrhofen (Tirol)
    Beiträge
    211
    Zu DirectX: Da fehlt bei dir wohl ein Verweis!

    Zum Beep: Verwende die Funktion Beep in der kernel32.dll

    Code :
    1
    2
    3
    4
    5
    
    class WinAPIAccess
    {
        [DllImport("kernel32.dll")]
        private static extern long Beep(short srtFrequency, short srtDuration);
    }
     

  3. #3
    The_MACman The_MACman ist offline Mitglied Silber
    Registriert seit
    Aug 2004
    Ort
    Chemnitz
    Beiträge
    67
    Wenn ich nich irre ist beep doch die Funktion die den Speaker anspricht.
    Das is leider schon zu einfach
    Diesen Ton kann man einfach niemanden anbieten

    Was für Verweis kann mir denn wo fehlen ?
     

  4. #4
    Avatar von Nico Graichen
    Nico Graichen Nico Graichen ist offline aka gemballa
    tutorials.de Moderator
    Registriert seit
    Dec 2003
    Ort
    Pulheim (NRW)
    Beiträge
    3.898
    Blog-Einträge
    34
    Zitat Zitat von The_MACman Beitrag anzeigen
    [...]
    Was für Verweis kann mir denn wo fehlen ?
    Schau mal im Solution Explorer (Projektmappen-Explorer), da gibts den Knoten References (Verweise)
    Da musst du die entsprechenden Dlls für DirectX hinzufügen.
     
    Grüße Nico
    ----------------------
    Xing
    ----------------------
    Zitat Zitat von Mark Twain (1835-1910)
    Es gibt drei Dinge, die eine Frau aus dem Nichts hervorzaubern kann: einen Hut, einen Salat und einen Ehekrach.
    Zitat Zitat von Mike Wilson - Biographie über Larry Ellison (CEO Oracle)
    The Difference Between God and Larry Ellison: God Doesn't Think He's Larry Ellison

  5. #5
    The_MACman The_MACman ist offline Mitglied Silber
    Registriert seit
    Aug 2004
    Ort
    Chemnitz
    Beiträge
    67
    Jap das mit den verweisen hab ich jetzt endlich hinbekommen!
    ich werd mich jetzt hier durch das Beispiel kämpfen.

    falls jmd einen tollen ansatz hat kann er trotzdem posten
     

  6. #6
    The_MACman The_MACman ist offline Mitglied Silber
    Registriert seit
    Aug 2004
    Ort
    Chemnitz
    Beiträge
    67
    Also ich hab jetzt hier ein Beispiel.

    Code :
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    
    using System;
    using System.Threading;
    using System.Windows.Forms;
    using Microsoft.DirectX.DirectSound;
     
    namespace MyProject
    {
        class Program
        {
            public delegate void PullAudio(short[] buffer, int length);
     
            public class SoundPlayer : IDisposable
            {
                private Device soundDevice;
                private SecondaryBuffer soundBuffer;
                private int samplesPerUpdate;
                private AutoResetEvent[] fillEvent = new AutoResetEvent[2];
                private Thread thread;
                private PullAudio pullAudio;
                private short channels;
                private bool halted;
                private bool running;
     
                public SoundPlayer(Control owner, PullAudio pullAudio, short channels)
                {
                    this.channels = channels;
                    this.pullAudio = pullAudio;
     
                    this.soundDevice = new Device();
                    this.soundDevice.SetCooperativeLevel(owner, CooperativeLevel.Priority);
     
                    // Set up our wave format to 44,100Hz, with 16 bit resolution
                    WaveFormat wf = new WaveFormat();
                    wf.FormatTag = WaveFormatTag.Pcm;
                    wf.SamplesPerSecond = 44100;
                    wf.BitsPerSample = 16;
                    wf.Channels = channels;
                    wf.BlockAlign = (short)(wf.Channels * wf.BitsPerSample / 8);
                    wf.AverageBytesPerSecond = wf.SamplesPerSecond * wf.BlockAlign;
     
                    this.samplesPerUpdate = 512;
     
                    // Create a buffer with 2 seconds of sample data
                    BufferDescription bufferDesc = new BufferDescription(wf);
                    bufferDesc.BufferBytes = this.samplesPerUpdate * wf.BlockAlign * 2;
                    bufferDesc.ControlPositionNotify = true;
                    bufferDesc.GlobalFocus = true;
     
                    this.soundBuffer = new SecondaryBuffer(bufferDesc, this.soundDevice);
     
                    Notify notify = new Notify(this.soundBuffer);
     
                    fillEvent[0] = new AutoResetEvent(false);
                    fillEvent[1] = new AutoResetEvent(false);
     
                    // Set up two notification events, one at halfway, and one at the end of the buffer
                    BufferPositionNotify[] posNotify = new BufferPositionNotify[2];
                    posNotify[0] = new BufferPositionNotify();
                    posNotify[0].Offset = bufferDesc.BufferBytes / 2 - 1;
                    posNotify[0].EventNotifyHandle = fillEvent[0].SafeWaitHandle;
                    posNotify[1] = new BufferPositionNotify();
                    posNotify[1].Offset = bufferDesc.BufferBytes - 1;
                    posNotify[1].EventNotifyHandle = fillEvent[1].SafeWaitHandle;
     
                    notify.SetNotificationPositions(posNotify);
     
                    this.thread = new Thread(new ThreadStart(SoundPlayback));
                    this.thread.Priority = ThreadPriority.Highest;
     
                    this.Pause();
                    this.running = true;
     
                    this.thread.Start();
                }
     
                public void Pause()
                {
                    if (this.halted)
                        return;
     
                    this.halted = true;
     
                    Monitor.Enter(this.thread);
                }
     
                public void Resume()
                {
                    if (!this.halted)
                        return;
     
                    this.halted = false;
     
                    Monitor.Pulse(this.thread);
                    Monitor.Exit(this.thread);
                }
     
                private void SoundPlayback()
                {
                    lock (this.thread) {
                        if (!this.running)
                            return;
     
                        // Set up the initial sound buffer to be the full length
                        int bufferLength = this.samplesPerUpdate * 2 * this.channels;
                        short[] soundData = new short[bufferLength];
     
                        // Prime it with the first x seconds of data
                        this.pullAudio(soundData, soundData.Length);
                        this.soundBuffer.Write(0, soundData, LockFlag.None);
     
                        // Start it playing
                        this.soundBuffer.Play(0, BufferPlayFlags.Looping);
     
                        int lastWritten = 0;
                        while (this.running) {
                            if (this.halted) {
                                Monitor.Pulse(this.thread);
                                Monitor.Wait(this.thread);
                            }
     
                            // Wait on one of the notification events
                            WaitHandle.WaitAny(this.fillEvent, 3, true);
     
                            // Get the current play position (divide by two because we are using 16 bit samples)
                            int tmp = this.soundBuffer.PlayPosition / 2;
     
                            // Generate new sounds from lastWritten to tmp in the sound buffer
                            if (tmp == lastWritten) {
                                continue;
                            } else {
                                soundData = new short[(tmp - lastWritten + bufferLength) % bufferLength];
                            }
     
                            this.pullAudio(soundData, soundData.Length);
     
                            // Write in the generated data
                            soundBuffer.Write(lastWritten * 2, soundData, LockFlag.None);
     
                            // Save the position we were at
                            lastWritten = tmp;
                        }
                    }
                }
     
                public void Dispose()
                {
                    this.running = false;
                    this.Resume();
     
                    if (this.soundBuffer != null) {
                        this.soundBuffer.Dispose();
                    }
                    if (this.soundDevice != null) {
                        this.soundDevice.Dispose();
                    }
                }
            }
        }
    }

    die Klasse hab ich bei mir im Projeject eingebunden und ich hab 1. einen fehler, wo er die Notification Events setzt. Bei
    Code :
    1
    
    posNotify[0].EventNotifyHandle = fillEvent[0].SafeWaitHandle;
    sollen das unterschiedliche typen sein.
    Naja das hab ich erstmal auskommentiert.

    Die eigentliche Frage ist wie ruf ich die Klasse denn auf das der mal nen ton von sich gibt
     

Ähnliche Themen

  1. UVW-Tag erzeugen!
    Von nixmehrfrei im Forum Cinema 4D
    Antworten: 3
    Letzter Beitrag: 19.06.10, 13:30
  2. PDF erzeugen mit VB.net
    Von AvalanchRider im Forum .NET Application und Service Design
    Antworten: 2
    Letzter Beitrag: 04.01.07, 15:46
  3. dll für ASP erzeugen
    Von JohnDoe im Forum .NET Café
    Antworten: 0
    Letzter Beitrag: 04.07.06, 09:22
  4. Mit AS Box erzeugen
    Von imini im Forum Flash Plattform
    Antworten: 4
    Letzter Beitrag: 08.04.05, 12:45
  5. *.bik erzeugen
    Von Dr_Ogen im Forum Videoschnitt, Videotechnik & -produktion
    Antworten: 7
    Letzter Beitrag: 20.02.03, 12:48