24 May '13 - 12:31 *
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: Recording line-in  (Read 862 times)
gabrielf
Posts: 5


« on: 22 Feb '12 - 19:44 »
Reply with quoteQuote

Hi there,
i'm trying to reproduce the sound that enters my line-in. My code looks like this (C#):

        private void Form1_Load(object sender, EventArgs e)
        {
            Bass.BASS_Init(-1, 44100, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero);
           
            Bass.BASS_RecordInit(-1);

            _myStreamCreate = new RECORDPROC(RecordProc);

            output = Bass.BASS_StreamCreate(44100, 2, 0, BASSStreamProc.STREAMPROC_PUSH); // create a "push" stream to play recorded data

            input = Bass.BASS_RecordStart(44100, 2, 0, _myStreamCreate, IntPtr.Zero); // start recording with a RECORDPROC         
           
            Bass.BASS_ChannelPlay(output, false); // start the output
           
        }


        private bool RecordProc(int handle, IntPtr buffer, int length, IntPtr user)
        {
            Bass.BASS_StreamPutData(output, buffer, length);
            return true;
        }

This works, but not the way it should. The output is kind of "truncated", i get only few pieces of the sound being inputed.
I tried to enable the BASSFlag.BASS_MUSIC_PRESCAN on the BASS_RecordStart method and it worked, but not for long. After a few minutes of reproducing I get the same noisy sound.
Any suggestions?
Sorry about my english, it's not my native language.
Thanks in advance.
Gabriel Farias
Logged
gnag
Posts: 160


« Reply #1 on: 22 Feb '12 - 21:49 »
Reply with quoteQuote

Hello Gabriel,
I dont think the BASS_MUSIC_PRESCAN is relevant to you, it is used for loading Mod Files for example.

Using your example I had no Problem, I connected my Cellphone with my Computer's Line In, Headset on my Computer's Speaker Output, played Music on the Phone and was able to listen to it correctly on my Headset.

So it was like this:
Cellphone Audio Out -> Line In -> Computer -> Bass/C# -> Audio Out -> Headset

You have to be careful to NOT be too loud, as soon as I set my Phones Volume some Steps Louder the Quality turned from normal to horrible, Voices got distorted etc.

Did you check the Windows Volume Level and Volume Boost Settings of your Input ?
I have set 100% Volume on my Line In.
Logged
gabrielf
Posts: 5


« Reply #2 on: 23 Feb '12 - 00:59 »
Reply with quoteQuote

I dont think the BASS_MUSIC_PRESCAN is relevant to you, it is used for loading Mod Files for example.

I totally agree with you. But if i don't use it, the output sound is terrible. 

So it was like this:
Cellphone Audio Out -> Line In -> Computer -> Bass/C# -> Audio Out -> Headset

This is exactly my scenario:
Cellphone Audio Out -> Line In (Computer with Delta 1010) -> Audio Out -> HeadSet

Pretty much like yours, right? 

Did you check the Windows Volume Level and Volume Boost Settings of your Input ?
I have set 100% Volume on my Line In.

Yes. The strange thing is that the output sound is perfectly clear at the beginning, but after a few minutes it starts distorting (like when the bass volume is too high).
Thank you for your quick reply, gnag.

Any other ideas?
Logged
Ian @ un4seen
Administrator
Posts: 15270


« Reply #3 on: 23 Feb '12 - 15:18 »
Reply with quoteQuote

This works, but not the way it should. The output is kind of "truncated", i get only few pieces of the sound being inputed.
I tried to enable the BASSFlag.BASS_MUSIC_PRESCAN on the BASS_RecordStart method and it worked, but not for long. After a few minutes of reproducing I get the same noisy sound.
Any suggestions?

The BASS_MUSIC_PRESCAN flag doesn't apply to BASS_RecordStart. What you're actually doing with that flag is lowering the recording period (the time between RECORDPROC calls). The HIWORD of the "flags" parameter can be used to set the recording period, and the BASS_MUSIC_PRESCAN flag (0x20000) sets that to 2.

It sounds like the problem you're having may be related to the output running out of data to play, ie. the playback buffer is empty. You could try pre-buffering some data before starting playback. The LIVEFX example (in the BASS package) does that like this...

BASS_INFO bi;
// initialize output, get latency
if (!BASS_Init(-1,44100,BASS_DEVICE_LATENCY,win,NULL)) {
Error("Can't initialize output");
return FALSE;
}

BASS_GetInfo(&bi);
...
// create a stream to play the recording
chan=BASS_StreamCreate(44100,2,0,STREAMPROC_PUSH,0);

// start recording with 10ms period
if (!BASS_RecordInit(-1) || !(rchan=BASS_RecordStart(44100,2,MAKELONG(0,10),RecordingCallback,0))) {
BASS_RecordFree();
BASS_Free();
Error("Can't initialize recording");
return FALSE;
}
...
{ // prebuffer at least "minbuf" amount of data before starting playback
DWORD prebuf=BASS_ChannelSeconds2Bytes(chan,bi.minbuf/1000.f);
while (BASS_ChannelGetData(chan,NULL,BASS_DATA_AVAILABLE)<prebuf)
Sleep(1);
}
BASS_ChannelPlay(chan,FALSE);

The strange thing is that the output sound is perfectly clear at the beginning, but after a few minutes it starts distorting (like when the bass volume is too high).

If the problem only happens after some time, then it could be that the recording and playback are going at slightly different speeds (are both using the same soundcard?), eg. if the playback is going faster than the recording then it will eventually run out of data to play and start stuttering. Does the noise you hear sound like stuttering?
Logged
gabrielf
Posts: 5


« Reply #4 on: 28 Feb '12 - 19:08 »
Reply with quoteQuote

Hi Ian,
thank you for your reply.
That is exactly what I think it's happening (i'm using the in line from my onboard soundcard and the output of my Delta 1010 soundcard) but I just can't get it to work.
Seems like I don't know how to pre buff data.
I could not translate your code in c#, can someone help me? Here is my code:
public partial class Form1 : Form
    {
        BASS_INFO info;
        private RECORDPROC _myStreamCreate;
        int mxr = 0;
        int output = 0;
        int input = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Bass.BASS_Init(-1, 44100, BASSInit.BASS_DEVICE_LATENCY, IntPtr.Zero);

            info = Bass.BASS_GetInfo();

            _myStreamCreate = new RECORDPROC(RecordProc);

            output = Bass.BASS_StreamCreate(44100, 2, 0, BASSStreamProc.STREAMPROC_PUSH); // create a "push" stream to play recorded data

            if (!Bass.BASS_RecordInit(-1) || Bass.BASS_RecordStart(44100, 2, 0, _myStreamCreate, IntPtr.Zero) == 0) // start recording with a RECORDPROC   
            {
                Bass.BASS_RecordFree();
                Bass.BASS_Free();
                MessageBox.Show("Can't initialize recording");
            }
           
            Bass.BASS_ChannelPlay(output, false); // start the output   
        }


        private bool RecordProc(int handle, IntPtr buffer, int length, IntPtr user)
        {
            Bass.BASS_StreamPutData(output, buffer, length);
            return true;
        }
    }

Thanks
Logged
Ian @ un4seen
Administrator
Posts: 15270


« Reply #5 on: 29 Feb '12 - 14:56 »
Reply with quoteQuote

I'm not a .Net user myself, but I think the translated BASS_RecordStart call would look like this...

            if (!Bass.BASS_RecordInit(-1) || Bass.BASS_RecordStart(44100, 2, Utils.MakeLong(0, 10), _myStreamCreate, IntPtr.Zero) == 0)

And the pre-buffering would look something like this...

int prebuf=Bass.BASS_ChannelSeconds2Bytes(output, info.minbuf/1000.f);
while (Bass.BASS_ChannelGetData(output, IntPtr.Zero, BASSData.BASS_DATA_AVAILABLE)<prebuf)
Thread.Sleep(1);

If the input and output devices are going at different speeds, then you may also need to adjust the output rate (via its BASS_ATTRIB_FREQ setting) to try to get it to match the input rate. That is quite tricky, particularly if you're aiming for low latency. You would monitor the output buffer fill level in the RECORDPROC, and if you see it rising then increase the output rate slightly, and vice versa.
Logged
gabrielf
Posts: 5


« Reply #6 on: 1 Mar '12 - 13:34 »
Reply with quoteQuote

Ok, looks just like my attempt to translate the code, but it still didn't work.
Can you please provide some example on how to monitor the output buffer level and increase or decrease the rate? Or maybe point where I could get a piece of code to try to work it out.

Thank you VERY MUCH
Logged
Ian @ un4seen
Administrator
Posts: 15270


« Reply #7 on: 2 Mar '12 - 13:52 »
Reply with quoteQuote

Here is a modified LIVEFX example to try, which will monitor the buffer level and adjust the output rate accordingly...

   www.un4seen.com/stuff/livefx.zip

If you still get interruptions/distortion, you can try increasing the "prebuf" level.
Logged
gabrielf
Posts: 5


« Reply #8 on: 2 Mar '12 - 20:36 »
Reply with quoteQuote

Thank you VERY MUCH for all your help Ian.

Faith in humanity: restored.
Logged
Pages: [1]
  Reply  |  Print  
 
Jump to:  

Powered by SMF 1.1.18 | SMF © 2013, Simple Machines