miniaudio sound lib mp3

Share your advanced PureBasic knowledge/code with the community.
User avatar
idle
Always Here
Always Here
Posts: 5897
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

miniaudio sound lib mp3

Post by idle »

A wrapper to miniaudio sound with mp3
windows x64

https://www.dropbox.com/scl/fi/j7a1ftav ... oi1wr&dl=0

Code: Select all

ImportC "miniaudiosound.lib" 
  ma_Sound_Init()
  ma_Sound_UnInit(engine)
  ma_Sound_LastError()
  ma_Sound_Free(sound) 
  ma_Sound_Load(engine,file.s,delay.f=0.0,decay.f=0.0)
  ma_Sound_Play(sound) 
  ma_Sound_Stop(sound)
  ma_Sound_Set_Volume(Sound,volume.f)
  ma_Sound_Get_Volume(Sound)
  ma_Sound_Set_Pan(Sound,pan.f)
  ma_Sound_Set_Pan_Mode(Sound,panMode.l)
  ma_Sound_Get_Pan_Mode(sound)
  ma_sound_set_Pitch(Sound,pitch.f)
  ma_Sound_Get_Pitch(Sound)
  ma_Sound_Set_Spatialization_Enabled(Sound,enabled.l)
  ma_Sound_Is_Spatialization_Enabled(Sound)
  ma_Sound_Set_Pinned_Listener_Index(Sound,listenerIndex.l)
  ma_Sound_Get_Pinned_Listener_Index(Sound)
  ma_Sound_Get_Listener_Index(Sound)
  ma_Sound_Get_Direction_To_Listener(Sound,*vec3.Vector3)
  ma_Sound_Set_Position(Sound,*vec3.Vector3) 
  ma_Sound_Get_Position(Sound,*vec3.Vector3)
  ma_Sound_Set_Direction(Sound,*vec3.Vector3)
  ma_Sound_Get_Direction(Sound,*vec3.Vector3)
  ma_Sound_Set_Velocity(Sound,*vec3.Vector3) 
  ma_Sound_Get_Velocity(Sound,*vec3.Vector3)
  ma_Sound_Set_Attenuation_Model(Sound,attenuationModel.l)
  ma_Sound_Get_Attenuation_Model(Sound)
  ma_Sound_Set_Positioning(Sound,ma_positioning.l)
  ma_Sound_Get_Positioning(Sound) 
  ma_Sound_Set_Rolloff(Sound,rolloff.f)
  ma_Sound_Get_Rolloff(Sound)
  ma_Sound_Set_Min_Gain(Sound,minGain.f)
  ma_Sound_Get_Min_Gain(Sound)
  ma_Sound_Set_Max_Gain(Sound,maxGain.f)
  ma_Sound_Get_Max_Gain(Sound)
  ma_Sound_Set_Min_Distance(Sound,minDistance.f)
  ma_Sound_Get_Min_Distance(Sound)
  ma_Sound_Set_Max_Distance(Sound,maxDistance.f)
  ma_Sound_Get_Max_Distance(Sound)
  ma_Sound_Set_Cone(Sound,*vec.Vector3)  ;float innerAngleInRadians, float outerAngleInRadians, float outerGain
  ma_Sound_Get_Cone(Sound,*vec.Vector3)  ;float* pInnerAngleInRadians, float* pOuterAngleInRadians, float* pOuterGain
  ma_Sound_Set_Doppler_Factor(Sound,dopplerFactor.f)
  ma_Sound_Get_Doppler_Factor(Sound)
  ma_Sound_Set_Directional_Attenuation_Factor(Sound,directionalAttenuationFactor.f)
  ma_Sound_Get_Directional_Attenuation_Factor(Sound)
  ma_Sound_Set_Fade_In_Pcm_Frames(Sound,volumeBeg.f,volumeEnd.f,fadeLengthInFrames.q)
  ma_Sound_Set_Fade_In_Milliseconds(sound,volumeBegin.f,volumeEnd.f,fadeLengthInMilliseconds.q)
  ma_Sound_Get_Current_Fade_Volume(Sound)
  ma_Sound_Set_Start_Time_In_Pcm_Frames(Sound,absoluteGlobalTimeInFrames.q)
  ma_Sound_Set_Start_Time_In_Milliseconds(Sound,absoluteGlobalTimeInMilliseconds.q)
  ma_Sound_Set_Stop_Time_In_Pcm_Frames(Sound,absoluteGlobalTimeInFrames.q)
  ma_Sound_Set_Stop_Time_In_Milliseconds(Sound,absoluteGlobalTimeInMilliseconds.q)
  ma_Sound_Is_Playing(Sound)
  ma_Sound_Get_Time_In_Pcm_Frames(Sound)
  ma_Sound_Set_Looping(Sound,isLooping.l)
  ma_Sound_Is_Looping(Sound)
  ma_Sound_At_End(Sound)
  ma_Sound_Seek_To_Pcm_Frame(Sound,frameindex.q)
  ma_Sound_Get_Data_Format(Sound,*Format.long,*Channels.long,*SampleRate.long,*ChannelMap.long,channelMapCap.i)
EndImport   

CompilerIf #PB_Compiler_IsMainFile 
  
  Global engine,sound,file.s,pan.f=-1.0    
  OpenConsole()
  
  file = "motat.mp3"
  
  If file <> "" 
    
    engine = ma_Sound_Init() 
    If engine 
      sound = ma_Sound_Load(engine,file,0.6,0.25) 
      ma_Sound_Set_Spatialization_Enabled(Sound,1)
      If sound 
        ma_Sound_Play(sound)
        Repeat 
          ma_Sound_Set_Pan(Sound,pan)
          ma_sound_Set_Pitch(Sound,1.25)
          pan + 0.1 
          If pan > 1.0 
            pan = 0 
          EndIf   
          Delay(250)
        Until ma_Sound_At_End(sound)  
         
        PrintN("Press Enter to quit...");
        Input()
        ma_Sound_Free(sound) 
      Else 
        PrintN("Press Enter to quit...");
        Input()
      EndIf 
      ma_Sound_UnInit(engine)
    EndIf  
    
  EndIf 
  
CompilerEndIf 
Quin
Addict
Addict
Posts: 1133
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: miniaudio sound lib mp3

Post by Quin »

Very cool, thanks for sharing! Just tested, and it works on Windows 10 x64.
User avatar
idle
Always Here
Always Here
Posts: 5897
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: miniaudio sound lib mp3

Post by idle »

Quin wrote: Sat Mar 16, 2024 11:01 pm Very cool, thanks for sharing! Just tested, and it works on Windows 10 x64.
Thanks it was an experiment with inlineC but unfortunately I still haven't got it working statically and it requires my tools and clang to compile it.

Code: Select all

ImportC "miniaudiox64.lib" : EndImport  ;import library to dll 

Structure sound_ma 
  *sound 
  *delaynode
EndStructure    

ProcedureCDLL ma_Sound_Init() 
  
  Protected *engine
  Protected sz 
  !v_sz = sizeof(ma_engine); 
  *engine= AllocateMemory(sz);
  !ma_result result;
  !result = ma_engine_init(NULL, p_engine);
  !if (result == MA_SUCCESS) {
  ProcedureReturn *engine
  !}  
  
EndProcedure 

ProcedureCDLL ma_Sound_UnInit(*engine)
  
  If *engine
    !ma_engine_uninit(p_engine);
    *engine = 0 
  EndIf
  
EndProcedure  

ProcedureCDLL maSound_Load(*engine,file.s,delay.f=0.0,decay.f=0.0)
  
  If file <> "" 
    
    Protected *snd.sound_ma = AllocateMemory(SizeOf(sound_ma))   
    Protected sz 
    !v_sz = sizeof(ma_sound);
    Protected *sound = AllocateMemory(sz) 
    
    !ma_result result;
    
    ! ma_uint32 channels;
    ! ma_uint32 sampleRate;
    
    ! channels   = ma_engine_get_channels(p_engine);
    ! sampleRate = ma_engine_get_sample_rate(p_engine);
    
    If delay <> 0.0 
      
      ! ma_delay_node* g_delayNode = malloc(sizeof(ma_delay_node));   /* The echo effect is achieved using a delay node. */
      ! p_snd->f_delaynode = g_delayNode; 
      
      ! ma_delay_node_config delayNodeConfig;  
      ! delayNodeConfig = ma_delay_node_config_init(channels, sampleRate, (ma_uint32)(sampleRate * v_delay), v_decay);
      ! result = ma_delay_node_init(ma_engine_get_node_graph(p_engine), &delayNodeConfig, NULL, g_delayNode);
      ! if (result != MA_SUCCESS) {
      ;err = "Failed to initialize delay node."
      Goto error
      ! }
      ! 
      !  ma_node_attach_output_bus(g_delayNode, 0, ma_engine_get_endpoint(p_engine), 0);
      
      !  result = ma_sound_init_from_file_w(p_engine,v_file, 0, NULL, NULL, p_sound);
      !  if (result != MA_SUCCESS) {
      ;err = "Failed to initialize sound"
      Goto error
      !  }
    EndIf 
    
    If delay <> 0.0 
      ;   /* Connect the output of the sound To the input of the effect. */
      ! ma_node_attach_output_bus(p_sound, 0, g_delayNode, 0);
    Else 
      !  result = ma_sound_init_from_file_w(p_engine,v_file, 0, NULL, NULL, p_sound);
      !  if (result != MA_SUCCESS) {
      ;err = "Failed to initialize sound"
      Goto error
      !  }
    EndIf 
    
    !p_snd->f_sound = p_sound;
    
    ProcedureReturn *snd 
    
    error: 
    
    maSound_Free(*snd)
    
  EndIf 
  
EndProcedure 

;....



User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: miniaudio sound lib mp3

Post by Kwai chang caine »

Works nice here :wink:
Thanks for sharing 8)
ImageThe happiness is a road...
Not a destination
dige
Addict
Addict
Posts: 1405
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Re: miniaudio sound lib mp3

Post by dige »

Wow nice! Finally I can play MP3s without having to use the old MCI.
Now I'm wondering what else this miniAudioLib can do? Can it also display a frequency?
"Daddy, I'll run faster, then it is not so far..."
User avatar
idle
Always Here
Always Here
Posts: 5897
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: miniaudio sound lib mp3

Post by idle »

dige wrote: Tue May 14, 2024 1:15 pm Wow nice! Finally I can play MP3s without having to use the old MCI.
Now I'm wondering what else this miniAudioLib can do? Can it also display a frequency?
not directly but yes it could be done, the main issue with the lib is that it returns a lot of structures by value and it needs wrapping to return pointers.
User avatar
tft
User
User
Posts: 99
Joined: Mon Dec 29, 2008 9:34 am

Re: miniaudio sound lib mp3

Post by tft »

"A great project and thank you for sharing it. I am looking for the option to select the output device. Do you have any possibilities for that?"
TFT seid 1989
Aktuelles Projekte : Driving School Evergarden
YouTube : Pure Basic to go
FaceBook : Temuçin SourceMagic Games
DISCORD : SourceMagic
W10 , i9 9900K ,32 GB Ram , GTX Titan , 3 Monitore FHD
ARDUINO Freak :-)
User avatar
idle
Always Here
Always Here
Posts: 5897
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: miniaudio sound lib mp3

Post by idle »

I will have a dig, I thought I had a device selection example but none of that is exposed in this lib though I could probably add to it at some point.
User avatar
tft
User
User
Posts: 99
Joined: Mon Dec 29, 2008 9:34 am

Re: miniaudio sound lib mp3

Post by tft »

What do you need to deal with it? A sandwich? Cuddles? How can I motivate this?
TFT seid 1989
Aktuelles Projekte : Driving School Evergarden
YouTube : Pure Basic to go
FaceBook : Temuçin SourceMagic Games
DISCORD : SourceMagic
W10 , i9 9900K ,32 GB Ram , GTX Titan , 3 Monitore FHD
ARDUINO Freak :-)
User avatar
idle
Always Here
Always Here
Posts: 5897
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: miniaudio sound lib mp3

Post by idle »

I Didn't get time to look at it today, tomorrow is another day though
User avatar
tft
User
User
Posts: 99
Joined: Mon Dec 29, 2008 9:34 am

Re: miniaudio sound lib mp3

Post by tft »

:D
TFT seid 1989
Aktuelles Projekte : Driving School Evergarden
YouTube : Pure Basic to go
FaceBook : Temuçin SourceMagic Games
DISCORD : SourceMagic
W10 , i9 9900K ,32 GB Ram , GTX Titan , 3 Monitore FHD
ARDUINO Freak :-)
User avatar
idle
Always Here
Always Here
Posts: 5897
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: miniaudio sound lib mp3

Post by idle »

Took me a while to clear my head and remember this enums the devices using the native PB miniaudio.lib, it might work with the mp3sound lib too. how to set the device configurations though might just be a case of setting the *deviceConfig\playback\pDeviceID

https://www.purebasic.fr/english/viewto ... 23#p616623
Post Reply