New to Purebasic (again) so please help me out!

Mac OSX specific forum
User avatar
HwyStar
Enthusiast
Enthusiast
Posts: 101
Joined: Mon Apr 05, 2010 7:13 pm
Location: Reno, Nevada

New to Purebasic (again) so please help me out!

Post by HwyStar »

I am trying to use Wilbert's Cocoa Sound calls and nothing is outputting. InitMovie() routines work but the Sound_Load() library does not. I made sure to put the libPBCocoa_x86 in the UserLibraries. What am I missing here? (iMac (27-inch, Late 2013))

Code: Select all

;
; This code does work:
;
; If InitMovie() = 0
;  MessageRequester("Error", "Can't initialize movie playback !", 0) 
;  End
; EndIf;;
; 
; MovieName$ = OpenFileRequester("Choose the movie to play", "", "Movie files|*.avi;*.mpg;*.mp3|All Files|*.*", 0)
; Debug(MovieName$)
; If MovieName$
;  If LoadMovie(0, MovieName$)
;  
;    OpenWindow(0, 100, 150, MovieWidth(0), MovieHeight(0), "PureBasic - Movie")
;    PlayMovie(0, WindowID(0))
;      
;    Repeat
;    Until WaitWindowEvent() = #PB_Event_CloseWindow
;  Else
;    MessageRequester("Error", "Can't load the movie...", 0)
;  EndIf
; EndIf 

Procedure Sound_Load(FileName.s)
  ProcedureReturn CocoaMessage(0,CocoaMessage(0,0,"NSSound alloc"),"initWithContentsOfFile:$",@FileName,"byReference:",#YES)
EndProcedure

Procedure Sound_Catch(*MemoryAddress, Size)
  Protected Result.i = CocoaMessage(0,0,"NSData dataWithBytes:",*MemoryAddress,"length:",Size)
  If Result : Result = CocoaMessage(0,CocoaMessage(0,0,"NSSound alloc"),"initWithData:",Result) : EndIf
  ProcedureReturn Result
EndProcedure

Procedure Sound_SetVolume(SoundObject, Volume.f)
  CocoaMessage(0,SoundObject,"setVolume:@",@Volume)
EndProcedure

Procedure Sound_Play(SoundObject, Loop = #NO)
  CocoaMessage(0,SoundObject,"setLoops:",Loop)
  CocoaMessage(0,SoundObject,"setCurrentTime:",0)
  ProcedureReturn CocoaMessage(0,SoundObject,"play")
EndProcedure

Procedure Sound_Stop(SoundObject)
  ProcedureReturn CocoaMessage(0,SoundObject,"stop")
EndProcedure

Procedure Sound_IsPlaying(SoundObject)
  ProcedureReturn CocoaMessage(0,SoundObject,"isPlaying")
EndProcedure

Procedure Sound_Release(SoundObject)
  CocoaMessage(0,SoundObject,"stop")
  CocoaMessage(0,SoundObject,"release")
EndProcedure

MySound = Sound_Load("/Users/iMac/PureBasic/test.mp3")
Sound_SetVolume(MySound, 0.8)
Sound_Play(MySound)

MessageRequester("Wait","Test")
Wolfram
Enthusiast
Enthusiast
Posts: 568
Joined: Thu May 30, 2013 4:39 pm

Re: New to Purebasic (again) so please help me out!

Post by Wolfram »

The Code Works.
Are you sure that you use the right path?

Code: Select all

MySound = Sound_Load("/Users/iMac/PureBasic/test.mp3")
Maybe it is easier to use this.

Code: Select all

MyFile.s = OpenFileRequester("Load AudioFile", "", "", 0)
MySound = Sound_Load(MyFile)
macOS Catalina 10.15.7
User avatar
HwyStar
Enthusiast
Enthusiast
Posts: 101
Joined: Mon Apr 05, 2010 7:13 pm
Location: Reno, Nevada

Re: New to Purebasic (again) so please help me out!

Post by HwyStar »

That didn't work either. I thought it may have been a UNC issue too, since I come from a Windows programming background the file paths don't always make sense to me on a Mac yet.

Code: Select all

MyFile.s = OpenFileRequester("Load AudioFile", "", "", 0)
MySound = Sound_Load(MyFile)

Sound_SetVolume(MySound, 0.8)
Sound_Play(MySound)

MessageRequester("Test","Test")
The movie code works though... XCode is installed and active. What else could be wrong with my PB setup? I will try restarting the computer just in case it is that...

* Update *
Restart did not solve it...
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: New to Purebasic (again) so please help me out!

Post by wilbert »

HwyStar wrote:InitMovie() routines work but the Sound_Load() library does not. I made sure to put the libPBCocoa_x86 in the UserLibraries. What am I missing here? (iMac (27-inch, Late 2013))
There might be a conflict.
The procedures you quoted with CocoaMessage in it are an implementation of the libPBCocoa_x86 sound procedures without the need for this library.
If you use both the library and the procedures you quoted, I believe they are defined twice.
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
HwyStar
Enthusiast
Enthusiast
Posts: 101
Joined: Mon Apr 05, 2010 7:13 pm
Location: Reno, Nevada

Re: New to Purebasic (again) so please help me out!

Post by HwyStar »

Nope. That's not it. I moved the _86x library to a different directory, recompiled and still no sound.

Since I'm new to PB; again, and forgotten most of it: the return value of "MySound" from Sound_Load, should I debug the value of it to see if it has been initialized correctly? This is the value I get: 4518793200 after recompiling it.

I am using a Mackie ONYX audio interface connected to my iMac. That shouldn't make any difference since InitMovie() works but I thought I would throw that out there.

Quicktime plays my test.mp3 file. I don't think it is a bad mp3 file. Yosemite: 10.10.5

Any other ideas? Thanks for your help guys!
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: New to Purebasic (again) so please help me out!

Post by wilbert »

Can you try with the updated code below ?
The problem might have to do with setCurrentTime.

Code: Select all

Procedure Sound_Load(FileName.s)
  ProcedureReturn CocoaMessage(0,CocoaMessage(0,0,"NSSound alloc"), "initWithContentsOfFile:$", @FileName, "byReference:", #YES)
EndProcedure

Procedure Sound_Catch(*MemoryAddress, Size)
  Protected Result.i = CocoaMessage(0, 0, "NSData dataWithBytes:", *MemoryAddress, "length:", Size)
  If Result : Result = CocoaMessage(0, CocoaMessage(0, 0, "NSSound alloc"), "initWithData:", Result) : EndIf
  ProcedureReturn Result
EndProcedure

Procedure Sound_SetVolume(SoundObject, Volume.f)
  CocoaMessage(0, SoundObject, "setVolume:@", @Volume)
EndProcedure

Procedure Sound_Play(SoundObject, Loop = 0)
  Protected currentTime.d
  CocoaMessage(0, SoundObject, "setLoops:", Loop)
  CocoaMessage(0, SoundObject, "setCurrentTime:@", @currentTime)
  ProcedureReturn CocoaMessage(0, SoundObject, "play")
EndProcedure

Procedure Sound_Stop(SoundObject)
  ProcedureReturn CocoaMessage(0, SoundObject, "stop")
EndProcedure

Procedure Sound_IsPlaying(SoundObject)
  ProcedureReturn CocoaMessage(0, SoundObject, "isPlaying")
EndProcedure

Procedure Sound_Release(SoundObject)
  CocoaMessage(0, SoundObject, "stop")
  CocoaMessage(0, SoundObject, "release")
EndProcedure
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
HwyStar
Enthusiast
Enthusiast
Posts: 101
Joined: Mon Apr 05, 2010 7:13 pm
Location: Reno, Nevada

Re: New to Purebasic (again) so please help me out!

Post by HwyStar »

Will give it a try Wilbert. Thx Man!
User avatar
HwyStar
Enthusiast
Enthusiast
Posts: 101
Joined: Mon Apr 05, 2010 7:13 pm
Location: Reno, Nevada

Re: New to Purebasic (again) so please help me out!

Post by HwyStar »

:D

That worked like a champ! My test.mp3 played the first time! Thx Man and don't forget to update the original post. Updated working code:

Code: Select all

Procedure Sound_Load(FileName.s)
  ProcedureReturn CocoaMessage(0,CocoaMessage(0,0,"NSSound alloc"), "initWithContentsOfFile:$", @FileName, "byReference:", #YES)
EndProcedure

Procedure Sound_Catch(*MemoryAddress, Size)
  Protected Result.i = CocoaMessage(0, 0, "NSData dataWithBytes:", *MemoryAddress, "length:", Size)
  If Result : Result = CocoaMessage(0, CocoaMessage(0, 0, "NSSound alloc"), "initWithData:", Result) : EndIf
  ProcedureReturn Result
EndProcedure

Procedure Sound_SetVolume(SoundObject, Volume.f)
  CocoaMessage(0, SoundObject, "setVolume:@", @Volume)
EndProcedure

Procedure Sound_Play(SoundObject, Loop = 0)
  Protected currentTime.d
  CocoaMessage(0, SoundObject, "setLoops:", Loop)
  CocoaMessage(0, SoundObject, "setCurrentTime:@", @currentTime)
  ProcedureReturn CocoaMessage(0, SoundObject, "play")
EndProcedure

Procedure Sound_Stop(SoundObject)
  ProcedureReturn CocoaMessage(0, SoundObject, "stop")
EndProcedure

Procedure Sound_IsPlaying(SoundObject)
  ProcedureReturn CocoaMessage(0, SoundObject, "isPlaying")
EndProcedure

Procedure Sound_Release(SoundObject)
  CocoaMessage(0, SoundObject, "stop")
  CocoaMessage(0, SoundObject, "release")
EndProcedure

 MyFile.s = OpenFileRequester("Load AudioFile", "", "", 0)
 MySound = Sound_Load(MyFile)
 Debug(MySound)
 
 Sound_SetVolume(MySound, 0.8)
 Sound_Play(MySound)
 
 MessageRequester("Test","Test")
Post Reply