mpg123_outblock() private method

private mpg123_outblock ( IntPtr mh ) : int
mh IntPtr
return int
    public void StartImport(String mPath)
    {
        source = (AudioSource)gameObject.GetComponent(typeof(AudioSource));
        if (source == null)
        {
            source = (AudioSource)gameObject.AddComponent <AudioSource>();
        }
        MPGImport.mpg123_init();
        handle_mpg = MPGImport.mpg123_new(null, errPtr);
        x          = MPGImport.mpg123_open(handle_mpg, mPath);
        MPGImport.mpg123_getformat(handle_mpg, out rate, out channels, out encoding);
        intRate     = rate.ToInt32();
        intChannels = channels.ToInt32();
        intEncoding = encoding.ToInt32();

        MPGImport.mpg123_id3(handle_mpg, out id3v1, out id3v2);
        MPGImport.mpg123_format_none(handle_mpg);
        MPGImport.mpg123_format(handle_mpg, intRate, intChannels, 208);

        FrameSize = MPGImport.mpg123_outblock(handle_mpg);
        byte[] Buffer = new byte[FrameSize];
        lengthSamples = MPGImport.mpg123_length(handle_mpg);

        myClip = AudioClip.Create("myClip", lengthSamples, intChannels, intRate, false, false);

        int importIndex = 0;

        while (0 == MPGImport.mpg123_read(handle_mpg, Buffer, FrameSize, out done))
        {
            float[] fArray;
            fArray = ByteToFloat(Buffer);

            myClip.SetData(fArray, (importIndex * fArray.Length) / 2);

            importIndex++;
        }

        MPGImport.mpg123_close(handle_mpg);

        print("done loading");
        myClip.name = Path.GetFileName(mPath);
        clips.Add(myClip);
    }
示例#2
0
    public void MP3(string mPath)
    {
        if (mPath.Length != 0)
        {
            MPGImport.mpg123_init();
            handle_mpg = MPGImport.mpg123_new(null, errPtr);
            x          = MPGImport.mpg123_open(handle_mpg, mPath);
            MPGImport.mpg123_getformat(handle_mpg, out rate, out channels, out encoding);
            intRate     = rate.ToInt32();
            intChannels = channels.ToInt32();
            intEncoding = encoding.ToInt32();

            MPGImport.mpg123_id3(handle_mpg, out id3v1, out id3v2);
            MPGImport.mpg123_format_none(handle_mpg);
            MPGImport.mpg123_format(handle_mpg, intRate, intChannels, 208);

            FrameSize = MPGImport.mpg123_outblock(handle_mpg);
            byte[] Buffer = new byte[FrameSize];
            lengthSamples = MPGImport.mpg123_length(handle_mpg);

            myClip = AudioClip.Create(mPath, lengthSamples, intChannels, intRate, false, false);

            int importIndex = 0;

            while (0 == MPGImport.mpg123_read(handle_mpg, Buffer, FrameSize, out done))
            {
                float[] fArray;
                fArray = ByteToFloat(Buffer);

                myClip.SetData(fArray, (importIndex * fArray.Length) / 2);

                importIndex++;
            }

            MPGImport.mpg123_close(handle_mpg);

            audioSource.clip = myClip;
            audioSource.loop = true;
            audioSource.Play();
        }
    }
示例#3
0
    private const double const_1_div_2147483648_ = 1.0 / 2147483648.0; // 32 bit
    #endregion

    public static AudioClip StartImport(string path)
    {
        MPGImport.mpg123_init();
        handle_mpg = MPGImport.mpg123_new(null, errPtr);
        int x = MPGImport.mpg123_open(handle_mpg, path);

        MPGImport.mpg123_getformat(handle_mpg, out rate, out channels, out encoding);
        int intRate     = rate.ToInt32();
        int intChannels = channels.ToInt32();
        int intEncoding = encoding.ToInt32();

        MPGImport.mpg123_id3(handle_mpg, out id3v1, out id3v2);
        MPGImport.mpg123_format_none(handle_mpg);
        MPGImport.mpg123_format(handle_mpg, intRate, intChannels, 208);

        int FrameSize = MPGImport.mpg123_outblock(handle_mpg);

        byte[] Buffer        = new byte[FrameSize];
        int    lengthSamples = MPGImport.mpg123_length(handle_mpg);

        AudioClip myClip = AudioClip.Create(Path.GetFileNameWithoutExtension(path), lengthSamples, intChannels, intRate, false, false);

        int importIndex = 0;

        while (0 == MPGImport.mpg123_read(handle_mpg, Buffer, FrameSize, out done))
        {
            float[] fArray;
            fArray = ByteToFloat(Buffer);

            myClip.SetData(fArray, (importIndex * fArray.Length) / 2);

            importIndex++;
        }

        MPGImport.mpg123_close(handle_mpg);

        return(myClip);
    }
示例#4
0
        static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.WriteLine("Please pass in a mp3 file");
            }

            if (SDL.SDL_Init(SDL.SDL_INIT_EVERYTHING) < 0)
            {
                Console.WriteLine(SDL.SDL_GetError());

                Environment.Exit(1);
            }

            if (SDL_mixer.Mix_Init(SDL_mixer.MIX_InitFlags.MIX_INIT_MP3) < 0)
            {
                Console.WriteLine(SDL.SDL_GetError());

                Environment.Exit(1);
            }

            IntPtr window = SDL.SDL_CreateWindow("Yay", SDL.SDL_WINDOWPOS_CENTERED, SDL.SDL_WINDOWPOS_CENTERED, 1024, 600, SDL.SDL_WindowFlags.SDL_WINDOW_SHOWN);

            if (window == IntPtr.Zero)
            {
                Console.WriteLine(SDL.SDL_GetError());

                Console.WriteLine("Sad: Window");

                Environment.Exit(1);
            }

            IntPtr renderer = SDL.SDL_CreateRenderer(window, -1, SDL.SDL_RendererFlags.SDL_RENDERER_ACCELERATED | SDL.SDL_RendererFlags.SDL_RENDERER_PRESENTVSYNC);

            if (renderer == IntPtr.Zero)
            {
                Console.WriteLine(SDL.SDL_GetError());

                Console.WriteLine("Sad: Renderer");

                Environment.Exit(1);
            }

            if (SDL_mixer.Mix_OpenAudio(44100, SDL_mixer.MIX_DEFAULT_FORMAT, 2, 2048) < 0)
            {
                Console.WriteLine(SDL.SDL_GetError());

                Console.WriteLine("Sad: Mixer");

                Environment.Exit(1);
            }

            bool mp3Done = false;

            IntPtr joy = SDL_mixer.Mix_LoadMUS(args[0]);

            if (joy == IntPtr.Zero)
            {
                Console.WriteLine(SDL.SDL_GetError());

                Console.WriteLine("Sad: Mix_LoadMUS");

                Environment.Exit(1);
            }

            MPGImport.mpg123_init();

            Int32 err = 0;

            GCHandle handleErr = GCHandle.Alloc(err, GCHandleType.Pinned);

            IntPtr ptrErr = GCHandle.ToIntPtr(handleErr);

            IntPtr handle_mpg = MPGImport.mpg123_new(null, ptrErr);
            int    x          = MPGImport.mpg123_open(handle_mpg, args[0]);

            handleErr.Free();

            MPGImport.mpg123_format_none(handle_mpg);
            MPGImport.mpg123_format(handle_mpg, 44100, 2, (int)MPGImport.mpg123_enc_enum.MPG123_ENC_SIGNED_16);

            int FrameSize = MPGImport.mpg123_outblock(handle_mpg);

            byte[] Buffer        = new byte[FrameSize];
            int    lengthSamples = MPGImport.mpg123_length(handle_mpg);

            if (SDL_mixer.Mix_PlayMusic(joy, 0) < 0)
            {
                Console.WriteLine(SDL.SDL_GetError());

                Console.WriteLine("Sad: Mix_PlayMusic");

                Environment.Exit(1);
            }

            object fun = new object();

            List <Int16> LeftChannels  = new List <Int16>();
            List <Int16> RightChannels = new List <Int16>();

            SDL_mixer.Mix_HookMusic((uData, stream, len) => {
                byte[] mixData       = new byte[len];
                byte[] managedStream = new byte[len];

                Marshal.Copy(stream, managedStream, 0, len);

                IntPtr done = IntPtr.Zero;

                if (MPGImport.mpg123_read(handle_mpg, managedStream, len, out done) is int hello)
                {
                    if (hello == -12)
                    {
                        mp3Done = true;
                    }
                    else if (hello == 0)
                    {
                        // foreach (var b in managedStream.Select((value, idx) => (value, idx)).Where((value, idx) => idx % 4 == 0)) {
                        Int16 leftChannel;
                        Int16 rightChannel;

                        byte[] datum = new byte[2];

                        datum[0]    = managedStream[0];
                        datum[1]    = managedStream[1];
                        leftChannel = BitConverter.ToInt16(datum, 0);

                        datum[0]     = managedStream[2];
                        datum[1]     = managedStream[3];
                        rightChannel = BitConverter.ToInt16(datum, 0);

                        leftChannel  = (Int16)Math.Sqrt(leftChannel);
                        rightChannel = (Int16)Math.Sqrt(rightChannel);

                        lock (fun) {
                            LeftChannels.Add(leftChannel);
                            RightChannels.Add(rightChannel);
                        }
                        // }
                    }
                }

                Marshal.Copy(managedStream, 0, stream, len);
            }, IntPtr.Zero);

            SDL.SDL_Event e;
            bool          quit = false;

            while (!quit)
            {
                while (SDL.SDL_WaitEventTimeout(out e, 10) != 0)
                {
                    switch (e.type)
                    {
                    case SDL.SDL_EventType.SDL_KEYDOWN:
                        switch (e.key.keysym.sym)
                        {
                        case SDL.SDL_Keycode.SDLK_ESCAPE:
                            quit = true;

                            break;
                        }

                        break;

                    case SDL.SDL_EventType.SDL_QUIT:
                        quit = true;

                        break;
                    }
                }

                if (mp3Done)
                {
                    quit = true;
                }
                else
                {
                    SDL.SDL_RenderClear(renderer);
                    SDL.SDL_SetRenderDrawColor(renderer, 218, 112, 214, 255);

                    // 40 is perfect.. ;)
                    lock (fun) {
                        foreach (var channel in LeftChannels.Select((value, idx) => (value, idx)))
                        {
                            if (channel.idx > 40)
                            {
                                continue;
                            }

                            SDL.SDL_Rect rect = new SDL.SDL_Rect()
                            {
                                x = channel.idx * 20,
                                y = 300,
                                w = 15,
                                h = -channel.value
                            };

                            SDL.SDL_RenderDrawRect(renderer, ref rect);
                        }

                        if (LeftChannels.Count > 40)
                        {
                            LeftChannels.RemoveAt(0);
                        }

                        foreach (var channel in RightChannels.Select((value, idx) => (value, idx)))
                        {
                            if (channel.idx > 40)
                            {
                                continue;
                            }

                            SDL.SDL_Rect rect = new SDL.SDL_Rect()
                            {
                                x = channel.idx * 20,
                                y = 315,
                                w = 15,
                                h = channel.value
                            };

                            SDL.SDL_RenderDrawRect(renderer, ref rect);
                        }

                        if (RightChannels.Count > 40)
                        {
                            RightChannels.RemoveAt(0);
                        }
                    }

                    SDL.SDL_Rect r = new SDL.SDL_Rect()
                    {
                        x = 0,
                        y = 300,
                        w = 1024,
                        h = 10
                    };

                    // SDL.SDL_RenderClear(renderer);
                    // SDL.SDL_SetRenderDrawColor(renderer, 218, 112, 214, 255);
                    SDL.SDL_RenderDrawRect(renderer, ref r);

                    SDL.SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
                    SDL.SDL_RenderPresent(renderer);
                }
            }

            SDL_mixer.Mix_FreeMusic(joy);

            SDL.SDL_Quit();
        }
    public AudioClip StartImport(string mPath)
    {
        MPGImport.mpg123_init();
        handle_mpg = MPGImport.mpg123_new(null, errPtr);
        try
        {
            x = MPGImport.mpg123_open(handle_mpg, mPath);
            MPGImport.mpg123_getformat(handle_mpg, out rate, out channels, out encoding);
            intRate     = rate.ToInt32();
            intChannels = channels.ToInt32();
            intEncoding = encoding.ToInt32();
            MPGImport.mpg123_id3(handle_mpg, out id3v1, out id3v2);
            MPGImport.mpg123_format_none(handle_mpg);
            MPGImport.mpg123_format(handle_mpg, intRate, intChannels, 208);

            string title;
            if (id3v1 != IntPtr.Zero)
            {
                Debug.Log("Getting ID3 info");
                MPGImport.mpg123_id3v1 v1 = (MPGImport.mpg123_id3v1)Marshal.PtrToStructure(id3v1, typeof(MPGImport.mpg123_id3v1));
                title = new String(v1.title);
            }
            else
            {
                title = Path.GetFileNameWithoutExtension(mPath);
            }

            FrameSize = MPGImport.mpg123_outblock(handle_mpg);
            byte[] Buffer = new byte[FrameSize];
            lengthSamples = MPGImport.mpg123_length(handle_mpg);

            Debug.Log("Creating audio clip");
            myClip = AudioClip.Create(title, lengthSamples, intChannels, intRate, false);

            int importIndex = 0;

            while (0 == MPGImport.mpg123_read(handle_mpg, Buffer, FrameSize, out done))
            {
                float[] fArray;
                fArray = ByteToFloat(Buffer);
                float offset = (importIndex * fArray.Length) / 2;
                if (offset > lengthSamples)
                {
                    Debug.LogWarning("[STED] MP3 file " + mPath + " is of an unexpected length and was truncated.");
                    break; // File was reported as shorter than it is. Salvage what we have and return.
                }
                myClip.SetData(fArray, (int)offset);
                importIndex++;
            }
        }
        catch (Exception ex)
        {
            // Attempt to dump any used memory before continuing.
            // TODO: Still holds onto memory when repeatedy failing.
            myClip.UnloadAudioData();
            myClip = null;
            throw ex;
        }
        finally
        {
            MPGImport.mpg123_close(handle_mpg);
        }
        return(myClip);
    }
示例#6
0
    IEnumerator ShowLoadDialogCoroutine()
    {
        // Show a load file dialog and wait for a response from user
        // Load file/folder: file, Initial path: default (Documents), Title: "Load File", submit button text: "Load"
        yield return(FileBrowser.WaitForLoadDialog(false, null, "Load File", "Load"));

        // Dialog is closed
        // Print whether a file is chosen (FileBrowser.Success)
        // and the path to the selected file (FileBrowser.Result) (null, if FileBrowser.Success is false)
        Debug.Log(FileBrowser.Success + " " + FileBrowser.Result);

        if (FileBrowser.Success)
        {
            // If a file was chosen, read its bytes via FileBrowserHelpers
            // Contrary to File.ReadAllBytes, this function works on Android 10+, as well
            mPath = FileBrowser.Result;
            Debug.Log(mPath);

            if (mPath.Length != 0)
            {
                Image o = Instantiate(prefab, transform);
                Debug.Log(mPath);

                //audioSource = (AudioSource)gameObject.GetComponent(typeof(AudioSource));
                //if (audioSource == null) audioSource = (AudioSource)gameObject.AddComponent<AudioSource>();


                MPGImport.mpg123_init();
                handle_mpg = MPGImport.mpg123_new(null, errPtr);
                x          = MPGImport.mpg123_open(handle_mpg, mPath);
                MPGImport.mpg123_getformat(handle_mpg, out rate, out channels, out encoding);
                intRate     = rate.ToInt32();
                intChannels = channels.ToInt32();
                intEncoding = encoding.ToInt32();

                MPGImport.mpg123_id3(handle_mpg, out id3v1, out id3v2);
                MPGImport.mpg123_format_none(handle_mpg);
                MPGImport.mpg123_format(handle_mpg, intRate, intChannels, 208);

                FrameSize = MPGImport.mpg123_outblock(handle_mpg);
                byte[] Buffer = new byte[FrameSize];
                lengthSamples = MPGImport.mpg123_length(handle_mpg);

                myClip = AudioClip.Create(mPath, lengthSamples, intChannels, intRate, false, false);

                int importIndex = 0;

                while (0 == MPGImport.mpg123_read(handle_mpg, Buffer, FrameSize, out done))
                {
                    float[] fArray;
                    fArray = ByteToFloat(Buffer);

                    myClip.SetData(fArray, (importIndex * fArray.Length) / 2);

                    importIndex++;
                }

                MPGImport.mpg123_close(handle_mpg);

                audioSource.clip = myClip;
                audioSource.loop = true;

                //audioSource.Play();
            }
        }
    }