Пример #1
0
/* Process wave file, write music probability and labels into the specified files. Return 0 on success, print error and return non-zero on error. */
int process(const char* infile,
            WAVE* wave,
            OpusSM* sm,
            FILE* ofp_pmusic,
            FILE* ofp_labels,
            double sm_segment_min_dur,
            double b_segment_min_dur
           )
{
	double frame_dur = (double)ANALYSIS_FRAME_SIZE/wave->header.SampleRate;
	Labeler* lb = lb_init(sm_segment_min_dur/frame_dur, b_segment_min_dur/frame_dur);

	float*   analysis_pcm = malloc(ANALYSIS_FRAME_SIZE*wave->header.NumChannels*sizeof(float));
	int16_t* buffer       = malloc(ANALYSIS_FRAME_SIZE*wave->header.NumChannels*sizeof(int16_t));
	double total_music_ratio = 0;
	int error = 0;
	for (int ii = 0; ii <= wave->size - ANALYSIS_FRAME_SIZE; ii = ii + ANALYSIS_FRAME_SIZE) {
		int readcount = wread(buffer, ANALYSIS_FRAME_SIZE, wave);
		
		error = (readcount != ANALYSIS_FRAME_SIZE);
		
		if (error) {
			fprintf(stderr, "Could not read from wave file \"%s\", read count %d.\n", infile, readcount);
			break;
		}

		int2float(buffer, analysis_pcm, ANALYSIS_FRAME_SIZE, wave->header.NumChannels);
		float pmusic = sm_pmusic(sm, analysis_pcm);
		total_music_ratio += pmusic;

		if (ofp_labels != NULL) {
			lb_add_frame(lb, pmusic);
		}

		fprintf(ofp_pmusic, "%f %f\n", (double)ii / wave->header.SampleRate, pmusic);

	}

	if (!error) {
		if (ofp_labels != NULL) {
			lb_finalize(lb);
			lb_print_to_file(lb, ofp_labels, frame_dur);
		}
		total_music_ratio = (total_music_ratio * (double)ANALYSIS_FRAME_SIZE) / (double) wave->size;
		fprintf(stderr, "Music ratio: %f\n", total_music_ratio);
	}


	lb = lb_destroy(lb);
	free(analysis_pcm);
	free(buffer);

	return error;
}
Пример #2
0
static gint32
load_image ( gchar *filename)
{
  gchar  *name;
  gint32  image_ID;
  GImageType    image_type = 0;
  GDrawableType layer_type = 0;
  GimpDrawable *drawable = NULL;

  FILE   *fp = NULL;
  int     fsize = 0;
  size_t  fpos = 0;
  unsigned char *data = 0;
  size_t  mem_n = 0;  /* needed memory in bytes */

  int info_good = 1;

  int type = 0;       /* PNM type */
  int width = 0;
  int height = 0;
  int spp = 0;        /* samples per pixel */
  int byteps = 1;        /* byte per sample */
  double maxval = 0;

  size_t start, end;

# ifdef DEBUG
  printf("%s:%d %s() 0x%x\n",__FILE__,__LINE__,__func__,(intptr_t)filename);
#endif

  name = g_strdup_printf( _("Opening '%s'..."), filename);
  gimp_progress_init (name);
  m_free (name)


  fp = fopen (filename, "rm");
  if (!fp) {
    g_message (_("Could not open '%s' for reading: %s"),
                 filename, g_strerror (errno));
    return FALSE;
  }
  fseek(fp,0L,SEEK_END);
  fsize = ftell(fp);
  rewind(fp);

  m_new (data, char, fsize, return FALSE)

  fpos = fread( data, sizeof(char), fsize, fp );
  if( fpos < fsize ) {
    g_message (_("Could not read '%s' : %s %d/%d"),
                 filename, g_strerror (errno), fsize,(int)fpos);
    m_free( data )
    fclose (fp);
    return FALSE;
  }
  fpos = 0;
  fclose (fp);
  fp = NULL;


  /* parse Infos */
  if(data[fpos] == 'P')
  {
    if(isdigit(data[++fpos])) {
      char tmp[2] = {0, 0};
      tmp[0] = data[fpos];
      type = atoi(tmp);
    } else
    if (!isspace(data[fpos]))
    {
      if(data[fpos] == 'F') /* PFM rgb */
        type = -6;
      else if (data[fpos] == 'f') /* PFM gray */
        type = -5;
      else
        info_good = 0;
    }
    else
      info_good = 0;
  }
  fpos++;

  /* parse variables */
  {
    int in_c = 0;    /* within comment */
    int v_read = 0;  /* number of variables allready read */
    int v_need = 3;  /* number of needed variable; start with three */
    int l_end = 0;   /* line end position */
    int l_pos = 0;   /* line position */
    int l_rdg = 1;   /* line reading */

    if(type == 1 || type == 4)
           v_need = 2;
    if(type == 7) /* pam  */
           v_need = 12;

    while(v_read < v_need && info_good)
    {
      l_pos = l_end = fpos;
      l_rdg = 1;

      /* read line */
      while(fpos < fsize && l_rdg)
      {
        if(data[fpos] == '#') {
          in_c = 1;
          l_end = fpos-1;
        } else if(data[fpos] == 10 || data[fpos] == 13) { /* line break */
          l_rdg = 0;
        } else if(data[fpos] != 0) {
          if(!in_c)
            ++l_end;
        } else {
          l_rdg = 0;
        }
        if(!l_rdg) {
          in_c = 0;
        }
        ++fpos;
      }

      /* parse line */
      while(info_good &&
            v_read < v_need &&
            l_pos < l_end)
      {
        if( info_good )
        {
          double var = -2;
          char var_s[64];
          int l = 0;
          wread ( data, l_pos, l_end, &start, &end );
          l = end - start;
          if ( l < 63 ) {
            memcpy(var_s, &data[start], l);
            var_s[l] = 0;
            var = atof(var_s);
#           ifdef DEBUG
            printf("var = \"%s\"  %d\n",var_s, l);
#           endif
          }
          l_pos = end + 1;
          if(type == 7)
          {
            if(height == -1)
              height = (int)var;
            if(width == -1)
              width = (int)var;
            if(spp == -1)
              spp = (int)var;
            if(maxval == -0.5)
              maxval = var;

            if(strcmp(var_s, "HEIGHT") == 0)
              height = -1; /* expecting the next token is the val */
            if(strcmp(var_s, "WIDTH") == 0)
              width = -1;
            if(strcmp(var_s, "DEPTH") == 0)
              spp = -1;
            if(strcmp(var_s, "MAXVAL") == 0)
              maxval = -0.5;
            if(strcmp(var_s, "TUPLTYPE") == 0)
              ; /* ?? */
            if(strcmp(var_s, "ENDHDR") == 0)
              v_need = v_read;
          }
          else
          {
            if (!var)
              info_good = 0;
            if(v_read == 0)
              width = (int)var;
            else if(v_read == 1)
              height = (int)var;
            else if(v_read == 2)
              maxval = var;
          }

          ++v_read;

        }
      }
    }
  }

  if(strstr(strrchr(filename, '.')+1, "raw"))
  {
    info_good = 1;
    width = atoi(getenv("RAW_WIDTH"));
    height = atoi(getenv("RAW_HEIGHT"));
    type = atoi(getenv("RAW_TYPE"));
    fpos = 0;
    maxval = atoi(getenv("RAW_MAXVAL"));
  }


  if(info_good)
    switch(type) {
      case 1:
      case 4:
           image_type = GIMP_GRAY;
           layer_type = GRAY_IMAGE;
           spp = 1;
           info_good = 0;
           break;
      case 2:
      case 5:
           if(maxval <= 255) {
             image_type = GIMP_GRAY;
             layer_type = GRAY_IMAGE;
             byteps        = 1;
           } else if (maxval <= 65535) {
             image_type = U16_GRAY;
             layer_type = U16_GRAY_IMAGE;
             byteps        = 2;
           }
           spp = 1;
           break;
      case 3:
      case 6:
           if(maxval <= 255) {
             image_type = GIMP_RGB;
             layer_type = RGB_IMAGE;
             byteps        = 1;
           } else if (maxval <= 65535) {
             image_type = U16_RGB;
             layer_type = U16_RGB_IMAGE;
             byteps        = 2;
           }
           spp = 3;
           break;
      case -5:
           image_type = FLOAT_GRAY;
           layer_type = FLOAT_GRAY_IMAGE;
           byteps = 4;
           spp = 1;
           break;
      case -6:
           byteps = 4;
           spp = 3;
           image_type = FLOAT_RGB;
           layer_type = FLOAT_RGB_IMAGE;
           break;
      case 7: /* pam */
           if (maxval == 1.0 || maxval == -1.0) {
             byteps        = 4;
             switch(spp)
             {
               case 1:
                   {
                     image_type = FLOAT_GRAY;
                     layer_type = FLOAT_GRAY_IMAGE;
                   }
                    break;
               case 2:
                   {
                     image_type = FLOAT_GRAY;
                     layer_type = FLOAT_GRAYA_IMAGE;
                   }
                    break;
               case 3:
                   {
                     image_type = FLOAT_RGB;
                     layer_type = FLOAT_RGB_IMAGE;
                   }
                    break;
               case 4:
                   {
                     image_type = FLOAT_RGB;
                     layer_type = FLOAT_RGBA_IMAGE;
                   }
                    break;
             }
           } else if(maxval <= 255) {
                     byteps        = 1;
             switch(spp)
             {
               case 1:
                   {
                     image_type = GIMP_GRAY;
                     layer_type = GRAY_IMAGE;
                   }
                    break;
               case 2:
                   {
                     image_type = GIMP_GRAY;
                     layer_type = GRAYA_IMAGE;
                   }
                    break;
               case 3:
                   {
                     image_type = GIMP_RGB;
                     layer_type = RGB_IMAGE;
                   }
                    break;
               case 4:
                   {
                     image_type = GIMP_RGB;
                     layer_type = RGBA_IMAGE;
                   }
                    break;
             }
           } else if (maxval <= 65535) {
                     byteps        = 2;
             switch(spp)
             {
               case 1:
                   {
                     image_type = U16_GRAY;
                     layer_type = U16_GRAY_IMAGE;
                   }
                    break;
               case 2:
                   {
                     image_type = U16_GRAY;
                     layer_type = U16_GRAYA_IMAGE;
                   }
                    break;
               case 3:
                   {
                     image_type = U16_RGB;
                     layer_type = U16_RGB_IMAGE;
                   }
                    break;
               case 4:
                   {
                     image_type = U16_RGB;
                     layer_type = U16_RGBA_IMAGE;
                   }
                    break;
             }
           }
           break;
      default:
           info_good = 0;
    }

# ifdef DEBUG
  printf("wxh %dx%d  type: %d|%d|%d   bytes/samples %d %d \n",width,height,image_type,layer_type,type,
                                       byteps, spp);
# endif

  if( !info_good )
  {
    g_message ("%s:%d failed to get info of %s",__FILE__,__LINE__,filename);
    m_free( data )
    return FALSE;
  }