Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3671714
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:31:15+00:00 2026-05-19T02:31:15+00:00

I posted a question on gamedev about how to play nsf files (NES console

  • 0

I posted a question on gamedev about how to play nsf files (NES console music) in FMOD. It didn’t get any results, but since then I made some progress. I decided that the easiest method was just to compile an existing player into a dll and then call it from C# to populate my buffer. The problem now is getting it to sound right, and making sure all my paremeters are correct.

Here are the facts so far:

  1. The nsf dll is dealing with shorts, so the data is PCM16.
  2. The sample nsf I’m using has a playback rate of 60 Hz.
  3. Just for playing around now, I’m using a frequency of 48000.
  4. Based on 2 and 3, the dll calculates a necessary buffer size of 48000 / 60hz = 800. This means it will render 800 shorts worth of buffer for every simulated NES frame.

I’ve so far got my C# code to play the nsf, at the correct pitch and tempo, but it’s very grainy / fuzzy, which I’m attributing to the fact that the FMOD read callback is giving a data length of 1600, whereas I should be expecting 800. I’ve tried playing around with all the numbers and it either crashes, or the music changes pitch, tempo, or both.

Here’s some of my C# code:

uint channels = 1, frequency = 48000;

FMOD.MODE mode = (FMOD.MODE.DEFAULT | FMOD.MODE.OPENUSER | FMOD.MODE.LOOP_NORMAL);

FMOD.Sound sound = new FMOD.Sound();
FMOD.CREATESOUNDEXINFO ex = new FMOD.CREATESOUNDEXINFO();
ex.cbsize = Marshal.SizeOf(ex);
ex.fileoffset = 0;
ex.format = FMOD.SOUND_FORMAT.PCM16;

// does this even matter? It doesn't change my results as long as it's long enough for one update
ex.length = frequency; 

ex.numchannels = (int)channels;
ex.defaultfrequency = (int)frequency;
ex.pcmreadcallback = pcmreadcallback;
ex.dlsname = null;

// eventually I will calculate this with frequency / nsf hz, but I'm just testing for now
ex.decodebuffersize = 800;

// from the dll
load_nsf_file("file.nsf", 8, (int)frequency); // 8 is the track number to play

var result = system.createSound(
        (string)null,
        (mode | FMOD.MODE.CREATESTREAM),
        ref ex,
        ref sound);

channel = new FMOD.Channel();
result = system.playSound(FMOD.CHANNELINDEX.FREE, sound, false, ref channel);

private FMOD.RESULT PCMREADCALLBACK(IntPtr soundraw, IntPtr data, uint datalen)
{
    // from the dll
    process_buffer(data, (int)800); // if I use datalen, it usually crashes (I can't get datalen to = 800 safely)

    return FMOD.RESULT.OK;
}

So here are some of my questions:

  1. What is the relationship between exinfo.decodebuffersize, frequency, and the datalen parameter of the read callback? With this code sample, it’s coming in as 3200. I don’t know where that factor of 4 between it and the decodebuffersize comes from.
  2. Is datalen in the callback referring to number of bytes, or shorts? The process_buffer function takes a short array and its length. I would expect fmod is talking about shorts as well because I told it PCM16.
  3. Maybe my playback quality is bad for some totally different reason. If so I have no idea where to begin solving that. Any ideas there?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T02:31:16+00:00Added an answer on May 19, 2026 at 2:31 am
    1. The FMOD_CREATESOUNDEXINFO ‘decodebuffersize’ member is in PCM samples and controls how much data will be requested in the ‘pcmreadcallback’ function. So since you set it to 800 and PCM16, ‘datalen’ will be 1600 as the value is in bytes. You mention at the ‘datalen’ value is 1600 (as expected) but later you say it’s 3200, perhaps I have confused your meaning. Finally the ‘frequency’ is just telling FMOD how quickly to play your data, this should match what ever your ‘process_buffer’ function produces.

    2. ‘datalen’ is in bytes.

    3. A good way to test this simply is to generate say 5 seconds of audio then load it into FMOD as a sample. This avoids potential complications with read callbacks.

    BTW: ‘ex.length’ tells Sound::getLength how big your file actually is, and if you are using FMOD_CREATESAMPLE this is how much memory is allocated.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I posted a question the other day and didn't get much response. I have
I posted this question on MWS forum but, didn't get an answer. So, I'm
Yesterday I've posted question about SQLite performance issues ( Terrible performance in Zend Db
I posted a question about keys yesterday, and got very helpful responses. I've been
I posted a question last night about Java Reflection and am discovering compiler warnings
I posted this question on Reddit Programming and did not get a single response.
Following a question posted here about how I can increase the speed on one
I posted a question earlier about stopping all sounds in a swf. Now, to
I posted a question yesterday, which I intend to get back to today however
I posted another question earlier about getting the arguments from a context menu App.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.