Click here to Skip to main content
15,897,518 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: can anyone suggest how to convert matlab code into c code..for resampling function Pin
Richard MacCutchan21-Aug-14 23:23
mveRichard MacCutchan21-Aug-14 23:23 
GeneralRe: can anyone suggest how to convert matlab code into c code..for resampling function Pin
mybm121-Aug-14 23:25
mybm121-Aug-14 23:25 
GeneralRe: can anyone suggest how to convert matlab code into c code..for resampling function Pin
Richard MacCutchan22-Aug-14 0:19
mveRichard MacCutchan22-Aug-14 0:19 
GeneralRe: can anyone suggest how to convert matlab code into c code..for resampling function Pin
mybm122-Aug-14 0:21
mybm122-Aug-14 0:21 
GeneralRe: can anyone suggest how to convert matlab code into c code..for resampling function Pin
Richard MacCutchan22-Aug-14 0:34
mveRichard MacCutchan22-Aug-14 0:34 
AnswerRe: can anyone suggest how to convert matlab code into c code..for resampling function Pin
Chris Losinger22-Aug-14 8:32
professionalChris Losinger22-Aug-14 8:32 
GeneralRe: can anyone suggest how to convert matlab code into c code..for resampling function Pin
mybm122-Aug-14 18:41
mybm122-Aug-14 18:41 
GeneralRe: can anyone suggest how to convert matlab code into c code..for resampling function Pin
enhzflep23-Aug-14 18:31
enhzflep23-Aug-14 18:31 
Here's some code that does re-sampling of audio. No idea if it'll be of any use to you - googling for how to do it it was not much easier than implementing it. It was used for up-sampling 8363 samples/sec sound into 44100 samples/sec sound. It was used for working with the audio samples as found in .XM files (somewhat similar to Amiga .mod files, or less-so, .MIDI files)

There's bound to be a bunch of code here that's useless to you, but I've no idea which format your .WAV files are, so I'll leave it to you to use that which you need. You're also on-your-own when it comes to reading the samples from a WAV file - in my instance, the header of the samples is different to the .WAV file header - my data also came with a single header - the wav file contains a number of chunks, each of these has its own header.

The re-sampling is of the linear variety.
You can determine the play-time of the WAV file by looking at a number of the chunk headers inside it. Don't ask me how, as I haven't bothered to do it, and don't know. (nor am I interested)

This page seems to be an easier read than the Wikipedia WAV page - http://www.sonicspot.com/guide/wavefiles.html[^]

C++
float *convert8tofloat(char *input, int numSamples)
{
    float *result = new float[numSamples];
    int i;
    for (i=0; i<numSamples; i++)
    {
        result[i] = input[i] / 128.0; //(((input[i]+128)/255.0) - 0.5) * 2.0;
    }
    return result;
}

float *convert16tofloat(short *input, int numSamples)
{
    float *result = new float[numSamples];
    int i;
    for (i=0; i<numSamples; i++)
    {
        result[i] = (int)input[i] / 32768.0; //(((input[i]+128)/255.0) - 0.5) * 2.0;
    }
    return result;
}

// t in range [0..1]
char interpolate8(char val1, char val2, float t)
{
    short difference;
    difference = val2 - val1;
    return val1 + t*difference;
}
// t in range [0..1]
short interpolate16(short val1, short val2, float t)
{
    int difference;
    difference = val2 - val1;
    return val1 + t*difference;
}
// t in range [0..1]
float interpolateFloat32(float val1, float val2, float t)
{
    float difference;
    difference = val2 - val1;
    return val1 + t*difference;
}


char *resample8Interp(char *input, int numSamplesIn, int numSamplesOut)
{
    char *output;
    output = new char[numSamplesOut];
    float curIndex, proportion, step;
    char curVal;
    int i, intPart;

    output[0] = input[0];
    output[numSamplesOut-1] = input[numSamplesIn-1];
    step = (float) (numSamplesIn-1) / (numSamplesOut-1);

    for (i=1; i<numSamplesOut-1; i++)
    {
        curIndex = (float) i * step;
        intPart = floor(curIndex);
        proportion = curIndex - intPart;
        curVal = interpolate8(input[intPart], input[intPart+1], proportion);
        output[i] = curVal;
    }
    return(output);
}

short *resample16Interp(short *input, int numSamplesIn, int numSamplesOut)
{
    short *output;
    output = new short[numSamplesOut];
    float curIndex, proportion, step;
    short curVal;
    int i, intPart;

    output[0] = input[0];
    output[numSamplesOut-1] = input[numSamplesIn-1];
    step = (float) (numSamplesIn-1) / (numSamplesOut-1);

    for (i=1; i<numSamplesOut-1; i++)
    {
        curIndex = (float) i * step;
        intPart = floor(curIndex);
        proportion = curIndex - intPart;
        curVal = interpolate16(input[intPart], input[intPart+1], proportion);
        output[i] = curVal;
    }
    return(output);
}

float *reSampleFloat32(float *srcArray, int origNumSamples, int origRate, int newRate, int &outNumSamples)
{
    outNumSamples = (origNumSamples * newRate) / origRate;
    float *output = new float[outNumSamples];
    int i, intPart;
    float fractionalPart, curIndex, curVal, step;

    step = (float)(origNumSamples-1) / (outNumSamples-1);
    output[0] = srcArray[0];
    output[outNumSamples-1] = srcArray[origNumSamples-1];
    for (i=0; i<outNumSamples; i++)
    {
        curIndex = step * i;
        intPart = floor(curIndex);
        fractionalPart = curIndex - intPart;
        curVal = interpolateFloat32(srcArray[intPart], srcArray[intPart+1], fractionalPart);
        output[i] = curVal;
    }
    return output;
}


Here's some examples of implementation. I dont remember a thing about the code, haven't touched it in years - nor am I inclined to. That's an exercise for you. I assume some of the comments are misleading - I just changed the one that says "only works for 16 bit samples" - it had said "only works for 8 bit samples, which was clearly inaccurate.

Finally, not that in my case I wanted to convert the samples to be a 32-bit floating point format in the range [-1..1] which simplifies drawing them to screen.

Good luck!

C++
void onSmpListChange(HWND smpListBox, XMFile *mSong)
{
    int curIns, curSmp;
    HWND dlg = GetParent(smpListBox);
    HWND insList = GetDlgItem(dlg, IDC_INS_LIST);

    curIns = SendMessage(insList, LB_GETCURSEL, 0, 0);
    curSmp = SendMessage(smpListBox, LB_GETCURSEL, 0, 0);

    XMSample *curSample = &mSong->instruments[curIns].samples[curSmp];
    int bits=8, numSamples = curSample->header.sampleLength;
    float *tmpBuffer;

    if (curSample->header.compression == 0)
        SetDlgItemText(dlg, IDC_COMP, "Delta");
    else
    {
        SetDlgItemText(dlg, IDC_COMP, "ADPCM");
        return;
    }

    // only works for 16 bit samples
    if (curSample->header.type & 0x16)
    {
        printf("Resampling 16bit sound, %d samples - %20.20s\n", numSamples, curSample->header.sampleName);
        numSamples /= 2;
        SetDlgItemInt(dlg, IDC_BITS, 16, true);
        char fileName[48];
        strcpy(fileName, "16bit 8363hz.wav");
//        strncat(fileName, curSample->header.sampleName, 20);
        writeWavFile(fileName, curSample->data, numSamples, 16, 1, 8363);

        short *sBuf = resample16Interp((short*)curSample->data, numSamples, 44100*numSamples / 8363);
        strcpy(fileName, "16bit 44100hz.wav");
//        strncat(fileName, curSample->header.sampleName, 20);
        writeWavFile(fileName, sBuf, (44100*numSamples)/8363, 16, 1, 44100);
        delete sBuf;

        tmpBuffer = convert16tofloat((short*)curSample->data, numSamples);
    }

    else
    {
        printf("Resampling 8bit sound, %d samples - %20.20s\n", numSamples, curSample->header.sampleName);
        SetDlgItemInt(dlg, IDC_BITS, 8, true);
        char fileName[48];
        strcpy(fileName, "8bit 8363hz.wav");
//        strncat(fileName, curSample->header.sampleName, 20);
        writeWavFile(fileName, curSample->data, numSamples, 8, 1, 8363);

        char *cBuf = resample8Interp((char*)curSample->data, numSamples, 44100*numSamples / 8363);
        strcpy(fileName, "8bit 44100hz.wav");
//        strncat(fileName, curSample->header.sampleName, 20);
        writeWavFile(fileName, cBuf, (44100*numSamples)/8363, 8, 1, 44100);
        delete cBuf;

        tmpBuffer = convert8tofloat((char*)curSample->data, numSamples);
    }
    // wParam = buffer (float)  // lParam = count
    SetDlgItemInt(dlg, IDC_NUM, numSamples, true);

    SendDlgItemMessage(dlg, IDC_WAV_IMG, WM_SET_DATA, (WPARAM)tmpBuffer, (LPARAM)numSamples);// * 44100 / 8835);



    delete g_curSamples;
    g_curSamples = tmpBuffer;
    g_numSamples = numSamples;// * 44100/8355;
}


void onConvertBtn(HWND parentDlg)
{
    float *mSamples;
    int nSamples;

//    mSamples = reSample(g_curSamples, g_numSamples, 8363, 44100, nSamples);
    mSamples = reSampleFloat32(g_curSamples, g_numSamples, xmRate, 44100, nSamples);
    SendDlgItemMessage(parentDlg, IDC_WAV_IMG2, WM_SET_DATA, (WPARAM)mSamples, (LPARAM)nSamples);
    delete mSamples;
}

GeneralRe: can anyone suggest how to convert matlab code into c code..for resampling function Pin
mybm124-Aug-14 19:30
mybm124-Aug-14 19:30 
AnswerRe: can anyone suggest how to convert matlab code into c code..for resampling function Pin
jschell22-Aug-14 10:21
jschell22-Aug-14 10:21 
GeneralRe: can anyone suggest how to convert matlab code into c code..for resampling function Pin
Albert Holguin28-Aug-14 5:51
professionalAlbert Holguin28-Aug-14 5:51 
Questionhow to call a function inside another function in file like utility.c Pin
mybm120-Aug-14 0:03
mybm120-Aug-14 0:03 
AnswerRe: how to call a function inside another function in file like utility.c Pin
Richard MacCutchan20-Aug-14 1:00
mveRichard MacCutchan20-Aug-14 1:00 
GeneralRe: how to call a function inside another function in file like utility.c Pin
mybm120-Aug-14 1:22
mybm120-Aug-14 1:22 
GeneralRe: how to call a function inside another function in file like utility.c Pin
Richard MacCutchan20-Aug-14 3:06
mveRichard MacCutchan20-Aug-14 3:06 
AnswerRe: how to call a function inside another function in file like utility.c Pin
WuRunZhe21-Aug-14 2:20
WuRunZhe21-Aug-14 2:20 
AnswerRe: how to call a function inside another function in file like utility.c Pin
Richard Andrew x6422-Aug-14 8:59
professionalRichard Andrew x6422-Aug-14 8:59 
Questionerror: invalid operands to binary / (have ‘float’ and ‘int *’) Pin
mybm119-Aug-14 20:24
mybm119-Aug-14 20:24 
AnswerRe: error: invalid operands to binary / (have ‘float’ and ‘int *’) Pin
enhzflep19-Aug-14 20:51
enhzflep19-Aug-14 20:51 
GeneralRe: error: invalid operands to binary / (have ‘float’ and ‘int *’) Pin
mybm119-Aug-14 21:01
mybm119-Aug-14 21:01 
GeneralRe: error: invalid operands to binary / (have ‘float’ and ‘int *’) Pin
enhzflep19-Aug-14 21:18
enhzflep19-Aug-14 21:18 
GeneralRe: error: invalid operands to binary / (have ‘float’ and ‘int *’) Pin
mybm120-Aug-14 0:42
mybm120-Aug-14 0:42 
AnswerRe: error: invalid operands to binary / (have ‘float’ and ‘int *’) Pin
David Crow20-Aug-14 3:15
David Crow20-Aug-14 3:15 
QuestionVariable not same across process in DLL Pin
peterasloan17-Aug-14 18:49
peterasloan17-Aug-14 18:49 
AnswerRe: Variable not same across process in DLL Pin
Richard MacCutchan17-Aug-14 20:39
mveRichard MacCutchan17-Aug-14 20:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.