home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / audio / p / sequencer / !Sequencer / c / files next >
Encoding:
Text File  |  1990-02-22  |  5.6 KB  |  232 lines

  1. #include "headers.h"
  2. #include "xfersend.h"    
  3. #include "xferrecv.h"
  4. #include "win.h"    
  5. #include "werr.h"              
  6. #include "wimpt.h"
  7. #include "os.h"
  8. #include "bbc.h"
  9. #include "dboxquery.h"
  10. #include "kernel.h"   
  11. #include "swis.h"
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15.  
  16. extern int     voice_map[];
  17. extern char    filename[];
  18. extern BOOL    modified;
  19. extern BOOL    loaded;
  20. extern char    win_title[];
  21. extern wimp_w  main_win_handle;
  22. extern pattern *this_patternptr;
  23. extern pattern *headptr;
  24. extern char    tempo_buff[];
  25. extern char    volume_buff[];
  26.  
  27. static BOOL filename_ok(char *name);
  28. static void load_error(void);
  29. static void load_finish(BOOL, char *);
  30. static void save_finish(BOOL, char *);
  31.  
  32. /****************************** SAVE TO FILE *********************************/
  33.  
  34. BOOL save_file(char *fname, void *handle)
  35. {
  36.   int        i;
  37.   pattern    *patternptr = headptr;
  38.   char       *voice;
  39.   os_filestr file;
  40.   os_regset  regs;
  41.   FILE       *fp;       
  42.  
  43.   handle = handle;
  44.           
  45.   if (!filename_ok(fname)) return FALSE;                
  46.  
  47.   if ((fp = fopen(fname,"w")) == NULL) {
  48.     error(); 
  49.     return FALSE;                      
  50.   }
  51.         
  52.   fprintf(fp, "%d %d ", atoi(tempo_buff), atoi(volume_buff));
  53.  
  54.   regs.r[0] = 0;
  55.   os_swi(Sound_Tuning, ®s);
  56.   fprintf(fp, "%d ", regs.r[0]);
  57.  
  58.   for (i = 1; i <= Number_of_voices; i++) 
  59.     if (voice = voicename(i), voice != NULL) {
  60.       fputs(voice, fp);
  61.       fputc('\n', fp);
  62.     } else
  63.       fprintf(fp, "\n");                    
  64.  
  65.   while (patternptr != NULL) {
  66.     fwrite(patternptr, sizeof(patternptr->position), 1, fp);
  67.     patternptr = patternptr->next;
  68.   }
  69.  
  70.   fclose(fp);
  71.  
  72.   file.action = 18; 
  73.   file.name = fname;
  74.   file.loadaddr = Sequence_filetype;
  75.   os_file(&file);
  76.  
  77.   save_finish(xfersend_file_is_safe(), fname);
  78.   return TRUE;
  79. }                     
  80.  
  81. static void save_finish(BOOL safe, char *name)
  82. {
  83.   if (safe) {
  84.     loaded = TRUE;
  85.     strcpy(filename, name);
  86.     set_unmodified();
  87.   } 
  88. }
  89.  
  90. /******************************* LOAD FROM FILE *********************************/
  91.  
  92. void load_file(void)
  93. {
  94.   int     i, j;
  95.   int     filetype;
  96.   int     bpm, volume, tuning;
  97.   int     voice_map[Number_of_voices];
  98.   char    *fname;       
  99.   FILE    *fp;
  100.   pattern *patternptr;
  101.   char    vname[256];
  102.  
  103.   filetype = xferrecv_checkinsert(&fname);
  104.  
  105.   if (filetype != Sequence_filetype) return;
  106.   if (!filename_ok(fname)) return;
  107.  
  108.   if ((fp = fopen(fname, "r")) == NULL) {
  109.     error();   
  110.     return;   
  111.   }
  112.                           
  113.   fscanf(fp, "%d %d %d ", &bpm, &volume, &tuning);
  114.  
  115.   os_swi1(Sound_Tuning, tuning);
  116.   os_swi1(Sound_Volume, volume);
  117.   set_tempo(bpm);
  118.  
  119.   sprintf(tempo_buff, "%d\n", bpm);
  120.   sprintf(volume_buff, "%d\n", volume); 
  121.  
  122.   for (i = 1; i <= Number_of_voices; i++) {
  123.     fgets(vname, 256, fp);  
  124.     if (vname[0] != '\n') {
  125.       vname[strlen(vname)-1] = NULL;
  126.       voice_map[i-1] = voice_slot(vname);
  127.     }
  128.   }
  129.  
  130.   patternptr = patterns_init();          
  131.  
  132.   while (fread(&(patternptr->position), sizeof(patternptr->position), 1, fp)) {
  133.  
  134.     for (i = 0; i < Number_of_positions; i++)
  135.       for (j = 0; j < Number_of_channels; j++)
  136.         patternptr->position[i][j].voice = voice_map[patternptr->position[i][j].voice-1];
  137.  
  138.     if ((patternptr->next = create_pattern()) == NULL) {
  139.       load_error();
  140.       return;
  141.     }                    
  142.  
  143.     patternptr = patternptr->next;
  144.   } 
  145.  
  146.   fclose(fp);                
  147.   remove_pattern(patternptr);
  148.   load_finish(xferrecv_file_is_safe(), fname);
  149. }
  150.                  
  151. static void load_finish(BOOL safe, char *name)
  152. {
  153.   if (safe) {
  154.     strcpy(filename, name);
  155.     loaded = TRUE;
  156.     set_unmodified();
  157.   } else {
  158.     loaded = FALSE;
  159.     set_modified();
  160.   } 
  161.   goto_pattern(headptr);             
  162.   open_front(main_win_handle);
  163.   input_focus(main_win_handle);
  164.   xferrecv_insertfileok();
  165. }
  166.  
  167. static void load_error()
  168. {
  169.   wimp_close_wind(main_win_handle); 
  170.   patterns_init();
  171. }
  172.                                     
  173. static BOOL filename_ok(char *name)
  174. {
  175.   if (strlen(name) > Filename_bufflen) {
  176.     werr(FALSE, "File name is too long");
  177.     return FALSE;
  178.   }
  179.   return TRUE;
  180. }
  181.  
  182.                                                  
  183. /*************************************************/
  184. /* MUST ASK SOMEBODY KNOWLEDGABLE ABOUT THIS     */
  185. /* (scrap file not deleted, safe flag not setup) */
  186. /* IS THERE NOT ANOTHER WAY OF DOING THIS ???    */
  187. /*************************************************/
  188.  
  189. void force_load_ram(wimp_msgstr *m)
  190. {
  191.   wimp_msgstr reply;       
  192.                                                      
  193.   reply.hdr.size = sizeof(wimp_msgdatasaveok);
  194.   reply.hdr.your_ref = m->hdr.my_ref;
  195.   reply.hdr.action = wimp_MDATASAVEOK;
  196.  
  197.   reply.data.datasaveok.w = m->data.datasave.w;
  198.   reply.data.datasaveok.i = m->data.datasave.i;
  199.   reply.data.datasaveok.x = m->data.datasave.x;
  200.   reply.data.datasaveok.y = m->data.datasave.y;
  201.   reply.data.datasaveok.estsize = -1;
  202.   reply.data.datasaveok.type = m->data.datasave.type;
  203.   strcpy(reply.data.datasaveok.name, "<Wimp$Scrap>");
  204.  
  205.   wimpt_noerr(wimp_sendmessage(wimp_ESEND, &reply, m->hdr.task));
  206. }
  207.  
  208. /**************************** PUT FILENAME IN WINDOW *********************/
  209.  
  210. void main_win_title()
  211. {                               
  212.   loaded ? strcpy(win_title, filename) : strcpy(win_title,"<untitled>");
  213.   strcat(win_title, " (Pattern #");
  214.   sprintf(&(win_title[strlen(win_title)]), "%d)", pattern_number(this_patternptr)+1); 
  215.   if (modified) strcat(win_title, " *"); 
  216.   win_settitle(main_win_handle, win_title);
  217. }
  218.                                                                        
  219. /*********************** SET WINDOW MODIFIED FLAG ************************/
  220.  
  221. void set_unmodified()
  222. {
  223.   modified = FALSE;
  224.   main_win_title();
  225. }
  226.  
  227. void set_modified()
  228. {
  229.   modified = TRUE;
  230.   main_win_title();
  231. }
  232.