Author Topic: 4k softsynth  (Read 6101 times)

0 Members and 1 Guest are viewing this topic.

Offline Nezbie

  • ZX 81
  • *
  • Posts: 2
  • Karma: 1
    • View Profile
4k softsynth
« on: March 03, 2008 »
I'm interested in creating a softsynth using Gm.dls wave data, for use in 4k intro development. However, I've never done any audio programming, so I'm not sure what approach I should use, and what type of filters are common, or even how to playback the data. My approach is similar to this:
-Samples (will contain the wave data from Gm.dls. In the stored format, only the Gm.dls offset will be stored, including looping data)
-Instrument (pointer to a sample, plus parameters for filter effects)
-Patterns (series of notes)
-Music (sequence of patterns in multiple simultaneous channels)
Then somehow have an update function that is called each frame, that calculates a small audio buffer sequence and sends it to the sound card.

So, the situation where I am at now, is that I've successfully created a program that can read and parse Gm.dls into appropriate wave data. However, I know that the wave data includes wave looping/loop length information, and I'm not sure how to extract this, and it is obviously critical, or else the samples often seems cut short. Additionally, is there a way using directsound to automatically take into account this looping info, and add parameters such as ramp and volume? Or do I automatically have to create the buffer myself? Additionally, I'm not even sure how to manipulate the sample audio data, and how much directsound can do for you. Any pointers would be welcome. In the meantime, here is my code for extracting gm.dls audio data (without loop data, which I don't know how to extract). It is not very optimized, as that will be done last. The program extracts the samples, displays the sample names and offsets, then you can type in a number to hear the corresponding wave data, and "q" to exit. It has the negative drawback of loading the absolute address for gm.dls, but that would take like 3sec to fix.


Code: [Select]
#include <fstream>
#include <iostream>
#include <string>
#include <map>
#include <windows.h>

using namespace std;

struct gmSample
{
short index;
DWORD offset;
long loopStart;
long loopLength;
};

int main()
{
ifstream input("C:\\Windows\\System32\\drivers\\Gm.dls", ios::in | ios::binary);
if(input.fail()) {
cerr << "Could not open Gm.dls";
return 1;
}
input.seekg(0, ios::end);
int _length = input.tellg();
input.seekg(0, ios::beg);
unsigned char* _data = new unsigned char[_length];
input.read(reinterpret_cast<char *> (_data), _length);
input.close();

typedef map<char*, gmSample> SampleMap;
typedef map<int, char*> SampleInd;
SampleMap samples;
SampleInd indicies;
int index = -1;
unsigned char* dt = _data;
while(dt < _data + _length - 64) {
if(*(DWORD *)dt == 'TSIL') {
*(DWORD *)dt = 'FFIR';
dt+=8;
if(*(DWORD *)dt == 'evaw') {
*(DWORD *)dt = 'EVAW';
gmSample sample;
sample.offset = DWORD(dt-8) - DWORD(_data);
sample.index = index++;
char* name = new char[8];
memset(name, 0, 8*sizeof(char));
memcpy(name, dt-16, 7*sizeof(char));
samples.insert(SampleMap::value_type(name, sample));
indicies.insert(SampleInd::value_type(index, name));
}
}
dt++;
}
for(SampleMap::iterator itor = samples.begin(); itor!=samples.end(); itor++) {
cout << itor->first << '\t' << itor->second.offset << endl;
}
string io("1");
while((io.compare("quit") != 0) && (io.compare("q") != 0)) {
int num = atoi(io.c_str());
gmSample smpl = samples.find(indicies.find(num)->second)->second;
PlaySoundA((const char*)(_data+smpl.offset), NULL, SND_MEMORY);
cin >> io;
}
samples.clear();
delete [] _data;
return 0;
}


PS: I'm also looking for a musician who would be willing to compose a tune for use in a PSP demo to be released soon (Any format is fine).
« Last Edit: March 03, 2008 by Nezbie »

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: 4k softsynth
« Reply #1 on: March 03, 2008 »
For information about the file-format you could just check the Specs.

Have a look at the Synth Secrets articles.
You can get a good understanding of creating musically useful sounds from very limited resources.
« Last Edit: March 03, 2008 by hellfire »
Challenge Trophies Won:

Offline taj

  • Bytes hurt
  • DBF Aficionado
  • ******
  • Posts: 4810
  • Karma: 189
  • Scene there, done that.
    • View Profile
Re: 4k softsynth
« Reply #2 on: March 03, 2008 »
Well if you've never done any audio programming before as your post says, then total respect for what you achieved. I'm afraid I don't know the answer to your question,  but here is some karma for a nice, with code, first post.
Challenge Trophies Won:

Offline Rbz

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 2757
  • Karma: 493
    • View Profile
    • https://www.rbraz.com/
Re: 4k softsynth
« Reply #3 on: March 04, 2008 »
@Nezbie: First, welcome to this forum.

About looping points, first you will need to learn .wav file format, look at sonicspot for more info:
http://www.sonicspot.com/guide/wavefiles.html

You can also check GmDlsTool  by xplsv, you can easily look for sample lengths and loop points:
http://www.pouet.net/prod.php?which=30541

Unfortunately diretsound cannot help you loop your wav files, you will need to do it yourself, using a sound buffer.


 
« Last Edit: March 04, 2008 by rbraz »
Challenge Trophies Won:

Offline Nezbie

  • ZX 81
  • *
  • Posts: 2
  • Karma: 1
    • View Profile
Re: 4k softsynth
« Reply #4 on: March 07, 2008 »
Thanks for the welcome (and karma)!

@rbaz
I was afraid of that. Oddly enough, using my current program I get different offsets from blackpawn's GmDlsTool, but they seem to work fine.

I've backtracked on my original idea, and I'm now thinking about using the .mod format as a basis, since it is a simple format, and there are many trackers for it out there. I already have a basic .mod parser set up. A musician would use my tool to load a gm.dls wave file, add effects, export it, and load it as a sample in their favorite tracker. I'd then convert the resulting mod into my own format. However, I'm wondering if the 4-channel limitation would seriously gimp the sound. (I only have very limited tracking experience, and that was 5 years ago). If I did end up creating my own tracker tool instead, I doubt any sane musician would ever want to touch it with a 20ft pole.

I was hoping to have a basic synth set up by Blockparty (which is in a month) so I could pump out a quick 4k, but considering I'm focusing most of my efforts on a PSP engine, I'm afraid I'll have to wait until NVscene (also, I'm only going to have a week left with a computer that supports shaders, since my school term finishes then. I'm hoping to buy a new computer, but need to wait until my co-op starts...).

If I do get a working synth solution, I'll be sure to release the full source to this community. Just don't expect it to compete with some of the nicer sounding solutions out there atm ;-)

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: 4k softsynth
« Reply #5 on: March 07, 2008 »
You are most probably not going to support most .mod-effects for codesize-reasons anyway. So if you just want to give a set of midi-samples to your musician to produce a (very limited) .mod-file, you can also use a midi-file and save basically all player-code by just calling DirectMusic to play it.
If you are still going for the synthesizer-thing, all you need are some oscilators and a fistful of filters and envelopes. You could use the waveforms (the loopable part) from gm.dls as oscilators, but you're usually doing well with just saw, pulse and noise.
However, your first synth will sound just awful. They always do ;)
Don't try to do your own tracker, better create a plugin for an existing one.
« Last Edit: March 07, 2008 by hellfire »
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17414
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: 4k softsynth
« Reply #6 on: March 07, 2008 »
I've backtracked on my original idea, and I'm now thinking about using the .mod format as a basis, since it is a simple format, and there are many trackers for it out there. I already have a basic .mod parser set up. A musician would use my tool to load a gm.dls wave file, add effects, export it, and load it as a sample in their favorite tracker. I'd then convert the resulting mod into my own format. However, I'm wondering if the 4-channel limitation would seriously gimp the sound. (I only have very limited tracking experience, and that was 5 years ago). If I did end up creating my own tracker tool instead, I doubt any sane musician would ever want to touch it with a 20ft pole.

I think that's a wise move, you'll have the devils own job in finding a musician that will use an own made tracker. At least the way you have suggested (making some tool to convert mod pattern data to your own format) then you have a good chance of getting a musician to work on things with you.

In S!P, Haddoc has made a synth for 4kbs, I haven't heard anything with it yet but I know that he had several problems with the tool he wrote to write songs for it and Ampli was tearing his hair out for a while.

You'll find it easier using the trackers that are there already.. I think (but am not 100% sure) that this was the approach Rbraz took when he made his 4kb synth.

As Hellfire says you are not going to want to support loads and loads of mod effects so maybe it would be the best time now to find a good musician that is used to writing small chip tunes and having a discussion with him to find out what effects will be practical for you to include in your synth.. Most likely you need to get a good compromise from the minimum amount of sound channels to save on pattern data and effects to get the best sound possible.

Coder modules will always sound like shit, my best advice to you is get a musician on board asap.

Shockwave ^ Codigos
Challenge Trophies Won:

Offline Rbz

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 2757
  • Karma: 493
    • View Profile
    • https://www.rbraz.com/
Re: 4k softsynth
« Reply #7 on: March 07, 2008 »
Yes, developing your own tracker, a really good one, is really hard and it will never make all musicians happy. But if you have one musician on your side helping you, it's possible.

As Shockwave said, I've used same approach but using XM format, and of course there's a lot of limitations in the player and effects due to limited size, which make it unusable for not "tiny musician specialists". I call "tiny musician specialists" musicians able to handle all limitations of a tiny synth a make a great song :)

One great advantage of that approach, is that even you can make a song "remix" using a normal tracker and changing samples.

I did that, you can download an example attached to this post, one problem that I cannot able to solve was removing some crackling on some samples, in fact I could remove, but I will need to recode the entirely player, which make me go in another direction and make real software synthesizer, not finished yet but it looks promising as "Gargaj" told me ;)

So good luck with your project, all this is a good way to understand how sound works, is really fun IMHO.


Challenge Trophies Won: