Playing music with custom code

46 views
Skip to first unread message

Ryan McQuaid

unread,
Jun 13, 2020, 2:31:19 PM6/13/20
to MPF Users


Hi, I am using custom code to structure assets based on player selection. I was able to use self.machine.widget_player.play to get it working displaying my level background images. I'm trying to do the same for music with self.machine.sound_player.play. I am using the same variables as my working widget player.  It works the first time and the correct song plays no matter what is chosen, but after that no music is played. In the log files it looks like the game thinks the music is indeed playing even though it isn't. When I use the track_player to stop all sounds on the music track I can hear the song for a fraction of a second.  when I exit the game I see some errors that show up in the console but not the logs related to the sound player. Am I missing an integer or setting somewhere? I couldnt find a doc with a reference to using the sound_player in custom code.

    def update_music(self):
        settings
= {
           
self.music_name: {
               
'action': 'play',
               
'loops': '-1',
               
'key': 'music',
           
#    'mode_end_action': 'stop',
               
}
             
}
             
       
self.machine.sound_player.play(settings, 'asset_manager', None)

FJIMG_20200613_020338.jpg

I checked the mentioned lines but its a bit over my head to find what is missing (if anything)

        sound_settings.current_loop = 0
        sound_settings
.sound_id = sound_instance.sound_id
        sound_settings
.sound_instance_id = sound_instance.id
        sound_settings
.sample = cython.address(sound_container.sample)
        sound_settings
.volume = <Uint8>(sound_instance.volume * SDL_MIX_MAXVOLUME)  #This is line 974 as mentioned in the error. I tried specifying a volume but it didnt change anything.
        sound_settings
.volume_left = sound_settings.volume
        sound_settings
.volume_right = sound_settings.volume
        sound_settings
.loops_remaining = sound_instance.loops
        sound_settings
.sound_priority = sound_instance.priority



        # Setup the player to start playing the sound
        player
.status = player_playing
       
self._set_player_sound_settings(cython.address(player.current), sound_instance)  #This is line 1062 mentioned in the error

       
# Send sound started notification
        send_sound_started_notification
(player.number, player.current.sound_id, player.current.sound_instance_id, self.state)

       
self.log.debug("Sound %s is set to begin playback on playlist track (loops=%d)",
                       sound_instance
.name, sound_instance.loops)



Quinn Capen

unread,
Jun 13, 2020, 6:24:50 PM6/13/20
to MPF Users
Ryan,

That is an error I have never run into in all my time writing and debugging the audio engine code. I tried your custom code and could not get it to fail so I'm not sure what might be happening. What happens when you try the exact same sound and config with the sound_player (it should be calling the same code)? What about if you create a sound_player entry with your desired parameters and then emit the triggering event from your custom code? How about with a different sound? Again I'm not sure why what you are doing isn't working. I added an integration test to MC with your code and config and it also passes.

Can you paste your pip list output here and also list your platform/environment information?

Thanks,

Quinn

Ryan McQuaid

unread,
Jun 13, 2020, 11:20:02 PM6/13/20
to MPF Users
Well in that case I'm glad its not a stupid question.

The purpose of the custom code is to generate filenames for which asset to use based on combining 2 variables. So I cant use an event to play the music unless we start allowing variables as asset names (which I think should be allowed) 

Tried playing one of the failing songs from config in act 2 with same settings and custom code disabled. Worked. Then enabled custom code and it worked on act 1 as it usually does, then the config sound player worked in act 2 as well. So at least its not breaking it.   Lots more code was sent from config sound_player though. defaults i imagine. would I have to input all of these if im using custom code? 

MPF-MC BCP Server : Received "trigger?json={"name": "sounds_play", "settings": {"sonic_sb_12_m": {"action": "play", "loops": -1, "key": "music", "priority": null, "track": null, "volume": null, "pan": null, "start_at": null, "fade_in": null, "fade_out": null, "about_to_finish_time": -1.0, "max_queue_time": -1.0, "events_when_played": ["use_sound_setting"], "events_when_stopped": ["use_sound_setting"], "events_when_looping": ["use_sound_setting"], "events_when_about_to_finish": ["use_sound_setting"], "mode_end_action": "use_sound_setting"}}, "context": "zone1_act2", "calling_context": "mode_zone1_act2_started", "priority": 100}"




Windows 10 64 bit

 
Package                 Version
----------------------- ------------
asciimatics             1.11.0
certifi                 2020.4.5.1
chardet                 3.0.4
docutils                0.16
future                  0.18.2
idna                    2.9
Kivy                    1.11.1
kivy-deps.glew          0.1.12
kivy-deps.glew-dev      0.1.12
kivy-deps.gstreamer     0.1.17
kivy-deps.gstreamer-dev 0.1.17
kivy-deps.sdl2          0.1.22
kivy-deps.sdl2-dev      0.1.22
Kivy-Garden             0.1.4
mpf                     0.54.0.dev26
mpf-mc                  0.54.0.dev6
Pillow                  7.1.2
pip                     20.1
prompt-toolkit          3.0.5
psutil                  5.7.0
pyfiglet                0.8.post1
Pygments                2.6.1
pypiwin32               223
pyserial                3.4
pyserial-asyncio        0.4
pywin32                 227
requests                2.23.0
ruamel.yaml             0.15.100
setuptools              46.1.3
sortedcontainers        2.1.0
terminaltables          3.1.0
urllib3                 1.25.9
wcwidth                 0.1.9
WARNING: You are using pip version 20.1; however, version 20.1.1 is available.
You should consider upgrading via the 'c:\users\mcqua\appdata\local\programs\python\python36\python.exe -m pip install --upgrade pip' command.



Ryan McQuaid

unread,
Jun 13, 2020, 11:22:43 PM6/13/20
to MPF Users
this is the code that is sent when my custom code runs: (I changed the key so i could tell the difference) I had added a few fields thinking that maybe some of them were needed

2020-06-13 20:13:54,445 : EventManager : Event: ======'sounds_play'====== Args={'settings': {'sonic_sb_12_m': {'action': 'play', 'loops': '-1', 'key': '0', 'volume': '0.5', 'priority': '100', 'fade_in': '3s'}}, 'context': 'asset_manager', 'calling_context': None, 'priority': 0}

Quinn Capen

unread,
Jun 13, 2020, 11:45:17 PM6/13/20
to MPF Users
The only settings you need to supply are the sound name and action. All other settings will be provided with defaults in the sound asset class and don't rely on the config spec to supply defaults if they are not there.

One of the issues that has caused some strange issues with gstreamer and sdl2 is Kivy changed the name of their somewhat recently and upgrading them sometimes causes some issues on Windows installations. I was seeing some strange behaviors so I created a new virtual environment and reinstalled all the dependencies.  Here is my pip list on Windows 64 bit, Python 3.6 that has been working well for me:

Package                 Version
----------------------- ------------
asciimatics             1.11.0
certifi                 2019.11.28
chardet                 3.0.4
Cython                  0.29.10
docutils                0.16
ffpyplayer              4.3.1
future                  0.18.2
idna                    2.9
Kivy                    1.11.1
kivy-deps.glew          0.1.12
kivy-deps.glew-dev      0.1.12
kivy-deps.gstreamer     0.1.18
kivy-deps.gstreamer-dev 0.1.18
kivy-deps.sdl2          0.1.23
kivy-deps.sdl2-dev      0.1.23
Kivy-Garden             0.1.4
mpf                     0.54.0.dev44
mpf-mc                  0.54.0.dev10
pillow                  7.1.2
pip                     19.0.3
prompt-toolkit          3.0.5
psutil                  5.7.0
pyfiglet                0.8.post1
Pygments                2.3.1
pypiwin32               223
pyserial                3.4
pyserial-asyncio        0.4
pywin32                 227
requests                2.23.0
ruamel.yaml             0.15.100
setuptools              40.8.0
sortedcontainers        2.1.0
terminaltables          3.1.0
urllib3                 1.25.8
wcwidth                 0.2.3

I did bump up some of the versions of the required Kivy deps in the latest mpf-mc dev build so you might want to try upgrading to the latest (I recommend removing both Kivy and all the Kivy deps modules before upgrading). It's not likely the culprit here, but I can't rule it out. It would be easier to figure out what might be happening if I could reproduce it on my end.

Quinn

Ryan McQuaid

unread,
Jun 14, 2020, 11:11:31 PM6/14/20
to MPF Users
I have updated to the latest release (fresh install) and I still have this problem unfortunately. I'll post the pup list tomorrow but it's now the same as yours with a few things .1 versions newer here and there.

Anthony van Winkle

unread,
Jun 15, 2020, 12:39:26 PM6/15/20
to MPF Users
Ryan-

I too am using custom code to play assets based on variable/argument names, and the only difference between your code and mine (that I see) is that I use self.mc.sound_player.play() while you use self.machine.sound_player.play(). This may just be because my code is older and the sound player wasn't available on self.machine at the time, or may be that mine is running in a scriptlet instead of mode code. But perhaps it may be of use?


Ryan McQuaid

unread,
Jun 15, 2020, 1:36:25 PM6/15/20
to MPF Users
I'll try this. I had no references when I made this so I just adapted the widget player example. I have no idea what I'm doing custom code wise. What is a scriptlet? I'm using mode code because its the only option I was aware of. I haven't been able to find much documentation on creating custom code.

jabdoa

unread,
Jun 15, 2020, 4:03:26 PM6/15/20
to MPF Users
Are you using scriptlets/custom_code or mc_scriptlets/mc_custom_code? That might make a difference. Especially "self.mc" would only be available on the mc side.

Jan

Ryan McQuaid

unread,
Jul 10, 2020, 8:15:59 PM7/10/20
to MPF Users
I fixed this. It was stupid.

'loops': '-1',

needed to be:

'loops': -1,
Reply all
Reply to author
Forward
0 new messages