Une petite implantation de la librairie BASS.
Code : Tout sélectionner
// Bass_Dll (c) ZJP //
// Windows 32B >> Copy 'bass_2302.dll' in windows\system32 folder
// Windows 64B >> Copy 'bass_2302.dll' in windows\SysWOW64 folder
//
using UnityEngine;
using System;
using System.Collections;
using System.Runtime.InteropServices;
public class Bass_Dll : MonoBehaviour
{
private int bass_stream = 0;
// ***************************************************************************************
// Initializes an output device.
// ***************************************************************************************
[DllImport ("bass_2302.dll", EntryPoint="BASS_Init")] private static extern int BASS_Init(int bass_device, int bass_freq, int bass_flag, int bass_win, int bass_clsid);
// device The device to use... -1 = default device, 0 = no sound, 1 = first real output device. BASS_GetDeviceDescription can be used to get the total number of devices.
// freq Output sample rate.
// flags Any combination of these flags.
// BASS_DEVICE_8BITS
// BASS_DEVICE_MONO
// BASS_DEVICE_3D Enable 3D functionality.
// BASS_DEVICE_LATENCY.
// BASS_DEVICE_SPEAKERS.
// BASS_DEVICE_NOSPEAKER.
// win The application's main window... 0 = the current foreground window (use this for console applications).
// clsid Class identifier of the object to create, that will be used to initialize DirectSound... NULL = use default.
// Return value. 0 = error / 1 = Init OK
// ***************************************************************************************
// Creates a sample stream from an MP3, MP2, MP1, OGG, WAV, AIFF or plugin supported file on the internet.
// ***************************************************************************************
[DllImport ("bass_2302.dll", EntryPoint="BASS_StreamCreateURL")] private static extern int BASS_StreamCreateURL(string bass_url, int bass_offset, int bass_flag, int bass_proc, int bass_user);
// ***************************************************************************************
// Starts (or resumes) playback of a sample, stream, MOD music, or recording.
// ***************************************************************************************
[DllImport ("bass_2302.dll", EntryPoint="BASS_ChannelPlay")] private static extern void BASS_ChannelPlay(int bass_handle, int bass_restart);
// ***************************************************************************************
// Frees all resources used by the output device, including all its samples, streams, and MOD musics.
// ***************************************************************************************
[DllImport ("bass_2302.dll", EntryPoint="BASS_Free")] private static extern void BASS_Free();
// ***************************************************************************************
// Retrieves the error code for the most recent BASS function call in the current thread.
// ***************************************************************************************
[DllImport ("bass_2302.dll", EntryPoint="BASS_ErrorGetCode")] private static extern int BASS_ErrorGetCode();
// 0 BASS_OK
// 1 BASS_ERROR_MEM
// 2 BASS_ERROR_FILEOPEN
// 3 BASS_ERROR_DRIVER
// 4 BASS_ERROR_BUFLOST
// 5 BASS_ERROR_HANDLE
// 6 BASS_ERROR_FORMAT
// 7 BASS_ERROR_POSITION
// 8 BASS_ERROR_INIT
// 9 BASS_ERROR_START
// 14 BASS_ERROR_ALREADY
// 16 BASS_ERROR_NOPAUSE
// 18 BASS_ERROR_NOCHAN
// 19 BASS_ERROR_ILLTYPE
// 20 BASS_ERROR_ILLPARAM
// 21 BASS_ERROR_NO3D
// 22 BASS_ERROR_NOEAX
// 23 BASS_ERROR_DEVICE
// 24 BASS_ERROR_NOPLAY
// 25 BASS_ERROR_FREQ
// 27 BASS_ERROR_NOTFILE
// 29 BASS_ERROR_NOHW
// 31 BASS_ERROR_EMPTY
// 32 BASS_ERROR_NONET
// 33 BASS_ERROR_CREATE
// 34 BASS_ERROR_NOFX
// 35 BASS_ERROR_PLAYING
// 37 BASS_ERROR_NOTAVAIL
// 38 BASS_ERROR_DECODE
// 39 BASS_ERROR_DX
// 40 BASS_ERROR_TIMEOUT
// 41 BASS_ERROR_FILEFORM
// 42 BASS_ERROR_SPEAKER
// 43 BASS_ERROR_VERSION
// 44 BASS_ERROR_CODEC
// -1 BASS_ERROR_UNKNOWN
// ***************************************************************************************
// Retrieves the current CPU usage of BASS.
// ***************************************************************************************
[DllImport ("bass_2302.dll", EntryPoint="BASS_GetCPU")] private static extern float BASS_GetCPU();
// ***************************************************************************************
// Retrieves the device setting in the current thread.
// ***************************************************************************************
[DllImport ("bass_2302.dll", EntryPoint="BASS_GetDevice")] private static extern int BASS_GetDevice();
// ***************************************************************************************
// Retrieves the version number of the BASS.DLL that is loaded.
// ***************************************************************************************
[DllImport ("bass_2302.dll", EntryPoint="BASS_GetVersion")] private static extern int BASS_GetVersion();
// ***************************************************************************************
// Retrieves the current master volume level.
// ***************************************************************************************
[DllImport ("bass_2302.dll", EntryPoint="BASS_GetVolume")] private static extern int BASS_GetVolume();
// ***************************************************************************************
// Stops the output, pausing all musics/samples/streams.
// ***************************************************************************************
[DllImport ("bass_2302.dll", EntryPoint="BASS_Pause")] private static extern void BASS_Pause();
// ***************************************************************************************
// Sets the device to use for subsequent calls in the current thread.
// ***************************************************************************************
// Return 1 = ok / 0 = error
[DllImport ("bass_2302.dll", EntryPoint="BASS_SetDevice")] private static extern int BASS_SetDevice(int BASS_Device);
// ***************************************************************************************
// Sets the output master volume.
// ***************************************************************************************
// Return 1 = ok / 0 = error
[DllImport ("bass_2302.dll", EntryPoint="BASS_SetVolume")] private static extern int BASS_SetVolume(int BASS_Device);
// ***************************************************************************************
// Starts (or resumes) the output.
// ***************************************************************************************
// Return 1 = ok / 0 = error
[DllImport ("bass_2302.dll", EntryPoint="BASS_Start")] private static extern int BASS_Start();
// ***************************************************************************************
// Stops the output, stopping all musics/samples/streams.
// ***************************************************************************************
// Return 1 = ok / 0 = error
[DllImport ("bass_2302.dll", EntryPoint="BASS_Stop")] private static extern int BASS_Stop();
// ***************************************************************************************
// Creates a sample stream from an MP3, MP2, MP1, OGG, WAV, AIFF or plugin supported file.
// ***************************************************************************************
[DllImport ("bass_2302.dll", EntryPoint="BASS_StreamCreateFile")] private static extern int BASS_StreamCreateFile(int BASS_mem, string BASS_file, int BASS_offset, int BASS_lenght, int BASS_flags);
// mem TRUE = stream the file from memory.
// file Filename (mem = FALSE) or a memory location (mem = TRUE).
// offset File offset to begin streaming from (only used if mem = FALSE).
// length Data length... 0 = use all data up to the end of the file (if mem = FALSE).
// flags Any combination of these flags.
// BASS_SAMPLE_FLOAT Use 32-bit floating-point sample data. See Floating-point channels for info.
// BASS_SAMPLE_MONO Decode/play the stream (MP3/MP2/MP1 only) in mono, reducing the CPU usage (if it was originally stereo). This flag is automatically applied if BASS_DEVICE_MONO was specified when calling BASS_Init.
// BASS_SAMPLE_SOFTWARE Force the stream to not use hardware mixing.
// BASS_SAMPLE_3D Use 3D functionality. This is ignored if BASS_DEVICE_3D wasn't specified when calling BASS_Init. 3D streams must be mono. The SPEAKER flags can not be used together with this flag.
// BASS_SAMPLE_LOOP Loop the file. This flag can be toggled at any time using BASS_ChannelSetFlags.
// BASS_SAMPLE_FX
// requires DirectX 8 or above Enable the old implementation of DirectX 8 effects. See the DX8 effect implementations section for details. Use BASS_ChannelSetFX to add effects to the stream.
// BASS_STREAM_PRESCAN Enable pin-point accurate seeking (to the exact byte) on the MP3/MP2/MP1 stream. This also increases the time taken to create the stream, due to the entire file being pre-scanned for the seek points.
// BASS_STREAM_AUTOFREE Automatically free the stream when playback reaches the end, or when BASS_ChannelStop or BASS_Stop is called.
// BASS_STREAM_DECODE Decode the sample data, without playing it. Use BASS_ChannelGetData to retrieve decoded sample data. The BASS_SAMPLE_3D, BASS_STREAM_AUTOFREE and SPEAKER flags can not be used together with this flag. The BASS_SAMPLE_SOFTWARE and BASS_SAMPLE_FX flags are also ignored.
// BASS_SPEAKER_xxx Speaker assignment flags.
// Return value If successful, the new stream's handle is returned, else 0 is returned. Use BASS_ErrorGetCode to get the error code.
// ***************************************************************************************
// Retrieves the playback length (in bytes) of a channel.
// ***************************************************************************************
[DllImport ("bass_2302.dll", EntryPoint="BASS_ChannelGetLength")] private static extern int BASS_ChannelGetLength(int BASS_Handle);
// handle The channel handle... a HCHANNEL, HMUSIC, HSTREAM. HSAMPLE handles may also be used.
// Return value If successful, then the channel's length is returned, else -1 is returned. Use BASS_ErrorGetCode to get the error code.
// ***************************************************************************************
// BASS_ChannelGetLevel%(Handle) : "BASS_ChannelGetLevel"
// ***************************************************************************************
[DllImport ("bass_2302.dll", EntryPoint="BASS_ChannelGetLevel")] private static extern int BASS_ChannelGetLevel(int BASS_Handle);
// handle The channel handle... a HCHANNEL, HMUSIC, HSTREAM. HSAMPLE handles may also be used.
// Return value If successful,If successful, the level of the left channel is returned in the low word (low 16-bits),
// and the level of the right channel is returned in the high word (high 16-bits). If the channel is mono,
// then the low word is duplicated in the high word.
// The level ranges linearly from 0 (silent) to 32768 (max). 0 will be returned when a channel is stalled
// ***************************************************************************************
// BASS_ChannelGetPosition%(Handle) : "BASS_ChannelGetPosition"
// ***************************************************************************************
[DllImport ("bass_2302.dll", EntryPoint="BASS_ChannelGetPosition")] private static extern int BASS_ChannelGetPosition(int BASS_Handle);
// handle The channel handle... a HCHANNEL, HMUSIC, HSTREAM, or HRECORD.
// Return value
// If an error occurs, -1 is returned, use BASS_ErrorGetCode to get the error code. If successful, the position is returned.
// ***************************************************************************************
// Checks if a sample, stream, or MOD music is active (playing) or stalled. Can also check if a recording is in progress.
// ***************************************************************************************
[DllImport ("bass_2302.dll", EntryPoint="BASS_ChannelIsActive")] private static extern int BASS_ChannelIsActive(int BASS_Handle);
// Parameters
// handle The channel handle... a HCHANNEL / HMUSIC / HSTREAM / HRECORD handle.
// Return value
// The return value is one of the following.
// 0 = The channel is not active, or handle is not a valid channel.
// 1 = The channel is playing (or recording).
// 2 = The channel is paused.
// 3 = Playback of the stream has been stalled due to there not being enough sample data to continue playing. The playback will automatically resume once there's sufficient data to do so.
// ***************************************************************************************
// Pauses a sample, stream, MOD music, or recording.
// ***************************************************************************************
[DllImport ("bass_2302.dll", EntryPoint="BASS_ChannelPause")] private static extern int BASS_ChannelPause(int BASS_Handle);
// ***************************************************************************************
// Starts (or resumes) playback of a sample, stream, MOD music, or recording.
// ***************************************************************************************
[DllImport ("bass_2302.dll", EntryPoint="BASS_ChannelPlay")] private static extern int BASS_ChannelPlay(int BASS_Handle, bool BASS_restart);
// Parameters
// handle The channel handle... a HCHANNEL / HMUSIC / HSTREAM / HRECORD handle.
// restart Restart playback from the beginning? If handle is a user stream, its current buffer contents are flushed. If it's a MOD music, its BPM/etc are automatically reset to their initial values.
// ***************************************************************************************
// Sets the playback position of a sample, MOD music, or stream.
// ***************************************************************************************
[DllImport ("bass_2302.dll", EntryPoint="BASS_ChannelSetPosition")] private static extern int BASS_ChannelSetPosition(int BASS_Handle, int BASS_pos);
// Parameters
// handle The channel handle... a HCHANNEL, HSTREAM or HMUSIC.
// pos The position, in bytes. With MOD musics, the MAKEMUSICPOS(order,row) macro can be used to set the position in orders and rows instead of bytes.
void Start ()
{
Debug.Log(BASS_Init(-1,44100,0,0,0));
Debug.Log ("Init error ? " + BASS_ErrorGetCode());
bass_stream = BASS_StreamCreateURL("http://185.33.22.22:8680",0,4,0,0); // url du stream à écouter.
// bass_stream = BASS_StreamCreateFile(0,"c:/Celestial.mp3",0,0,256); // fichier à écouter
Debug.Log ("StreamCreateURL error ? " + BASS_ErrorGetCode());
BASS_ChannelPlay(bass_stream ,1);
Debug.Log ("ChannelPlay error ? " + BASS_ErrorGetCode());
}
void Update()
{
Debug.Log("Bass CPU usage " + BASS_GetCPU());
}
void OnDisable()
{
BASS_Free();
}
}
JP