1

我在这里找到的这段代码创建了给定文件的频谱图,但它让我在播放和绘制频谱图时等待。

我需要修改此代码以立即创建频谱图,而不播放文件。

提前致谢。

public partial class Form1 : Form
{
    private int _handle;
    private int _pos;
    private BASSTimer _timer;
    private Visuals _visuals;

    public Form1()
    {
        InitializeComponent();
    }

    private void timer_Tick(object sender, EventArgs e)
    {
        bool spectrum3DVoicePrint = _visuals.CreateSpectrum3DVoicePrint(_handle, pictureBox1.CreateGraphics(),
                                                                        pictureBox1.Bounds, Color.Cyan, Color.Green,
                                                                        _pos, false, true);
        _pos++;
        if (_pos >= pictureBox1.Width)
        {
            _pos = 0;
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        string file = "..\\..\\mysong.mp3";
        if (Bass.BASS_Init(-1, 44100, BASSInit.BASS_DEVICE_DEFAULT, Handle))
        {
            _handle = Bass.BASS_StreamCreateFile(file, 0, 0, BASSFlag.BASS_DEFAULT);

            if (Bass.BASS_ChannelPlay(_handle, false))
            {
                _visuals = new Visuals();
                _timer = new BASSTimer((int) (1.0d/10*1000));
                _timer.Tick += timer_Tick;
                _timer.Start();
            }
        }
    }
}
4

1 回答 1

1

这是我解决问题的方法。这个想法是你不应该听整个记录,你可以移动音频光标并在某些点评估频谱。

    private Bitmap DrawSpectrogram(string fileName, int height, int stepsPerSecond)
    {
        Bass.BASS_Init(-1, 44100, BASSInit.BASS_DEVICE_DEFAULT, Handle);
        int channel = Bass.BASS_StreamCreateFile(fileName, 0, 0, BASSFlag.BASS_DEFAULT);

        long len = Bass.BASS_ChannelGetLength(channel, BASSMode.BASS_POS_BYTES); // the length in bytes
        double time = Bass.BASS_ChannelBytes2Seconds(channel, len); // the length in seconds

        int steps = (int)Math.Floor(stepsPerSecond * time);

        Bitmap result = new Bitmap(steps, height);
        Graphics g = Graphics.FromImage(result);

        Visuals visuals = new Visuals();


        Bass.BASS_ChannelPlay(channel, false);

        for (int i = 0; i < steps; i++)
        {
            Bass.BASS_ChannelSetPosition(channel, 1.0 * i / stepsPerSecond);
            visuals.CreateSpectrum3DVoicePrint(channel, g, new Rectangle(0, 0, result.Width, result.Height), Color.Black, Color.White, i, true, false);
        }

        Bass.BASS_ChannelStop(channel);

        Bass.BASS_Stop();
        Bass.BASS_Free();

        return result;
    }
于 2015-08-27T11:44:07.523 回答