# Mod Player (aka Chiptune Player) Native Extension

**URL:** <https://forum.defold.com/t/mod-player-aka-chiptune-player-native-extension/55467>\
**Category:** The Defoldmine\
**Created:** [June 20, 2019, 8:26am UTC](https://forum.defold.com/t/mod-player-aka-chiptune-player-native-extension/55467 "2019-06-20T08:26:47Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![selimanac](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/selimanac/32/6397_2.png) [@selimanac](https://forum.defold.com/u/selimanac)\
**Post date:** [June 20, 2019, 8:26am UTC](https://forum.defold.com/t/mod-player-aka-chiptune-player-native-extension/55467/1 "2019-06-20T08:26:47Z")

</div>

![defoldheader](https://forum-defold.b-cdn.net/uploads/default/original/3X/b/f/bfa179919486fbbacdf3ba3c37da6eddff9c60a3.png)

Here is my [UDGJ2](https://itch.io/jam/unofficial-defold-game-jam-2) entry; ModPlayer and I want socks now 🙂 It is not of course, but I wanted it to be somehow ready for the UDGJ. Maybe someone wants to use it for their Jam game.  
As a dinosaur developer, I miss my C64 and Amiga days. When developing this simple extension I remembered those days a lot.

[Mod Player](https://www.defold.com/community/projects/147260/) (aka chiptune player) can load and play good old [.XM](https://en.wikipedia.org/wiki/XM_(file_format)) and [.MOD](https://en.wikipedia.org/wiki/MOD_(file_format)) file formats.  
There are some hitchups, but extension supports MacOS, Windows, Linux, Android, iOS and HTML5. Unfortunately, [HTML5 bundle](https://github.com/selimanac/defold-modplayer#html5-bundle) is little [tricky](https://forum.defold.com/t/reading-files-from-res-common-folder-with-emscripten-4194/55056). There is a little [py script](https://github.com/selimanac/modplayer-html5-example/blob/master/mod_file_parser.py) which simplify things for now. Please read the details on [Github](https://github.com/selimanac/defold-modplayer) page.

[![selimanac/defold-modplayer - GitHub](https://gh-card.dev/repos/selimanac/defold-modplayer.svg)](https://github.com/selimanac/defold-modplayer)

**Simple Examples:** [GitHub - selimanac/defold-modplayer-examples: Simple module player examples for Defold Engine · GitHub](https://github.com/selimanac/defold-modplayer-examples)  
**Nanowar Example:** [GitHub - selimanac/nanowar-modplayer: Nanowar Game - Defold Mod Player Extension example · GitHub](https://github.com/selimanac/nanowar-modplayer) (All credits goes to @benjames171 )

Most important issue for me is that I couldn’t find a way to retrieve build path when developing on Defold Editor. It returns something like ../Defold.app/.. and it is not possible to read files from that location. As a workaround I added a `player.build_path` function for manuel setting. But this is just for working on the editor only. If there is a better way of doing this please let me know.

I couldn’t test the musics on Linux because I couldn’t manage to configure the sound card on my VMs successfully. If someone can test it I’ll be glad.

Other notes and know issues:

- Loading and parsing is blocker. It will block the main thread (UI thread). Since the mod files are small it is better to load them when bootstraping or preloading. It may cause a pause on UI.
- Loading and parsing .XM files much more faster then .MOD files. Use XM if possible. (Tested with same tracker file as .mod and .xm)
- Not %100 compatible with every MOD or XM file.
- I couldn’t find a way to retrieve build path when developing on Defold Editor. You have to provide a full path to `player.build_path("<FULL_PATH>/res/common/assets/")` function for **working on Defold Editor only**. It doesn’t required when bundling.
- Different platform bundles didn’t tested very well.
  - MacOS: Long run.
  - iOS: Long run.
  - Windows: Short run. Tested on [Wine](https://www.winehq.org/)
  - Android: Short run.
  - Linux: I couldn’t manage to have sound on my VMs. But app is successfully load the files and run on Debian and Ubuntu

- Hashtable is limited to 10 elements. I think it is more than enough. It is a bad practice to load or play more than two music files at the same time.
- Currently, it is not possible to Build HTML5 on the Defold Editor with mod music. You can build it for testing, but can’t load the the musics.

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [June 20, 2019, 8:39am UTC](https://forum.defold.com/t/mod-player-aka-chiptune-player-native-extension/55467/2 "2019-06-20T08:39:35Z")

</div>

This is great! Thank you for making this contribution!

---

<div class="post-metadata">

**Author:** ![NeZvers](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/nezvers/32/16030_2.png) [@NeZvers](https://forum.defold.com/u/NeZvers)\
**Post date:** [June 22, 2019, 12:02am UTC](https://forum.defold.com/t/mod-player-aka-chiptune-player-native-extension/55467/3 "2019-06-22T00:02:10Z")

</div>

Example doesn’t run for me.  
ERROR:MODPLAYER: build\_path cannot be empty.  
What do you mean by" \<FULL\_PATH\> "?

---

<div class="post-metadata">

**Author:** ![selimanac](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/selimanac/32/6397_2.png) [@selimanac](https://forum.defold.com/u/selimanac)\
**Post date:** [June 22, 2019, 8:21am UTC](https://forum.defold.com/t/mod-player-aka-chiptune-player-native-extension/55467/4 "2019-06-22T08:21:39Z")

</div>

> [@NeZvers](#):
>
> ERROR:MODPLAYER: build\_path cannot be empty.

As the error said, you have to provide a [project path](https://github.com/selimanac/defold-modplayer#playerbuild_pathfull_pathstring) when building on Defold Editor. You are passing a empty string.

> [@NeZvers](#):
>
> What do you mean by" \<FULL\_PATH\> "?

Just updated the [docs](https://github.com/selimanac/defold-modplayer#playerbuild_pathfull_pathstring) with more info. Also updated the error message

---

<div class="post-metadata">

**Author:** ![NeZvers](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/nezvers/32/16030_2.png) [@NeZvers](https://forum.defold.com/u/NeZvers)\
**Post date:** [June 22, 2019, 10:23pm UTC](https://forum.defold.com/t/mod-player-aka-chiptune-player-native-extension/55467/5 "2019-06-22T22:23:57Z")

</div>

Ok, so you mean os system folder location, your example of “\<FULL\_PATH\>/res/common/assets/” was throwing off because of wrong slash symbols.  
But I get error :

> ```
> Compilation failed: invalid escape sequence near '"D
> 
> ```

for your example project on line:

> ```
> local build_path = "D:\Users\agnis\Documents\Defold\SOURCE\defold-modplayer-master\res\common\assets"
> 
> ```

P.s. Bundled EXE runs nicely.

---

<div class="post-metadata">

**Author:** ![selimanac](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/selimanac/32/6397_2.png) [@selimanac](https://forum.defold.com/u/selimanac)\
**Post date:** [June 23, 2019, 7:52am UTC](https://forum.defold.com/t/mod-player-aka-chiptune-player-native-extension/55467/6 "2019-06-23T07:52:38Z")

</div>

> [@NeZvers](#):
>
> Ok, so you mean os system folder location, your example of “\<FULL\_PATH\>/res/common/assets/” was throwing of because of wrong slash symbols.

Actually slash symbols are correct. I made a mistake on doc by using `\`. My bad, sorry about that. (Just updated the [doc](https://github.com/selimanac/defold-modplayer#playerbuild_pathfull_pathstring))

This should work. Don’t forget to add trailing `/`

```lua
local build_path = "D:/Users/agnis/Documents/Defold/SOURCE/defold-modplayer-master/res/common/assets/"

```

Unfortunately I don’t have a Windows box and I can’t test this with Defold Editor. But this time, I tested this path with Wine and it works. Please let me know it is fail or not.

> [@NeZvers](#):
>
> P.s. Bundled EXE runs nicely.

👻 Nice to hear that. Thank you for testing 🙂

---

<div class="post-metadata">

**Author:** ![NeZvers](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/nezvers/32/16030_2.png) [@NeZvers](https://forum.defold.com/u/NeZvers)\
**Post date:** [June 23, 2019, 8:00am UTC](https://forum.defold.com/t/mod-player-aka-chiptune-player-native-extension/55467/7 "2019-06-23T08:00:56Z")

</div>

I don’t get what’s wrong.  
After adding trailing slash everything after changes color like string value without ending quotes (").

 ![Capture](https://forum-defold.b-cdn.net/uploads/default/original/3X/9/8/9847d7e0c471b7e5e6a1fa6fb1509e593c30c754.png)

---

<div class="post-metadata">

**Author:** ![selimanac](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/selimanac/32/6397_2.png) [@selimanac](https://forum.defold.com/u/selimanac)\
**Post date:** [June 23, 2019, 8:05am UTC](https://forum.defold.com/t/mod-player-aka-chiptune-player-native-extension/55467/8 "2019-06-23T08:05:22Z")

</div>

This is wrong `\`. Use this `/`

Just copy-paste this please:

```lua
local build_path = "D:/Users/agnis/Documents/Defold/SOURCE/defold-modplayer-master/res/common/assets/"

```

---

<div class="post-metadata">

**Author:** ![NeZvers](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/nezvers/32/16030_2.png) [@NeZvers](https://forum.defold.com/u/NeZvers)\
**Post date:** [June 23, 2019, 8:10am UTC](https://forum.defold.com/t/mod-player-aka-chiptune-player-native-extension/55467/9 "2019-06-23T08:10:08Z")

</div>

> [@selimanac](#):
>
> D:/Users/agnis/Documents/Defold/SOURCE/defold-modplayer-master/res/common/assets/

YES! That is working. I tried backward slash before but didn’t think about trailing one.  
I say backward because on window directories have "\ ", I don’t remember if Mac or Linux have an opposite.

---

<div class="post-metadata">

**Author:** ![selimanac](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/selimanac/32/6397_2.png) [@selimanac](https://forum.defold.com/u/selimanac)\
**Post date:** [June 23, 2019, 8:16am UTC](https://forum.defold.com/t/mod-player-aka-chiptune-player-native-extension/55467/10 "2019-06-23T08:16:08Z")

</div>

> [@NeZvers](#):
>
> YES! That is working. I tried backward slash before but didn’t think about trailing one.

Great!  
I think I should add a control for trailing one. This will be a common mistake.

> [@NeZvers](#):
>
> I say backward because on window directories have “”, I don’t remember if Mac or Linux have an opposite.

Actually every platform uses `/` inted of `\` It is a ancient DOS problem… [https://stackoverflow.com/a/38428899/10580287](https://stackoverflow.com/a/38428899/10580287)

---

<div class="post-metadata">

**Author:** ![NeZvers](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/nezvers/32/16030_2.png) [@NeZvers](https://forum.defold.com/u/NeZvers)\
**Post date:** [June 23, 2019, 8:18am UTC](https://forum.defold.com/t/mod-player-aka-chiptune-player-native-extension/55467/11 "2019-06-23T08:18:17Z")

</div>

Thank you for extension! I’m really excited to try it out, since that’s closest thing I can get to NES-like music.

---

<div class="post-metadata">

**Author:** ![selimanac](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/selimanac/32/6397_2.png) [@selimanac](https://forum.defold.com/u/selimanac)\
**Post date:** [June 23, 2019, 8:49am UTC](https://forum.defold.com/t/mod-player-aka-chiptune-player-native-extension/55467/12 "2019-06-23T08:49:46Z")

</div>

It is not perfect but better than nothing. I hope someday we can get rid of this [build\_path](https://github.com/defold/editor2-issues/issues/2606) problem.

---

<div class="post-metadata">

**Author:** ![NeZvers](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/nezvers/32/16030_2.png) [@NeZvers](https://forum.defold.com/u/NeZvers)\
**Post date:** [June 23, 2019, 9:31am UTC](https://forum.defold.com/t/mod-player-aka-chiptune-player-native-extension/55467/13 "2019-06-23T09:31:47Z")

</div>

Well, the biggest problem with it is to convey the right information to users other than that it’s an actually neat workaround.

Sorry, for additional questioning:  
I noticed that it does auto-loop for songs so I made test pattern in Milkytracker with 2 snare hits to test and the mod player kept looping it (for the first time returning to start it makes tiny glitchy sounds and I’ve heard those also when it looped included sound files).  
Is this mod player usable for game sound effects (single play over music or taking over channel like NES music)?

---

<div class="post-metadata">

**Author:** ![selimanac](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/selimanac/32/6397_2.png) [@selimanac](https://forum.defold.com/u/selimanac)\
**Post date:** [June 23, 2019, 10:11am UTC](https://forum.defold.com/t/mod-player-aka-chiptune-player-native-extension/55467/14 "2019-06-23T10:11:58Z")

</div>

> [@NeZvers](#):
>
> I noticed that it does auto-loop for songs so I made test pattern in Milkytracker with 2 snare hits to test and the mod player kept looping it (for the first time returning to start it makes tiny glitchy sounds and I’ve heard those also when it looped included sound files).

I’m aware of that glitch. But honestly I’m not sure why it is happening. It happens on some patterns with some samples(only when starting), but I didn’t investigate it yet. (It shouldn’t happend with .mod files, only with .xm) Somehow it is related to [my patch](https://github.com/selimanac/defold-modplayer/issues/2). I’ll take a look at this after the [UDGJ2](https://forum.defold.com/t/unofficial-defold-game-jam-2/52823/40)  
It is better not to trust the loop for pattern continuity. It is not suitable for this kind of job.

> [@NeZvers](#):
>
> Is this mod player usable for game sound effects (single play over music or taking over channel like NES music)?

Maybe 🙂 Interesting idea, I never thought about this. It is quite performant but I’m not sure if it is usable for this kind of a job or not. I’ll definitely try this when I got time.

You can set loop by using; `player.music_loop(music, 1)` ← single play

---

<div class="post-metadata">

**Author:** ![NeZvers](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/nezvers/32/16030_2.png) [@NeZvers](https://forum.defold.com/u/NeZvers)\
**Post date:** [June 23, 2019, 10:17am UTC](https://forum.defold.com/t/mod-player-aka-chiptune-player-native-extension/55467/15 "2019-06-23T10:17:33Z")

</div>

Wow, I don’t know why I missed loop function!  
Thank you!

---

<div class="post-metadata">

**Author:** ![COCO](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/coco/32/38692_2.png) [@COCO](https://forum.defold.com/u/COCO)\
**Post date:** [January 3, 2021, 8:46am UTC](https://forum.defold.com/t/mod-player-aka-chiptune-player-native-extension/55467/16 "2021-01-03T08:46:46Z")

</div>

Is there any tools we can use to produce mx music?

---

<div class="post-metadata">

**Author:** ![selimanac](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/selimanac/32/6397_2.png) [@selimanac](https://forum.defold.com/u/selimanac)\
**Post date:** [January 3, 2021, 10:12am UTC](https://forum.defold.com/t/mod-player-aka-chiptune-player-native-extension/55467/17 "2021-01-03T10:12:41Z")

</div>

> [@COCO](#):
>
> mx music?

What is the “mx music”?  
If you are looking for a tracker. I would suggest [Milky Tracker](https://milkytracker.org/)

---

<div class="post-metadata">

**Author:** ![COCO](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/coco/32/38692_2.png) [@COCO](https://forum.defold.com/u/COCO)\
**Post date:** [January 3, 2021, 10:48am UTC](https://forum.defold.com/t/mod-player-aka-chiptune-player-native-extension/55467/18 "2021-01-03T10:48:13Z")

</div>

Thanks.
