3

我正在尝试使用这种技术实现音频合成器:

https://ccrma.stanford.edu/~stilti/papers/blit.pdf

我正在使用 SDL2_Mixer 库在标准 C 中执行此操作。

这是我的 BLIT 函数实现:

double blit(double angle, double M, double P) {
    double x = M * angle / P;
    double denom = (M * sin(M_PI * angle / P));
    if (denom < 1)
        return (M / P) * cos(M_PI * x) / cos(M_PI * x / M);
    else {
        double numerator = sin(M_PI * x);
        return (M / P) * numerator / denom;
    }
}

这个想法是按照纸上的说明将其结合起来生成方波。我使用以下配置设置了 SDL2_mixer:

SDL_AudioSpec *desired, *obtained;
SDL_AudioSpec *hardware_spec;

desired = (SDL_AudioSpec*)malloc(sizeof(SDL_AudioSpec));
obtained = (SDL_AudioSpec*)malloc(sizeof(SDL_AudioSpec));

desired->freq=44100;
desired->format=AUDIO_U8;
desired->channels=1;
desired->samples=2048;
desired->callback=create_rect;
desired->userdata=NULL;

这是我的create_rect功能。它创建了一个双极脉冲序列,然后整合它的值来生成一个带限的矩形函数。

void create_rect(void *userdata, Uint8 *stream, int len) {
    static double angle = 0;
    static double integral = 0;
    int i = 0;
    // This is the freq of my tone
    double f1 = tone_table[current_wave.note];
    // Sample rate
    double fs = 44100;
    // Pulse
    double P = fs / f1;
    int M = 2 * floor(P / 2) + 1;

    double oldbipolar = 0;
    double bipolar = 0;
    for(i = 0; i < len; i++) {
        if (++angle > P)
            angle -= P;
        double angle2 = angle + floor(P/2);
        if (angle2 > P)
            angle2 -= P;

        bipolar = blit(angle2, M, P) - blit(angle, M, P);

        integral += (bipolar + old bipolar) * 0.5;
        oldbipolar = bipolar;
        *stream++ = (integral + 0.5) * 127;
    }
}

我的问题是:产生的波很好,但几秒钟后它开始发出噪音。我试图绘制结果,这是它:

标准版本 更多放大版 评论区

任何想法?

编辑:这是双极 BLIT 在集成之前的图:

BLIT 缩放 BLIT

4

0 回答 0