25 May '13 - 03:40 *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register  
Pages: [1]
  Reply  |  Print  
Author Topic: Create Spectrum from audio device  (Read 659 times)
RiKey
Posts: 3


« on: 8 May '12 - 15:45 »
Reply with quoteQuote

Hi, I'm doing my diploma and I'm stuck on making my spectrum to work Embarrassed Please HELP me Sad
I'm developing it in C# and using system standard TTS engine to make my program talk, and I need graphical interface for it (when program talk there should be spectrum diagram showing it on form).
I creating it by using:
pictureBoxSpectrum.Image = _vis.CreateSpectrumLinePeak(_stream, this.pictureBoxSpectrum.Width, this.pictureBoxSpectrum.Height, Color.Gray, Color.LightBlue, Color.Orange, Color.FromArgb(0, 34, 34, 34), 2, 1, 2, 10, false, false, false);

in "_stream" I must have my device (sound card), it can't be standard filestream!
So the main question - How I could do that? How to put what play my sound card into stream? Maybe it's a stupid question but I didn't find the answer Sad
What I managed to do is trying to record what my sound card playing and putting it to stream, but it doesn't work:
     private void button1_Click(object sender, System.EventArgs e)
      {
            //..........
            Bass.BASS_Init(-1, 44100, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero);
            Bass.BASS_RecordInit(-1);
            _myRecProc = new RECORDPROC(MyRecording);
            int recHandle = Bass.BASS_RecordStart(44100, 1, BASSFlag.BASS_RECORD_PAUSE, _myRecProc, IntPtr.Zero);
            // start recording
            Bass.BASS_ChannelPlay(recHandle, false);
            pictureBoxSpectrum.Image = _vis.CreateSpectrumLinePeak(recHandle, this.pictureBoxSpectrum.Width, this.pictureBoxSpectrum.Height, Color.Gray, Color.LightBlue, Color.Orange, Color.FromArgb(0, 34, 34, 34), 2, 1, 2, 10, false, false, false);

            Bass.BASS_Stop();
            Bass.BASS_Free();
            //..........
        }

         private unsafe bool MyRecording(int handle, IntPtr buffer, int length, IntPtr user)
        {
            bool cont = true;
            if (length > 0 && buffer != IntPtr.Zero)
            {
                // assuming 16-bit sample data here
                short *data = (short*)buffer;
            }
            return cont;
        }
I didn't understand how to record too. Huh
Please help me!

P.S.: Sorry for my english.
Logged
Ian @ un4seen
Administrator
Posts: 15276


« Reply #1 on: 9 May '12 - 16:50 »
Reply with quoteQuote

I'm not a .Net user myself, so I'm afraid I can't advise on that stuff, but it looks like you basically have the right idea, eg. visualising a recording channel (from BASS_RecordStart). What problem are you having with that, eg. is it just that the spectrum display isn't moving with the soundcard output? If so, check the "Sound" control panel to see what is set as the default recording device. It would need to be something like "Stereo Mix" to capture the soundcard's output.
Logged
RiKey
Posts: 3


« Reply #2 on: 10 May '12 - 22:15 »
Reply with quoteQuote

Yes, after I press the button and program speaks my spectrum display got drawn but doesn't moving.
Quote
If so, check the "Sound" control panel to see what is set as the default recording device.
I checked it,... I guess I checked it right

I don't know where to look else.
And the second, I don't fully understand this part of code, why do I need it? What it is doing?
        private unsafe bool MyRecording(int handle, IntPtr buffer, int length, IntPtr user)
        {
            bool cont = true;
            if (length > 0 && buffer != IntPtr.Zero)
            {
                // assuming 16-bit sample data here
                short *data = (short*)buffer;
            }
            return cont;
        }

Thanks for helping.
« Last Edit: 11 May '12 - 00:33 by RiKey » Logged
radio42
Posts: 4012


« Reply #3 on: 11 May '12 - 10:22 »
Reply with quoteQuote

You are calling "pictureBoxSpectrum.Image = _vis.CreateSpectrumLinePeak(...)" only once at startup.
As such it is not redrawn again at any time - which is needed in order to reflect the new sample data currently being processed.
You might create a Timer once you started your recording and in the timer tick event you might call "pictureBoxSpectrum.Image = _vis.CreateSpectrumLinePeak(...)".
The timer might e.g. be triggered every 30 or 50 milliseconds.
As such the spectrum would be redrawn every so often.
E.g. something like this:
private System.Windows.Forms.Timer _updateTimer;
private int _recHandle = 0;
...
_updateTimer.Interval = 30;
_updateTimer.Tick += new System.EventHandler(_updateTimer_Tick);

...
_recHandle = Bass.BASS_RecordStart(44100, 1, BASSFlag.BASS_RECORD_PAUSE, _myRecProc, IntPtr.Zero);
// start recording
Bass.BASS_ChannelPlay(_recHandle, false);
_updateTimer.Enabled = true;
...
// on stop recording disable the timer
_updateTimer.Enabled = false;

...
private void _updateTimer_Tick(object sender, EventArgs e)
{
    pictureBoxSpectrum.Image = _vis.CreateSpectrumLinePeak(_recHandle, this.pictureBoxSpectrum.Width, this.pictureBoxSpectrum.Height, Color.Gray, Color.LightBlue, Color.Orange, Color.FromArgb(0, 34, 34, 34), 2, 1, 2, 10, false, false, false);
}


Your RECORDPROC doesn't need to look like this, it can simply be:
private bool MyRecording(int handle, IntPtr buffer, int length, IntPtr user)
{
    return true;
}
Logged
RiKey
Posts: 3


« Reply #4 on: 11 May '12 - 16:25 »
Reply with quoteQuote

Thanks radio42, I've already changed my RECORDPROC, nevertheless I forgot to include timer and that was the problem  Roll Eyes
Everything works, thanks for helping! Smiley
Logged
Pages: [1]
  Reply  |  Print  
 
Jump to:  

Powered by SMF 1.1.18 | SMF © 2013, Simple Machines