home *** CD-ROM | disk | FTP | other *** search
- /* Includes for OS routines */
-
- #include "baricon.h"
- #include "dbox.h"
- #include "event.h"
- #include "flex.h"
- #include "heap.h"
- #include "menu.h"
- #include "msgs.h"
- #include "os.h"
- #include "res.h"
- #include "coords.h"
- #include "resspr.h"
- #include "template.h"
- #include "visdelay.h"
- #include "wimp.h"
- #include "wimpt.h"
- #include "saveas.h"
- #include "xferrecv.h"
- #include "werr.h"
- #include "win.h"
- #include "bbc.h"
- #include "coords.h"
-
- /* Includes for ANSI routines */
-
- #include <string.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
-
- #define PI 3.141592654
-
-
- BOOL quit,dragging;
- wimp_w win_main;
- wimp_box wave;
- int harmonic[16];
- int sint[16][256];
- int harm_val[256];
- char *harm_table;
- wimp_box harm_icons[16];
- int drag_y,drag_h,odx,ody,max_harm;
- char modname[ ] = "Harmonica";
- int *modbase;
-
- char convert_byte(char);
-
- double get_double(wimp_i icon)
- {
- wimp_icon i;
- wimp_get_icon_info(win_main,icon,&i);
- return atof(i.data.indirecttext.buffer);
- }
-
- void redraw_wave(wimp_box *b)
- {
- int y,x,dx = wimpt_dx(),dy = wimpt_dy();
- wimp_setcolour(0);
- bbc_rectanglefill(b->x0,b->y0,b->x1-b->x0-dx,b->y1-b->y0-dy);
- wimp_setcolour(11);
- bbc_move(b->x0,128+b->y0);
- for (x = 0 ; x < 512 ; x += dx)
- {
- y = 128 + harm_val[x/2] * 128/max_harm;
- bbc_draw(x+b->x0,y+b->y0);
- }
- }
-
- void recalc_harms(void)
- {
- int x,h,r;
- max_harm = 1;
- for (x = 0 ; x < 256 ; x++)
- {
- harm_val[x] = 0;
- for (h = 0 ; h < 16 ; h++)
- harm_val[x] += harmonic[h] * sint[h][x];
- if (harm_val[x] > max_harm)
- max_harm = harm_val[x];
- }
- for (x = 0 ; x < 256 ; x++)
- {
- r = 127 + harm_val[x]*128/max_harm;
- if (r < 0) r = 0;
- if (r > 255) r = 255;
- harm_table[x] = convert_byte(r);
- }
- visdelay_percent(25*x/64);
- visdelay_end();
- }
-
- void load_harm(char *fname)
- {
- int h;
- FILE *file;
- file = fopen(fname,"rb");
- for (h = 0 ; h < 16 ; h++)
- harmonic[h] = fgetc(file);
- fclose(file);
- xferrecv_insertfileok();
- }
-
- void redraw_harmonics(wimp_box *r,wimp_box *scr)
- {
- int dx,dy,h,y;
- dx = wimpt_dx();
- dy = wimpt_dy();
- y = harm_icons[0].y1 - harm_icons[0].y0 - dy;
- for (h = 0 ; h < 16 ; h ++)
- if (coords_boxesoverlap(r,&harm_icons[h]))
- {
- wimp_setcolour(11);
- bbc_rectanglefill(
- harm_icons[h].x0+dx - r->x0 + scr->x0,
- harm_icons[h].y0+dy - r->y0 + scr->y0,
- 36-dx, y*harmonic[h]/256);
- wimp_setcolour(0);
- bbc_rectanglefill(
- harm_icons[h].x0+dx - r->x0 + scr->x0,
- harm_icons[h].y0+y+dy - r->y0 + scr->y0,
- 36-dx, -(y-y*harmonic[h]/256));
- }
- }
-
- BOOL save_sample(char *fname,void *handle)
- {
- char temp[256] = "SetType ";
- double pr,rr;
- int pos,ptr;
- FILE *file;
- file = fopen(fname,"wb");
- rr = get_double(26) * 1000.0;
- if (rr == 0) rr = 20000.0;
- pr = get_double(27);
- if (pr == 0) pr = 256;
- for (ptr = 0 ; ptr < (int) rr ; ptr ++)
- {
- pos = (int) ((double)ptr * pr/rr * 256.0);
- pos = pos % 256;
- fputc(harm_table[pos],file);
- }
- fclose(file);
- strcat(temp,fname);
- strcat(temp," &631");
- os_cli(temp);
- return 1;
- }
-
- BOOL save_harms(char *fname,void *handle)
- {
- char temp[256] = "SetType ";
- int h;
- FILE *file;
- handle = handle;
- file = fopen(fname,"wb");
- for (h = 0 ; h < 16 ; h++)
- fputc(harmonic[h],file);
- fclose(file);
- strcat(temp,fname);
- strcat(temp," &635");
- os_cli(temp);
- return 1;
- }
-
- void scr_box(wimp_box *b,wimp_redrawstr *r)
- {
- b->x0 += r->box.x0 - r->scx;
- b->x1 += r->box.x0 - r->scx;
- b->y0 += r->box.y1 - r->scy;
- b->y1 += r->box.y1 - r->scy;
- }
-
- void work_box(wimp_box *b,wimp_redrawstr *r)
- {
- b->x0 -= r->box.x0 - r->scx;
- b->x1 -= r->box.x0 - r->scx;
- b->y0 -= r->box.y1 - r->scy;
- b->y1 -= r->box.y1 - r->scy;
- }
-
- int work_to_val(int y)
- {
- int val = (y - harm_icons[0].y0);
- if (val < 0) val = 0;
- if (val > 255) val = 255;
- return val;
- }
-
- int scr_to_val(int y)
- {
- int val = (y - drag_y);
- if (val < 0) val = 0;
- if (val > 255) val = 255;
- return val;
- }
-
- void redraw_main(void)
- {
- int more;
- wimp_redrawstr r;
- wimp_box red;
- wimp_box wave2;
- wimpt_checkmode();
- r.w = win_main;
- wimpt_noerr(wimp_redraw_wind(&r,&more));
- wave2 = wave;
- scr_box(&wave2,&r);
- while (more)
- {
- red = r.g;
- work_box(&red,&r);
- redraw_wave(&wave2);
- redraw_harmonics(&red,&r.g);
- wimp_get_rectangle(&r,&more);
- }
- }
-
- void redraw_icon(int i)
- {
- int more;
- wimp_redrawstr r;
- wimp_box red;
- wimpt_checkmode();
- r.w = win_main;
- r.box = harm_icons[i];
- wimpt_noerr(wimp_update_wind(&r,&more));
- while (more)
- {
- red = r.g;
- work_box(&red,&r);
- redraw_harmonics(&red,&r.g);
- wimp_get_rectangle(&r,&more);
- }
- }
-
- void mouse_harm(int x,int y,int harm)
- {
- wimp_dragstr d;
- wimp_wstate w;
- if (dragging) return;
- d.window = win_main;
- d.type = wimp_USER_HIDDEN;
- d.box.x0 = d.box.x1 = x;
- d.box.y0 = d.box.y1 = y;
- d.parent = harm_icons[harm];
- wimp_get_wind_state(win_main,&w);
- scr_box(&d.parent,(wimp_redrawstr *)&w.o);
- drag_y = d.parent.y0;
- drag_h = harm;
- wimp_drag_box(&d);
- dragging = 1;
- win_claim_idle_events(win_main);
- odx = x; ody = -y;
- }
-
- void open_win(wimp_w handle)
- {
- wimp_wstate w;
- wimp_get_wind_state(handle,&w);
- w.o.behind = -1;
- wimp_open_wind(&w.o);
- }
-
- void play_harm(void)
- {
- os_regset r;
- double pitch;
- pitch = get_double(27);
- if (pitch == 0.0) pitch = 256.0;
- pitch = (double)0x4000 + (double)0x1000 * log(pitch/(double)256) / log((double)2);
- r.r[0] = 1;
- r.r[1] = 0x1d0;
- r.r[2] = (int) pitch;
- r.r[3] = 0x14;
- os_swi(0x60189,&r);
- }
-
- void handler(wimp_eventstr *e,void *handle)
- {
- char *name;
- wimp_mousestr mse;
- handle = handle;
- switch (e->e)
- {
- case wimp_ENULL:
- if (dragging)
- {
- wimp_get_point_info(&mse);
- if (mse.y != ody && mse.bbits != 0)
- {
- ody = mse.y;
- harmonic[drag_h] = scr_to_val(mse.y);
- redraw_icon(drag_h);
- }
- if (mse.bbits == 0)
- {
- dragging = 0;
- win_claim_idle_events(-1);
- recalc_harms();
- wimp_close_wind(win_main);
- open_win(win_main);
- }
- }
- break;
- case wimp_EREDRAW:
- if (e->data.o.w == win_main)
- {
- if (dragging) redraw_icon(drag_h);
- else redraw_main();
- }
- break;
- case wimp_EOPEN:
- wimp_open_wind(&e->data.o);
- break;
- case wimp_ECLOSE:
- quit = 1;
- wimp_close_wind(e->data.o.w);
- break;
- case wimp_EBUT:
- if (e->data.but.m.w == win_main)
- {
- if (e->data.but.m.i == 25)
- play_harm();
- if (e->data.but.m.i == 31)
- saveas(0x631,"LogSample",20000,save_sample,0,0,0);
- if (e->data.but.m.i == 30)
- saveas(0x635,"Harmonics",16,save_harms,0,0,0);
- if (e->data.but.m.i >= 1 && e->data.but.m.i <= 16)
- mouse_harm(e->data.but.m.x,e->data.but.m.y,(int) e->data.but.m.i-1);
- }
- break;
- case wimp_EKEY:
- wimp_processkey(e->data.key.chcode);
- break;
- case 17:
- case 18:
- switch (e->data.msg.hdr.action)
- {
- case wimp_MDATALOAD:
- case wimp_MDATAOPEN:
- if (xferrecv_checkinsert(&name) == 0x635)
- {
- e->data.msg.hdr.your_ref = e->data.msg.hdr.my_ref;
- e->data.msg.hdr.action = 4;
- e->data.msg.data.dataload.size = -1;
- load_harm(e->data.msg.data.dataload.name);
- recalc_harms();
- wimp_close_wind(win_main);
- open_win(win_main);
- }
- break;
- }
- break;
- }
- }
-
- BOOL create_win(char *name,wimp_w *handle)
- {
- wimp_wind *win;
- win = template_syshandle(name);
- if (win == 0) return FALSE;
- return (wimpt_complain(wimp_create_wind(win,handle)) == 0);
- }
-
- void init(void)
- {
- int c,s;
- os_regset r;
- double d;
- wimp_icon i;
- visdelay_init();
- visdelay_begin();
- wimpt_init("Harmonica");
- res_init("Harmonica");
- resspr_init();
- template_init();
- dbox_init();
- flex_init();
- heap_init(TRUE);
- r.r[0] = 18;
- r.r[1] = (int)modname;
- os_swi(0x2001e,&r);
- modbase = (int *)r.r[3];
- harm_table = (char *)modbase[24];
- create_win("main",&win_main);
- win_activeinc();
- win_register_event_handler(win_main,handler,0);
- event_setmask(0);
- open_win(win_main);
- wimp_get_icon_info(win_main,0,&i);
- wave = i.box;
- for (c = 0 ; c < 16 ; c++)
- {
- for (s = 0 ; s < 256 ; s++)
- {
- d = ((double)(c+(double)1)*(double)s*2*PI/256);
- sint[c][s] = (int)(((double) 256) * sin(d));
- }
- harmonic[c] = 128/(c+1);
- if (c != 0) harmonic[c] = 0;
- wimp_get_icon_info(win_main,1+c,&i);
- harm_icons[c] = i.box;
- visdelay_percent(25*c/4);
- }
- recalc_harms();
- }
-
- int main(int argc,char *argv[ ])
- {
- quit = 0;
- init();
- bbc_vdu(7);
- if (argc >= 2 && argv[1][0] == 45)
- load_harm(&argv[1][1]);
- while (!quit)
- event_process();
- }
-