Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Problem accessing a function in a nested class

Problem accessing a function in a nested class

Scheduled Pinned Locked Moved C#
helpquestioncsharpjson
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    crushinghellhammer
    wrote on last edited by
    #1

    I was checking out Ianier Munoz's implementation of streaming audio input using the DirectSound API and C# .NET. My question has to do with a problem I'm facing in C#. The function I want to access is declared as follows: [c] public void Play(nsStreamPlayer.PullAudioCallback pullAudio) { // Bunch of stuff } [/c] nsStreamPlayer is a namespace in which a delegate and an interface are defined. [c] namespace nsStreamPlayer { /// /// Delegate used to fill in a buffer /// public delegate void PullAudioCallback(IntPtr data, int count); /// /// Audio player interface /// public interface IAudioPlayer : IDisposable { int SamplingRate { get; } int BitsPerSample { get; } int Channels { get; } int GetBufferedSize(); void Play(PullAudioCallback onAudioData); void Stop(); } } [/c] The "Play" function is defined in a nested private class [c] private class PullStream : Stream [/c] which itself belongs to a public class StreamingPlayer [c] public class StreamingPlayer : IDisposable, nsStreamPlayer.IAudioPlayer [/c] NOW: On my mainform designer I have a button called btnStartStream. I want to write the code such that clicking btnStartStream calls the play function. How do I do this? Everything I've tried throws up an error message. I know I'm missing something basic. Could you please help me out? Thanks a lot for your time!

    N 1 Reply Last reply
    0
    • C crushinghellhammer

      I was checking out Ianier Munoz's implementation of streaming audio input using the DirectSound API and C# .NET. My question has to do with a problem I'm facing in C#. The function I want to access is declared as follows: [c] public void Play(nsStreamPlayer.PullAudioCallback pullAudio) { // Bunch of stuff } [/c] nsStreamPlayer is a namespace in which a delegate and an interface are defined. [c] namespace nsStreamPlayer { /// /// Delegate used to fill in a buffer /// public delegate void PullAudioCallback(IntPtr data, int count); /// /// Audio player interface /// public interface IAudioPlayer : IDisposable { int SamplingRate { get; } int BitsPerSample { get; } int Channels { get; } int GetBufferedSize(); void Play(PullAudioCallback onAudioData); void Stop(); } } [/c] The "Play" function is defined in a nested private class [c] private class PullStream : Stream [/c] which itself belongs to a public class StreamingPlayer [c] public class StreamingPlayer : IDisposable, nsStreamPlayer.IAudioPlayer [/c] NOW: On my mainform designer I have a button called btnStartStream. I want to write the code such that clicking btnStartStream calls the play function. How do I do this? Everything I've tried throws up an error message. I know I'm missing something basic. Could you please help me out? Thanks a lot for your time!

      N Offline
      N Offline
      Nick Parker
      wrote on last edited by
      #2

      You have an instance of the StreamingPlayer class, correct?

      StreamingPlayer stmPlyr = new StreamingPlayer();
      private void btnStartStream_Click(object sender, EventArgs e)
      {
      PullAudioCallback cbk = new PullAudioCallback(someAudioCallBack);
      stmPlyr.Play(cbk);
      }

      private void someAudioCallBack(IntPtr data, int count)
      {
      // something here.
      }

      - Nick Parker
      My Blog | My Articles

      C 1 Reply Last reply
      0
      • N Nick Parker

        You have an instance of the StreamingPlayer class, correct?

        StreamingPlayer stmPlyr = new StreamingPlayer();
        private void btnStartStream_Click(object sender, EventArgs e)
        {
        PullAudioCallback cbk = new PullAudioCallback(someAudioCallBack);
        stmPlyr.Play(cbk);
        }

        private void someAudioCallBack(IntPtr data, int count)
        {
        // something here.
        }

        - Nick Parker
        My Blog | My Articles

        C Offline
        C Offline
        crushinghellhammer
        wrote on last edited by
        #3

        Nick, thanks for the reply! However, I still have a slight problem. The constructor for StreamingPlayer needs three arguments - owner, DirectSound playback device and waveformat. How do I include these in the function call? They are set in the function definition, i.e. public StreamingPlayer(Control owner, Device device, WaveFormat format) { m_Device = device; if (m_Device == null) { m_Device = new Device(); m_Device.SetCooperativeLevel(owner, CooperativeLevel.Normal); m_OwnsDevice = true; } BufferDescription desc = new BufferDescription(format); desc.BufferBytes = format.AverageBytesPerSecond; desc.ControlVolume = true; desc.GlobalFocus = true; m_Buffer = new SecondaryBuffer(desc, m_Device); m_BufferBytes = m_Buffer.Caps.BufferBytes; m_Timer = new System.Timers.Timer(BytesToMs(m_BufferBytes) / 6); m_Timer.Enabled = false; m_Timer.Elapsed += new System.Timers.ElapsedEventHandler(Timer_Elapsed); }

        C 1 Reply Last reply
        0
        • C crushinghellhammer

          Nick, thanks for the reply! However, I still have a slight problem. The constructor for StreamingPlayer needs three arguments - owner, DirectSound playback device and waveformat. How do I include these in the function call? They are set in the function definition, i.e. public StreamingPlayer(Control owner, Device device, WaveFormat format) { m_Device = device; if (m_Device == null) { m_Device = new Device(); m_Device.SetCooperativeLevel(owner, CooperativeLevel.Normal); m_OwnsDevice = true; } BufferDescription desc = new BufferDescription(format); desc.BufferBytes = format.AverageBytesPerSecond; desc.ControlVolume = true; desc.GlobalFocus = true; m_Buffer = new SecondaryBuffer(desc, m_Device); m_BufferBytes = m_Buffer.Caps.BufferBytes; m_Timer = new System.Timers.Timer(BytesToMs(m_BufferBytes) / 6); m_Timer.Enabled = false; m_Timer.Elapsed += new System.Timers.ElapsedEventHandler(Timer_Elapsed); }

          C Offline
          C Offline
          crushinghellhammer
          wrote on last edited by
          #4

          Sorry, disregard that...that was a really dumb question. Been working on this too long...not thinking straight. BUT: PullAudioCallback is a delegate defined as public delegate void PullAudioCallback(IntPtr data, int count); When I instantiate a PullAudioCallback object, PullAudioCallback cbk = new PullAudioCallback(...) I need arguments for data and count. What should these be? I tried digging through the source code but I don't see where he calls this function explicitly, though it is integral to the application.

          N 1 Reply Last reply
          0
          • C crushinghellhammer

            Sorry, disregard that...that was a really dumb question. Been working on this too long...not thinking straight. BUT: PullAudioCallback is a delegate defined as public delegate void PullAudioCallback(IntPtr data, int count); When I instantiate a PullAudioCallback object, PullAudioCallback cbk = new PullAudioCallback(...) I need arguments for data and count. What should these be? I tried digging through the source code but I don't see where he calls this function explicitly, though it is integral to the application.

            N Offline
            N Offline
            Nick Parker
            wrote on last edited by
            #5

            crushinghellhammer wrote: I need arguments for data and count. What should these be? It's a delegate which means that it's instance has to represent that signature it defines. Take a look at my example, the example function matches that method signature. Does that make sense? - Nick Parker
            My Blog | My Articles

            C 1 Reply Last reply
            0
            • N Nick Parker

              crushinghellhammer wrote: I need arguments for data and count. What should these be? It's a delegate which means that it's instance has to represent that signature it defines. Take a look at my example, the example function matches that method signature. Does that make sense? - Nick Parker
              My Blog | My Articles

              C Offline
              C Offline
              crushinghellhammer
              wrote on last edited by
              #6

              I see now! Sorry if this is exasperating...I'm new to C# and sometimes reading other people's code (in this case Ianier's) is a little confusing. My understanding of his intention in using PullAudioCallback is that the delegate is supposed to wait for count bytes to be copied to the data buffer which is an IntPtr before it plays these bytes. What exactly is IntPtr...is it a pointer to a byte array? What would the contents of "someAudioCallBack(IntPtr data, int count" be? Thanks again!

              N 1 Reply Last reply
              0
              • C crushinghellhammer

                I see now! Sorry if this is exasperating...I'm new to C# and sometimes reading other people's code (in this case Ianier's) is a little confusing. My understanding of his intention in using PullAudioCallback is that the delegate is supposed to wait for count bytes to be copied to the data buffer which is an IntPtr before it plays these bytes. What exactly is IntPtr...is it a pointer to a byte array? What would the contents of "someAudioCallBack(IntPtr data, int count" be? Thanks again!

                N Offline
                N Offline
                Nick Parker
                wrote on last edited by
                #7

                crushinghellhammer wrote: What exactly is IntPtr IntPtr Structure[^] - Nick Parker
                My Blog | My Articles

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups