Subversion Repositories pentevo

Rev

Rev 796 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed | ?url?

  1. #include "std.h"
  2.  
  3. #include "emul.h"
  4. #include "vars.h"
  5. #include "gs.h"
  6. #include "tape.h"
  7. #include "config.h"
  8. #include "sndrender/sndcounter.h"
  9. #include "sound.h"
  10.  
  11.  
  12. int spkr_dig = 0, mic_dig = 0, covFB_vol = 0, covDD_vol = 0, sd_l = 0, sd_r = 0;
  13.  
  14. void flush_dig_snd()
  15. {
  16. //   __debugbreak();
  17.    if (temp.sndblock)
  18.        return;
  19.    int mono = spkr_dig+mic_dig+covFB_vol+covDD_vol;
  20. //   printf("mono=%u\n", mono);
  21. //[vv]  
  22. sound.update(cpu.t - temp.cpu_t_at_frame_start, unsigned(mono + sd_l), unsigned(mono + sd_r));
  23. }
  24.  
  25. void init_snd_frame()
  26. {
  27.    temp.cpu_t_at_frame_start = cpu.t;
  28. //[vv]  
  29.    sound.start_frame();
  30.    comp.tape.sound.start_frame();
  31.  
  32.    if (conf.sound.ay_scheme)
  33.    {
  34.       ay[0].start_frame();
  35.       if (conf.sound.ay_scheme > AY_SCHEME_SINGLE)
  36.           ay[1].start_frame();
  37.    }
  38.  
  39.    Saa1099.start_frame();
  40.  
  41.    #ifdef MOD_GS
  42.    init_gs_frame();
  43.    #endif
  44. }
  45.  
  46. static float y_1[2] = { 0.0f };
  47. static i16 x_1[2] = { 0 };
  48.  
  49. void flush_snd_frame()
  50. {
  51.    tape_bit();
  52.    #ifdef MOD_GS
  53.    flush_gs_frame();
  54.    #endif
  55.  
  56.    if (temp.sndblock)
  57.        return;
  58.  
  59.    unsigned endframe = cpu.t - temp.cpu_t_at_frame_start;
  60.  
  61.    if (conf.sound.ay_scheme)
  62.    { // sound chip present
  63.  
  64.       ay[0].end_frame(endframe);
  65.       // if (conf.sound.ay_samples) mix_dig(ay[0]);
  66.  
  67.       if (conf.sound.ay_scheme > AY_SCHEME_SINGLE)
  68.       {
  69.  
  70.          ay[1].end_frame(endframe);
  71.          // if (conf.sound.ay_samples) mix_dig(ay[1]);
  72.  
  73.          if (conf.sound.ay_scheme == AY_SCHEME_PSEUDO)
  74.          {
  75.             unsigned char last = ay[0].get_r13_reloaded()? 13 : 12;
  76.             for(unsigned char r = 0; r <= last; r++)
  77.             {
  78.                 ay[1].select(r);
  79.                 ay[1].write(0, ay[0].get_reg(r));
  80.             }
  81.          }
  82.       }
  83.  
  84.       if (savesndtype == 2)
  85.       {
  86.          if (!vtxbuf)
  87.          {
  88.             vtxbuf = (unsigned char*)malloc(32768);
  89.             vtxbufsize = 32768;
  90.             vtxbuffilled = 0;
  91.          }
  92.  
  93.          if (vtxbuffilled + 14 >= vtxbufsize)
  94.          {
  95.             vtxbufsize += 32768;
  96.             vtxbuf = (unsigned char*)realloc(vtxbuf, vtxbufsize);
  97.          }
  98.  
  99.          for (unsigned char r = 0; r < 14; r++)
  100.             vtxbuf[vtxbuffilled+r] = ay[0].get_reg(r);
  101.  
  102.          if (!ay[0].get_r13_reloaded())
  103.             vtxbuf[vtxbuffilled+13] = 0xFF;
  104.  
  105.          vtxbuffilled += 14;
  106.       }
  107.    }
  108.    Saa1099.end_frame(endframe);
  109.  
  110.    sound.end_frame(endframe);
  111.    // if (comp.tape.play_pointer) // play tape pulses
  112.    comp.tape.sound.end_frame(endframe);
  113.    // else comp.tape.sound.end_empty_frame(endframe);
  114.  
  115.    unsigned bufplay, n_samples;
  116.    sndcounter.begin();
  117.  
  118.    sndcounter.count(sound);
  119.    sndcounter.count(comp.tape.sound);
  120.    if (conf.sound.ay_scheme)
  121.    {
  122.       sndcounter.count(ay[0]);
  123.       if (conf.sound.ay_scheme > AY_SCHEME_SINGLE)
  124.          sndcounter.count(ay[1]);
  125.    }
  126.  
  127.    sndcounter.count(Saa1099);
  128.  
  129. #ifdef MOD_GS
  130.    #ifdef MOD_GSZ80
  131.    if (conf.gs_type==1)
  132.        sndcounter.count(z80gs::sound);
  133.    #endif
  134.  
  135.    #ifdef MOD_GSBASS
  136.    // if (conf.gs_type==2) { gs.mix_fx(); return; }
  137.    #endif
  138. #endif // MOD_GS
  139.    sndcounter.end(bufplay, n_samples);
  140.  
  141.   for (unsigned k = 0; k < n_samples; k++, bufplay++)
  142.   {
  143.       u32 v = sndbuf[bufplay & (SNDBUFSIZE-1)];
  144.       u32 Y;
  145.       if(conf.RejectDC) // DC rejection filter
  146.       {
  147.           i16 x[2];
  148.           float y[2];
  149.           x[0] = i16(v & 0xFFFF);
  150.           x[1] = i16(v >> 16U);
  151.           y[0] = 0.995f * (x[0] - x_1[0]) + 0.99f * y_1[0];
  152.           y[1] = 0.995f * (x[1] - x_1[1]) + 0.99f * y_1[1];
  153.           x_1[0] = x[0];
  154.           x_1[1] = x[1];
  155.           y_1[0] = y[0];
  156.           y_1[1] = y[1];
  157.           Y = u32(((i16(y[1]) & 0xFFFF)<<16) | (i16(y[0]) & 0xFFFF));
  158.       }
  159.       else
  160.       {
  161.           Y = v;
  162.       }
  163.  
  164.       sndplaybuf[k] = Y;
  165.       sndbuf[bufplay & (SNDBUFSIZE-1)] = 0;
  166.    }
  167.  
  168. #if 0
  169. //   printf("n_samples=%u\n", n_samples);
  170.    for (unsigned k = 0; k < n_samples; k++, bufplay++)
  171.    {
  172.       sndplaybuf[k] = sndbuf[bufplay & (SNDBUFSIZE-1)];
  173. /*
  174.       if(sndplaybuf[k] == 0x20002000)
  175.           __debugbreak();
  176. */
  177.       sndbuf[bufplay & (SNDBUFSIZE-1)] = 0;
  178.    }
  179. #endif
  180.    spbsize = n_samples*4;
  181. //   assert(spbsize != 0);
  182.  
  183. return;
  184.  
  185. /*
  186.  
  187.    // count available samples and copy to sound buffer
  188.    unsigned save_ticks = temp.snd_frame_ticks; // sound output limit = 1 frame
  189.    save_ticks = min(save_ticks, sound.ready_samples());
  190.    save_ticks = min(save_ticks, comp.ay->sound.ready_samples());
  191.    save_ticks = min(save_ticks, comp.tape.sound.ready_samples());
  192.    #ifdef MOD_GSZ80
  193.    if (conf.gs_type == 1)
  194.       save_ticks = min(save_ticks, z80gs::sound.ready_samples());
  195.    #endif
  196.  
  197.    // fx player always gives enough samples
  198.    #ifdef MOD_GSBASS
  199.    if (conf.gs_type == 2)
  200.       for (int i = 0; i < 4; i++)
  201.          save_ticks = min(save_ticks, gs.chan[i].sound_state.ready_samples());
  202.    #endif
  203. */
  204.  
  205. }
  206.  
  207. void restart_sound()
  208. {
  209. //   printf("%s\n", __FUNCTION__);
  210.  
  211.    unsigned cpufq = conf.intfq * conf.frame;
  212.    sound.set_timings(cpufq, conf.sound.fq);
  213.    comp.tape.sound.set_timings(cpufq, conf.sound.fq);
  214.    if (conf.sound.ay_scheme)
  215.    {
  216.       ay[0].set_timings(cpufq, conf.sound.ayfq, conf.sound.fq);
  217.       if (conf.sound.ay_scheme > AY_SCHEME_SINGLE) ay[1].set_timings(cpufq, conf.sound.ayfq, conf.sound.fq);
  218.    }
  219.  
  220.    Saa1099.set_timings(cpufq, conf.sound.saa1099fq, conf.sound.fq);
  221.  
  222.    // comp.tape.sound.clear();
  223.    #ifdef MOD_GS
  224.    reset_gs_sound();
  225.    #endif
  226.  
  227.    sndcounter.reset();
  228.  
  229.    memset(sndbuf, 0, sizeof sndbuf);
  230. }
  231.  
  232. void apply_sound()
  233. {
  234.    if (conf.sound.ay_scheme < AY_SCHEME_QUADRO) comp.active_ay = 0;
  235.  
  236.    load_ay_stereo();
  237.    load_ay_vols();
  238.  
  239.    ay[0].set_chip((SNDCHIP::CHIP_TYPE)conf.sound.ay_chip);
  240.    ay[1].set_chip((SNDCHIP::CHIP_TYPE)conf.sound.ay_chip);
  241.  
  242.    const SNDCHIP_VOLTAB *voltab = (SNDCHIP_VOLTAB*)&conf.sound.ay_voltab;
  243.    const SNDCHIP_PANTAB *stereo = (SNDCHIP_PANTAB*)&conf.sound.ay_stereo_tab;
  244.    ay[0].set_volumes(unsigned(conf.sound.ay_vol), voltab, stereo);
  245.  
  246.    SNDCHIP_PANTAB reversed;
  247.    if (conf.sound.ay_scheme == AY_SCHEME_PSEUDO) {
  248.       for (int i = 0; i < 6; i++)
  249.          reversed.raw[i] = stereo->raw[i^1]; // swap left/right
  250.       stereo = &reversed;
  251.    }
  252.    ay[1].set_volumes(unsigned(conf.sound.ay_vol), voltab, stereo);
  253.  
  254.  
  255.    #ifdef MOD_GS
  256.    apply_gs();
  257.    #endif
  258.  
  259.    restart_sound();
  260. }
  261.  
  262. /*
  263. #define SAMPLE_SIZE (1024*3)
  264. #define SAMPLE_T    256
  265. int waveA[SAMPLE_SIZE], waveB[SAMPLE_SIZE], waveC[SAMPLE_SIZE];
  266.  
  267. void mix_dig(SNDCHIP &chip)
  268. {
  269.    unsigned base = sb_start_frame >> TICK_FF;
  270.    for (unsigned i = 0; i < temp.snd_frame_samples; i++) {
  271.  
  272.       ta += fa; while (ta >= SAMPLE_SIZE*0x100) ta -= SAMPLE_SIZE*0x100;
  273.       tb += fb; while (tb >= SAMPLE_SIZE*0x100) tb -= SAMPLE_SIZE*0x100;
  274.       tc += fc; while (tc >= SAMPLE_SIZE*0x100) tc -= SAMPLE_SIZE*0x100;
  275.       tn += fn;
  276.       while (tn >= 0x10000) {
  277.          ns = (ns*2+1) ^ (((ns>>16)^(ns>>13)) & 1);
  278.          bitN = 0 - ((ns >> 16) & 1);
  279.          tn -= 0x10000;
  280.       }
  281.       te += fe;
  282.       while (te >= 0x10000) {
  283.          env += denv;
  284.          if (env & ~31) {
  285.             unsigned mask = 1 << r_env;
  286.             if (mask & ((1<<0)|(1<<1)|(1<<2)|(1<<3)|(1<<4)|(1<<5)|(1<<6)|(1<<7)|(1<<9)|(1<<15)))
  287.                env = denv = 0;
  288.             else if (mask & ((1<<8)|(1<<12)))
  289.                env &= 31;
  290.             else if (mask & ((1<<10)|(1<<14)))
  291.                denv = -(int)denv, env = env + denv;
  292.             else env = 31, denv = 0; //11,13
  293.          }
  294.          te -= 0x10000;
  295.       }
  296.  
  297.       unsigned left = 0, right = 0, en, vol;
  298.  
  299.       en = (r_vA & 0x10) ? env : (r_vA & 0x0F)*2+1;
  300.       vol = (bitN | bit3) & (waveA[ta/0x100] | bit0) & 0xFFFF;
  301.       left += vol*vols[0][en], right += vol*vols[1][en];
  302.  
  303.       en = (r_vB & 0x10) ? env : (r_vB & 0x0F)*2+1;
  304.       vol = (bitN | bit4) & (waveB[tb/0x100] | bit1) & 0xFFFF;
  305.       left += vol*vols[2][en], right += vol*vols[3][en];
  306.  
  307.       en = (r_vC & 0x10) ? env : (r_vC & 0x0F)*2+1;
  308.       vol = (bitN | bit5) & (waveC[tc/0x100] | bit2) & 0xFFFF;
  309.       left += vol*vols[4][en], right += vol*vols[5][en];
  310.  
  311.       *(unsigned*)&sndbuf[(i+base) & (SNDBUFSIZE-1)] += (left >> 16) + (right & 0xFFFF0000);
  312.    }
  313.    sound.flush_empty();
  314. }
  315.  
  316. #define PI 3.14159265359
  317.  
  318. double sin1(int i) {
  319.    while (i > SAMPLE_SIZE) i -= SAMPLE_SIZE;
  320.    if (i < SAMPLE_SIZE/2) return (double)i*2/SAMPLE_SIZE;
  321.    return 2-(double)i*2/SAMPLE_SIZE;
  322. }
  323. double cos1(int i) {
  324.    return 1-sin1(i);
  325. }
  326.  
  327. int *wavs[3] = { waveA, waveB, waveC };
  328. void make_samples()
  329. {
  330.    #define cl (0.35)
  331.    #define cl2 (0.25)
  332.    #define clip(x) (((x>cl) ? cl : (x < cl) ? -cl : x)/cl)
  333.    #define clip2(x) ((x < -cl2) ? 0 : (x+cl2))
  334.    for (int i = 0; i < SAMPLE_SIZE; i++) {
  335.       double p1 = 0.8+0.2*sin1(i*4);
  336.       double p2 = 0.7+0.3*cos1(i*2);
  337.       double p3 = 0.9+0.1*sin1(i);
  338.       double t = (double)(i % SAMPLE_T)*2*PI/SAMPLE_T;
  339. //      #define fabs(x) (x)
  340.       waveA[i] = (unsigned)(fabs(p1*clip(1+sin(3*t/2))*0.7+p3*clip(sin(t))+p1*sin(4*t)*0.25+p2*clip2(cos(1+6*t)))*0x3FFF);
  341.       waveB[i] = (unsigned)(fabs(p1*clip(2+sin(3*t/2))*0.7+p3*clip(sin(t))+p1*sin(1+7*t/2)*0.4+p2*clip2(cos(2+5*t)))*0x3FFF);
  342.       waveC[i] = (unsigned)(fabs(p1*clip(0.5+sin(3*t/2))*0.7+p3*clip(sin(t))+p1*sin(0.2+9*t/2)*0.6+p2*clip2(cos(3+5*t)))*0x3FFF);
  343. //      #undef fabs
  344.    }
  345.    #undef clip
  346.    #undef cl
  347.    #undef cl2
  348.    #undef clip2
  349.    for (int ind = 0; ind < 3; ind++) {
  350.       int *arr = wavs[ind], max = -0x7FFFFFFF, min = 0x7FFFFFFF;
  351.       for (int i1 = 0; i1 < SAMPLE_SIZE; i1++) {
  352.          if (arr[i1] > max) max = arr[i1];
  353.          if (arr[i1] < min) min = arr[i1];
  354.       }
  355.       for (i1 = 0; i1 < SAMPLE_SIZE; i1++)
  356.          arr[i1] = (int)(((double)arr[i1] - min)*0x10000/(max-min));
  357.    }
  358. }
  359. */
  360.