25 May '13 - 05:45 *
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: Weird sounds instead of music  (Read 391 times)
Obrens
Posts: 3


« on: 13 Apr '12 - 21:37 »
Reply with quoteQuote

Hello, I'm trying to make a static class that will be used to write a wav file. It would have one variable, representing the data input, that can be set, and one public method that would be called to write the file.
The WriteFile class(WriteFile.Data and WriteFile.writefile() ) is supposed to take a byte[] and make a .wav(something.wav) file out of it. In the Main I take a song (Tvojneko.mp3) and read it into a byte[] (buffer). And that is what's supposed to be played and written into something.wav. But instead, what's played and written into something.wav is some noise.
static void Main(string[] args)
        {
            try
            {
                FileStream fs = File.OpenRead("Tvojneko.mp3");
                // get the legth of the file
                int length = (int)fs.Length;
                // create the buffer which will keep the file in memory
                byte[] buffer = new byte[length];
                // read the file into the buffer
                fs.Read(buffer, 0, length);
                // buffer is filled, file can be closed
                fs.Close();

                WriteFile.Data = buffer;
                WriteFile.writefile();
                Console.ReadKey();
            }
            catch
            {
                Console.ReadLine();
            }
        }

static class WriteFile
    {
        static byte[] data;
        static public byte[] Data
        {
            get { return data; }
            set { data = value; }
        }

        public static void writefile()
        {
            try
            {
                if (Bass.BASS_Init(-1, 44100, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero))
                {
                    STREAMPROC streamcreate = new STREAMPROC(WaveformProc1);
                    int stream = Bass.BASS_StreamCreate(44100, 2, BASSFlag.BASS_DEFAULT, streamcreate, IntPtr.Zero);
                    BassEnc.BASS_Encode_Start(stream, "something.wav", BASSEncode.BASS_ENCODE_PCM, null, IntPtr.Zero);
                    Bass.BASS_ChannelPlay(stream, false);

                    Console.ReadKey();
                    Bass.BASS_StreamFree(stream);
                    Bass.BASS_Free();
                }
            }
            catch
            {
                Console.ReadLine();
            }
        }

        private static int WaveformProc1(int handle, IntPtr buffer, int length, IntPtr user)
        {
            int waveCalculated = length;
            Marshal.Copy(data, 0, buffer, length);
            return waveCalculated;
        }
    }
I'm sure Tvojneko.mp3 is real music not some noise. I guess you can ignore the try... catch blocks. Yeah, I know I have excessive Console.ReadKey()'s.
It doesn't show any error message when I run the program. It all seems to work perfectly except for the fact that it gives noise instead of Tvojneko.mp3.
« Last Edit: 13 Apr '12 - 22:11 by Obrens » Logged
gnag
Posts: 160


« Reply #1 on: 14 Apr '12 - 15:12 »
Reply with quoteQuote

in your example you need to fill pcm samples, the problem is that you feed the mp3 encoded data, thats why you hear noise i guess,take a look at bass_streamcreatefile
Logged
Obrens
Posts: 3


« Reply #2 on: 14 Apr '12 - 23:48 »
Reply with quoteQuote

Thank thank you very much, gnag. It's working like a charm. And I didn't need the whole class, one method was enough.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Un4seen.Bass;
using System.Runtime.InteropServices;
using Un4seen.Bass.AddOn.Enc;
using System.IO;

namespace BassEnc_klasa
{
    class Program
    {
        static void Main(string[] args)
        {
            FileStream fs = File.OpenRead("Tvojneko.mp3");
            int length = (int)fs.Length;
            byte[] buffer = new byte[length];
            fs.Read(buffer, 0, length);
            fs.Close();

            writefile(buffer, "something");
        }

        static void writefile(byte[] data, string filename)
        {
            if (Bass.BASS_Init(-1, 44100, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero))
            {
                GCHandle gch = GCHandle.Alloc(data, GCHandleType.Pinned);
                int stream = Bass.BASS_StreamCreateFile(gch.AddrOfPinnedObject(), 0L, data.Length, BASSFlag.BASS_SAMPLE_FLOAT);
                BassEnc.BASS_Encode_Start(stream, filename + ".wav", BASSEncode.BASS_ENCODE_PCM, null, IntPtr.Zero);
                Bass.BASS_ChannelPlay(stream, false);

                Console.ReadKey();
                Bass.BASS_StreamFree(stream);
                Bass.BASS_Free();
                gch.Free();
            }
        }
    }
}
Logged
Pages: [1]
  Reply  |  Print  
 
Jump to:  

Powered by SMF 1.1.18 | SMF © 2013, Simple Machines