⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mm_util.c

📁 在linux下多媒体开发实例linux下多媒体开发
💻 C
字号:
/* * Utility functions for Music Machine application */#include <unistd.h>#include <signal.h>#include <stdio.h>#include <string.h>#include <assert.h>#include <forms.h>#include "soundit.h"#include "mm.h"#include "mm_util.h"/* bitmaps for the buttons */#include "bitmaps/stop.xbm"#include "bitmaps/loop.xbm"#include "bitmaps/rest.xbm"#include "bitmaps/zero.xbm"#include "bitmaps/one.xbm"#include "bitmaps/two.xbm"#include "bitmaps/three.xbm"#include "bitmaps/four.xbm"#include "bitmaps/five.xbm"#include "bitmaps/six.xbm"#include "bitmaps/seven.xbm"#include "bitmaps/eight.xbm"#include "bitmaps/nine.xbm"#include "bitmaps/ten.xbm"#include "bitmaps/eleven.xbm"#include "bitmaps/twelve.xbm"#include "bitmaps/thirteen.xbm"#include "bitmaps/fourteen.xbm"#include "bitmaps/fifteen.xbm"/* array containing sound sample data */Sample snd[NUM_SAMPLES];/* load in sound sample files and initialize SoundIt library */void init_samples(void){  int i, status, rate;  const char *device;  char file[256];  /* stop playing and clear previous sound samples, if any */  Snd_restore();  /*   * free up any memory previously allocated - otherwise we'll have a   * major memory leak   */  for (i = 0 ; i < NUM_SAMPLES ; i++)    if (snd[i].data != 0) {      free(snd[i].data);      snd[i].data = 0;    }    /* load each sample file */  for (i = 0 ; i < NUM_SAMPLES ; i++) {    /* check for empty filename */    if (!strcmp(fl_get_input(sample_file[i]), "")) {      fl_show_alert("Error",		    "One or more sample files are empty.",		    "Enter valid sample file names and try again.",		    0);      return;    }        /* get directory and add filename to it */    strcpy(file, fl_get_input(sample_dir));    strcat(file, "/");    strcat(file, fl_get_input(sample_file[i]));        /* make sure file exists */    if (!file_exists(file)) {      char msg[256];      sprintf(msg, "Sample file \"%s\" does not exist.", file);      fl_show_alert("Warning:",		    msg,		    "Enter a valid sample file name and try again.", 0);      return;    }    status = Snd_loadRawSample(file, &snd[i]);    if (status) {      char msg[256];      sprintf(msg, "Sample file \"%s\" could not be loaded.", file);      fl_show_alert("Error:",		    msg,		    "Check the sample file and try again.",		    0);      return;    }  }  /* get sampling rate and sound device from window */  rate = atoi(fl_get_input(sampling_rate));  device = fl_get_input(audio_device);  /* initialize sound library */  status = Snd_init(NUM_SAMPLES, snd, rate, NUM_CHANNELS, device);  if (status == EXIT_FAILURE)    fl_show_alert("Error",		  "Unable to initialize SoundIt library",		  "Check parameters in options menu and try again", 		  0);}/* update bitmaps for one channel to reflect current page of sound data */void update_bitmap(FL_OBJECT *ob, int i){  switch (i) {  case STOP:    fl_set_bitmapbutton_data(ob, stop_width, stop_height, stop_bits);    break;  case LOOP:    fl_set_bitmapbutton_data(ob, loop_width, loop_height, loop_bits);    break;  case REST:    fl_set_bitmapbutton_data(ob, rest_width, rest_height, rest_bits);    break;  case 1:    fl_set_bitmapbutton_data(ob, one_width, one_height, zero_bits);    break;  case 2:    fl_set_bitmapbutton_data(ob, one_width, one_height, one_bits);    break;  case 3:    fl_set_bitmapbutton_data(ob, two_width, two_height, two_bits);    break;  case 4:    fl_set_bitmapbutton_data(ob, three_width, three_height, three_bits);    break;  case 5:    fl_set_bitmapbutton_data(ob, four_width, four_height, four_bits);    break;  case 6:    fl_set_bitmapbutton_data(ob, five_width, five_height, five_bits);    break;  case 7:    fl_set_bitmapbutton_data(ob, six_width, six_height, six_bits);    break;  case 8:    fl_set_bitmapbutton_data(ob, seven_width, seven_height, seven_bits);    break;  case 9:    fl_set_bitmapbutton_data(ob, eight_width, eight_height, eight_bits);    break;  case 10:    fl_set_bitmapbutton_data(ob, nine_width, nine_height, nine_bits);    break;  case 11:    fl_set_bitmapbutton_data(ob, ten_width, ten_height, ten_bits);    break;  case 12:    fl_set_bitmapbutton_data(ob, eleven_width, eleven_height, eleven_bits);    break;  case 13:    fl_set_bitmapbutton_data(ob, twelve_width, twelve_height, twelve_bits);    break;  case 14:    fl_set_bitmapbutton_data(ob, thirteen_width, thirteen_height, thirteen_bits);    break;  case 15:    fl_set_bitmapbutton_data(ob, fourteen_width, fourteen_height, fourteen_bits);    break;  case 16:    fl_set_bitmapbutton_data(ob, fifteen_width, fifteen_height, fifteen_bits);    break;  default:    assert(0); /* should never happen */    break;  }}/* update all bitmaps to reflect current page of sound data */void update_bitmaps(void){  int i;  int j = SAMPLES_PER_PAGE * (int) fl_get_counter_value(page);  /* optimization: "freeze" the form during updating */  fl_freeze_form(main_window);  /* update all of the note buttons */  for (i = 0 ; i < SAMPLES_PER_PAGE ; i++){    update_bitmap(channel_0[i], notes[0][i+j]);    update_bitmap(channel_1[i], notes[1][i+j]);    update_bitmap(channel_2[i], notes[2][i+j]);    update_bitmap(channel_3[i], notes[3][i+j]);    update_bitmap(channel_4[i], notes[4][i+j]);    update_bitmap(channel_5[i], notes[5][i+j]);    update_bitmap(channel_6[i], notes[6][i+j]);    update_bitmap(channel_7[i], notes[7][i+j]);  }  fl_unfreeze_form(main_window);}/* save song to a file */void save_sample_file(const char *filename){  int i, j;  FILE *fp;  /* open file for write */  fp = fopen(filename, "w");  /* error if can't open */  if (fp == 0) {    char msg[256];    sprintf(msg, "Unable to save song file \"%s\".", filename);    fl_show_alert("Error:",		  msg,		  strerror(errno),		  0);    return;  }  /* write data to file */  fprintf(fp, "Music Machine version 1.0\n");  fprintf(fp, "Audio Device: %s\n", fl_get_input(audio_device));  fprintf(fp, "Sampling Rate: %d\n", atoi(fl_get_input(sampling_rate)));  fprintf(fp, "Sample Directory: %s\n", fl_get_input(sample_dir));  fprintf(fp, "Tempo: %.0f\n", fl_get_slider_value(tempo));  for (i = 0; i < NUM_SAMPLES ; i++)    fprintf(fp, "File%d: %s\n", i, fl_get_input(sample_file[i]));  fprintf(fp, "Sample Data: %d channels %d samples\n",	  NUM_CHANNELS, NUM_PAGES*SAMPLES_PER_PAGE);  for (i = 0; i < NUM_PAGES*SAMPLES_PER_PAGE ; i++)    for (j = 0 ; j < NUM_CHANNELS ; j++)      j == NUM_CHANNELS-1 ?	fprintf(fp, "%d\n", notes[j][i]) 	  : fprintf(fp, "%d ", notes[j][i]);  /* close file */  i = fclose(fp);  /* error if can't close */  if (i != 0) {    char msg[256];    sprintf(msg, "Unable to close song file \"%s\".", filename);    fl_show_alert("Error:",		  msg,		  strerror(errno),		  0);  }}/* load song from a file */void load_sample_file(const char *filename){  int i, st;  float version;  float f;  FILE *fp;  char s1[256], s2[256];  /* open file for read */  fp = fopen(filename, "r");  /* error if can't open */  if (fp == 0) {    char msg[256];    sprintf(msg, "Unable to open song file \"%s\".", filename);    fl_show_alert("Error:",		  msg,		  strerror(errno),		  0);    return;  }  st = fscanf(fp, "Music Machine version %f\n", &version);  if (st != 1) {    fl_show_alert("Error:",		  "This is not a music machine song file.",		  "Check the file and try again.",		  0);    return;  }  if (version != 1.0) {    fl_show_alert("Error:",		  "Incompatible version of music machine song file.",		  "Should be version 1.0",		  0);    return;  }  st = fscanf(fp, "Audio Device: %s\n", s1);  if (st != 1) {    fl_show_alert("Error:",		  "Invalid song file.",		  "Audio device field is bad",		  0);    return;  }  fl_set_input(audio_device, s1);  st = fscanf(fp, "Sampling Rate: %s\n", s1);  if (st != 1) {    fl_show_alert("Error",		  "Invalid song file.",		  "Sampling rate field is bad.",		  0);    return;  }  fl_set_input(sampling_rate, s1);  st = fscanf(fp, "Sample Directory: %s\n", s1);  if (st != 1) {    fl_show_alert("Error:",		  "Invalid song file.",		  "Sample directory section is bad.",		  0);    return;  }  fl_set_input(sample_dir, s1);  st = fscanf(fp, "Tempo: %f\n", &f);  if (st != 1) {    fl_show_alert("Error",		  "Invalid song file.",		  "Tempo field section is bad.",		  0);    return;  }  fl_set_slider_value(tempo, f);  for (i = 0; i < NUM_SAMPLES ; i++) {    sprintf(s2, "File%d: %%s\n", i);    st = fscanf(fp, s2, s1);    if (st != 1) {      fl_show_alert("Error:",		    "Invalid song file.",		    "Sample file section is bad.",		    0);      return;    }    fl_set_input(sample_file[i], s1);  }  st =  fscanf(fp, "Sample Data: %d channels %d samples\n", &i, &i);  if (st != 2) {    fl_show_alert("Error:",		  "Invalid song file.",		  "Sample data header section is bad.",		  0);    return;  }  for (i = 0; i < NUM_PAGES*SAMPLES_PER_PAGE ; i++) {    st = fscanf(fp, "%d %d %d %d %d %d %d %d\n",		&notes[0][i], &notes[1][i], &notes[2][i], &notes[3][i],		&notes[4][i], &notes[5][i], &notes[6][i], &notes[7][i]);    if (st != 8) {      fl_show_alert("Error:",		    "Invalid song file.",		    "Sample data section is bad.",		    0);      return;    }  }  /* close file */  i = fclose(fp);  /* error if can't close */  if (i != 0) {    char msg[256];    sprintf(msg, "Unable to close song file \"%s\".", filename);    fl_show_alert("Error:",		  msg,		  strerror(errno),		  0);  }  /* update the display */  update_bitmaps();  /* load the new sound samples */  init_samples();}/* clear out all rhythm mmory */void clear_samples(void){  int i, j;  for (i = 0 ; i < NUM_CHANNELS ; i++)    for (j = 0 ; j < NUM_PAGES*SAMPLES_PER_PAGE ; j++)      notes[i][j] = REST;}/* *  return whether a file exists (could be a regular file, directory, * *  or even device file) */int file_exists(const char *filename){  FILE *fp = fopen(filename, "r");  if (fp != 0) {    fclose(fp);    return 1;  } else {    return 0;  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -