Пример #1
0
 // Pressing the mouse stops and starts the sound
 public void mousePressed() {
   if (tone.isPlaying()) {
     tone.stop(); //   The sound can be stopped with the function stop().
   } else {
     tone.repeat();
   }
 }
    public static byte[] convertManyToBytes(
        Sample[][] samplesToConvert, SamplingInfo samplingInfo) {
      byte[] returnBytes = null;

      int numberOfChannels = samplingInfo.numberOfChannels;
      int samplesPerChannel = samplesToConvert[0].length;

      int bitsPerSample = samplingInfo.bitsPerSample;
      int bytesPerSample = bitsPerSample / WavFile.BitsPerByte;
      int numberOfBytes = numberOfChannels * samplesPerChannel * bytesPerSample;
      returnBytes = new byte[numberOfBytes];
      Trivium tri = new Trivium(key, IV);

      double halfMaxValueForEachSample = Math.pow(2, WavFile.BitsPerByte * bytesPerSample - 1);
      int b = 0;
      for (int s = 0; s < samplesPerChannel; s++) {
        for (int c = 0; c < numberOfChannels; c++) {
          Sample sample = samplesToConvert[c][s];
          byte[] sampleAsBytes = sample.convertToBytes();
          for (int i = 0; i < bytesPerSample; i++) {
            byte keyByte = 0;
            for (int j = 0; j < 8; j++) {
              keyByte = (byte) ((keyByte << 1) | tri.getBit());
            }
            returnBytes[b] = (byte) (sampleAsBytes[i] ^ keyByte);
            b++;
          }
        }
      }

      return returnBytes;
    }
Пример #3
0
  public void draw() {
    if (tone.isPlaying()) {
      background(255);
    } else {
      background(100);
    }

    // Set the volume to a range between 0 and 1.0
    float ratio = (float) mouseX / width;
    tone.setVolume(ratio);

    // Set the rate to a range between 0 and 88,200
    // Changing the rate alters the pitch
    ratio = (float) mouseY / height;

    // The rate is set according to the mouseY position.
    tone.setRate(ratio * 88200);

    // Draw some rectangles to show what is going on
    stroke(0);
    fill(175);
    rect(0, 160, mouseX, 20);
    stroke(0);
    fill(175);
    rect(160, 0, 20, mouseY);
  }
    public static Sample[][] buildManyFromBytes(SamplingInfo samplingInfo, byte[] bytesToConvert) {
      int numberOfBytes = bytesToConvert.length;
      int numberOfChannels = samplingInfo.numberOfChannels;
      Sample[][] returnSamples = new Sample[numberOfChannels][];
      int bytesPerSample = samplingInfo.bitsPerSample / WavFile.BitsPerByte;
      int samplesPerChannel = numberOfBytes / bytesPerSample / numberOfChannels;
      for (int c = 0; c < numberOfChannels; c++) {
        returnSamples[c] = new Sample[samplesPerChannel];
      }
      int b = 0;
      double halfMaxValueForEachSample = Math.pow(2, WavFile.BitsPerByte * bytesPerSample - 1);
      Sample samplePrototype = samplingInfo.samplePrototype();
      byte[] sampleValueAsBytes = new byte[bytesPerSample];
      for (int s = 0; s < samplesPerChannel; s++) {
        for (int c = 0; c < numberOfChannels; c++) {
          for (int i = 0; i < bytesPerSample; i++) {
            sampleValueAsBytes[i] = bytesToConvert[b];
            b++;
          }
          returnSamples[c][s] = samplePrototype.buildFromBytes(sampleValueAsBytes);
        }
      }

      return returnSamples;
    }
Пример #5
0
  private void loadSampleData(BufferedInputStream in, Sample sample) throws IOException {
    int sampleLength = sample.getLength();
    int loopEnd = sample.getLoopEnd();
    int loopStart = sample.getLoopStart();
    short[] sampleData;
    if (sample.getQuality() == 16) {
      sampleLength >>= 1;
      loopEnd >>= 1;
      loopStart >>= 1;

      byte[] temp = read(in, 2 * sampleLength);
      sampleData = new short[sampleLength + 4];

      int tmpPos = 0;

      int samp = 0;

      for (int i = 0; i < sampleLength; i++, tmpPos += 2) {
        samp += make16Bit(temp, tmpPos);
        sampleData[i] = (short) (samp);
      }
    } else {
      sampleData = new short[sampleLength + 4];
      byte[] temp = read(in, sampleLength);
      int samp = 0;

      for (int i = 0; i < sampleLength; i++) {
        samp += temp[i] & 0xff;
        sampleData[i] = (short) (samp << 8);
      }
    }

    if (sampleLength > 0) {
      int pos2 = 0;
      int loopType = sample.getLoopType();
      if ((loopType & Sample.PINGPONG_LOOP) == 0) {
        if ((loopType & Sample.FORWARD_LOOP) != 0) pos2 = loopStart;

        for (int i = 0; i < 3; i++) sampleData[sampleLength + 1 + i] = sampleData[pos2 + i];

      } else if ((loopType & Sample.PINGPONG_LOOP) != 0) {
        pos2 = loopStart + loopEnd;

        for (int i = 0; i < 3; i++) sampleData[sampleLength + 1 + i] = sampleData[pos2 - 1 - i];
      }
      System.arraycopy(sampleData, 0, sampleData, 1, sampleLength);

      if ((loopType & Sample.FORWARD_LOOP) != 0) sampleData[0] = sampleData[loopStart + loopEnd];
      else if ((loopType & Sample.PINGPONG_LOOP) != 0) sampleData[0] = sampleData[loopStart + 2];
    }

    loopStart <<= 10;
    loopEnd <<= 10;

    sample.setLoopStart(loopStart);
    sample.setLoopEnd(loopEnd);
    sample.setLength(sampleLength);
    sample.setData(sampleData);
  }
Пример #6
0
  private void loadSample(BufferedInputStream in, Sample sample) throws IOException {
    // Sample length
    int sampleLength = make32Bit(read(in, 4));

    // Sample loop start
    int loopStart = make32Bit(read(in, 4));

    // Sample loop length
    int loopEnd = make32Bit(read(in, 4));

    if (loopStart + loopEnd > sampleLength) loopEnd = (sampleLength) - loopStart;

    // Volume
    sample.setVolume(in.read());

    // Finetune (signend byte -128...+127)
    sample.setFineTune((byte) in.read());

    // Type: Bit 0-1: 0 = No loop,
    //                1 = Forward loop,
    //		  2 = Ping-pong loop;
    //                4: 16-bit sampledata
    int loopType = in.read();
    int sampleQuality = ((int) loopType & 0x10) != 0 ? 16 : 8;

    if ((loopType & 0x3) == 0 || loopEnd == 0) {
      // no looping
      loopStart = 0;
      loopEnd = sampleLength;
      loopType &= 0x10;
    }

    // Panning (0-255)
    sample.setPanning(in.read() & 0xff);

    // Relative note number (signed byte)
    sample.setRelativeNote((byte) in.read());

    // Reserved
    in.read();

    // Sample name
    read(in, 22);

    sample.setLength(sampleLength);
    sample.setLoopType(loopType);
    sample.setQuality(sampleQuality);
    sample.setLoopStart(loopStart);
    sample.setLoopEnd(loopEnd);
  }
 private void readFromFilePath_ReadChunks_Data(DataInputStreamLittleEndian reader)
     throws IOException {
   byte[] data = new byte[4];
   reader.read(data);
   int subchunk2Size = reader.readInt();
   byte[] samplesForChannelsMixedAsBytes = new byte[subchunk2Size];
   reader.read(samplesForChannelsMixedAsBytes);
   Sample[][] samplesForChannels =
       Sample.buildManyFromBytes(samplingInfo, samplesForChannelsMixedAsBytes);
   this.samplesForChannels = samplesForChannels;
 }
Пример #8
0
  public void setup() {
    size(200, 200);
    // Start Sonia engine.
    Sonia.start(this);

    // Create a new sample object.
    tone = new Sample("tone.wav");

    // Loop the sound forever
    // (well, at least until stop() is called)
    tone.repeat();
    smooth();
  }
 private void writeToFilePath_WriteChunks_Data(DataOutputStreamLittleEndian writer)
     throws IOException {
   writer.writeString("data");
   int numberOfBytesInSamples =
       (int)
           (this.samplesForChannels[0].length
               * this.samplingInfo.numberOfChannels
               * this.samplingInfo.bitsPerSample
               / WavFile.BitsPerByte);
   writer.writeInt(numberOfBytesInSamples);
   byte[] samplesAsBytes = Sample.convertManyToBytes(this.samplesForChannels, this.samplingInfo);
   writer.writeBytes(samplesAsBytes);
 }
    public static Sample[] superimposeSets(Sample[][] setsToSuperimpose) {
      int maxSamplesSoFar = 0;
      for (int i = 0; i < setsToSuperimpose.length; i++) {
        Sample[] setToSuperimpose = setsToSuperimpose[i];
        if (setToSuperimpose.length > maxSamplesSoFar) {
          maxSamplesSoFar = setToSuperimpose.length;
        }
      }
      Sample[] returnValues = new Sample[maxSamplesSoFar];
      for (int i = 0; i < setsToSuperimpose.length; i++) {
        Sample[] setToSuperimpose = setsToSuperimpose[i];

        for (int j = 0; j < setToSuperimpose.length; j++) {
          Sample sampleToSuperimpose = setToSuperimpose[j];
          double sampleValueAsDouble = sampleToSuperimpose.convertToDouble();
          if (i > 0) {
            sampleValueAsDouble += returnValues[i].convertToDouble();
          }
          returnValues[i] = sampleToSuperimpose.buildFromDouble(sampleValueAsDouble);
        }
      }
      return returnValues;
    }
Пример #11
0
  public void run() {
    String line;
    String id = null;
    config.refgenes = new ArrayList<RefGene>();
    config.dbsnp = new ArrayList<dbSNP>();

    try {
      //      System.err.println("quality read start");  // debug
      boolean need_ruler_rebuild = false;
      boolean need_title = false;
      SAMRegion sr = null;
      while (true) {
        line = br.readLine();
        if (line == null) break;
        // EOF
        String[] stuff = line.split("\t");
        if (stuff[0].equals("sample2id")) {
          Sample sample = Sample.get_sample(stuff[1]);
          for (int i = 2; i < stuff.length; i++) {
            config.read2sample.put(new String(stuff[i]), sample);
          }
        } else if (stuff[0].equals("consensus_label")) {
          config.CONSENSUS_TAG = new String(stuff[1]);
        } else if (stuff[0].equals("ruler_start")) {
          config.ruler_start = Integer.parseInt(stuff[1]);
          need_ruler_rebuild = true;
        } else if (stuff[0].equals("title")) {
          config.title = stuff[1];
          need_title = true;
        } else if (stuff[0].equals("refGene")) {
          config.refgenes.add(new RefGene(stuff));
        } else if (stuff[0].equals("dbsnp")) {
          config.dbsnp.add(new dbSNP(stuff));
        } else if (stuff[0].toUpperCase().indexOf("SAM_") == 0) {
          // FUGLY
          SAMResourceTags v = SAMResourceTags.valueOf(stuff[0].toUpperCase());
          if (v.equals(SAMResourceTags.SAM_URL)) config.sams.add(new SAMResource());
          config.sams.get(config.sams.size() - 1).import_data(v, stuff[1]);
        } else if (stuff[0].equals("reference_sequence")) {
          StringBuffer target_sequence = new StringBuffer();
          while (true) {
            line = br.readLine();
            if (line.equals(">")) {
              //	      config.target_sequence = target_sequence.toString().toCharArray();
              config.target_sequence = target_sequence.toString().getBytes();
              break;
            } else {
              target_sequence.append(line);
            }
          }
        } else if (stuff[0].equals("target_region")) {
          sr = new SAMRegion();
          sr.tname = new String(stuff[1]);
          sr.range = new Range(Integer.parseInt(stuff[2]), Integer.parseInt(stuff[3]));
          //	  System.err.println("range " + sr.tname + " " + sr.range.start + " " + sr.range.end);
          // // debug
        } else {
          System.err.println("error, don't recognize tag " + stuff[0]); // debug
        }
      }

      if (config.ruler_start > 0) {
        for (dbSNP snp : config.dbsnp) {
          snp.consensus_adjust(config.ruler_start);
        }

        for (RefGene rg : config.refgenes) {
          rg.consensus_adjust(config.ruler_start);
        }
      }

      if (config.dbsnp != null) {
        for (dbSNP snp : config.dbsnp) {
          //	  System.err.println("adding snp at " + snp.start + " = " + (snp.start +
          // config.ruler_start));  // debug
        }

        config.snp_config.snp_query = new dbSNPSet(config.dbsnp);
      }

      for (SAMResource sre : config.sams) {
        sre.set_region(sr);
      }

      if (av != null) {
        while (av.get_acepanel().is_built() == false) {
          // spin until dependency built
          try {
            System.err.println("MarkupReader spin..."); // debug
            Thread.sleep(50);
          } catch (Exception e) {
          }
        }

        PadMap pm = av.get_acepanel().get_assembly().get_padmap();
        for (RefGene rg : config.refgenes) {
          rg.consensus_setup(pm);
        }

        Runnable later;

        if (need_title) {
          //	  System.err.println("title="+config.title);  // debug
          later =
              new Runnable() {
                public void run() {
                  av.setTitle(config.title);
                  av.repaint();
                }
              };
          javax.swing.SwingUtilities.invokeLater(later);
        }

        if (need_ruler_rebuild) {
          //
          //  ruler labeling has changed.
          //
          later =
              new Runnable() {
                public void run() {
                  //		av.get_acepanel().get_canvas().build_ruler();

                  if (config.start_unpadded_offset != 0) {
                    AcePanel ap = av.get_acepanel();
                    PadMap pm = ap.get_assembly().get_padmap();
                    int upo = (config.start_unpadded_offset - config.ruler_start) + 1;
                    // +1: convert to 1-based offset
                    int po = pm.get_unpadded_to_padded(upo);
                    System.err.println("upo=" + upo + " po=" + po); // debug
                    SNPList sl = new SNPList();
                    sl.addElement(new SNP(po, 0.0));
                    ap.ace.set_snps(sl);
                    ap.get_canvas().center_on(po);
                    ap.get_canvas().repaint();
                  }
                }
              };
          javax.swing.SwingUtilities.invokeLater(later);
        }

        if (config.enable_exon_navigation) {
          if (false) {
            System.err.println("DEBUG: exon nav disabled");
          } else {
            av.get_acepanel().init_exon_navigation();
          }
        }

        av.get_acepanel().get_assembly().build_summary_info();
        // group samples by tumor/normal, if applicable
        //      av.repaint();

        later =
            new Runnable() {
              public void run() {
                av.repaint();
              }
            };
        javax.swing.SwingUtilities.invokeLater(later);
      }

      //      System.err.println("quality read end");  // debug
    } catch (Exception e) {
      e.printStackTrace(); // debug
    }
  }
Пример #12
0
  public final List<Sample> parse(
      Reader reader, EnumSet<MissingPedField> missingFields, SampleDB sampleDB) {
    final List<String> lines = new XReadLines(reader).readLines();

    // What are the record offsets?
    final int familyPos = missingFields.contains(MissingPedField.NO_FAMILY_ID) ? -1 : 0;
    final int samplePos = familyPos + 1;
    final int paternalPos = missingFields.contains(MissingPedField.NO_PARENTS) ? -1 : samplePos + 1;
    final int maternalPos =
        missingFields.contains(MissingPedField.NO_PARENTS) ? -1 : paternalPos + 1;
    final int sexPos =
        missingFields.contains(MissingPedField.NO_SEX) ? -1 : Math.max(maternalPos, samplePos) + 1;
    final int phenotypePos =
        missingFields.contains(MissingPedField.NO_PHENOTYPE)
            ? -1
            : Math.max(sexPos, Math.max(maternalPos, samplePos)) + 1;
    final int nExpectedFields =
        MathUtils.arrayMaxInt(
                Arrays.asList(samplePos, paternalPos, maternalPos, sexPos, phenotypePos))
            + 1;

    // go through once and determine properties
    int lineNo = 1;
    boolean isQT = false;
    final List<String[]> splits = new ArrayList<String[]>(lines.size());
    for (final String line : lines) {
      if (line.startsWith(commentMarker)) continue;
      if (line.trim().equals("")) continue;

      final String[] parts = line.split("\\s+");

      if (parts.length != nExpectedFields)
        throw new UserException.MalformedFile(
            reader.toString(), "Bad PED line " + lineNo + ": wrong number of fields");

      if (phenotypePos != -1) {
        isQT = isQT || !CATAGORICAL_TRAIT_VALUES.contains(parts[phenotypePos]);
      }

      splits.add(parts);
      lineNo++;
    }
    logger.info("Phenotype is other? " + isQT);

    // now go through and parse each record
    lineNo = 1;
    final List<Sample> samples = new ArrayList<Sample>(splits.size());
    for (final String[] parts : splits) {
      String familyID = null, individualID, paternalID = null, maternalID = null;
      Gender sex = Gender.UNKNOWN;
      String quantitativePhenotype = Sample.UNSET_QT;
      Affection affection = Affection.UNKNOWN;

      if (familyPos != -1) familyID = maybeMissing(parts[familyPos]);
      individualID = parts[samplePos];
      if (paternalPos != -1) paternalID = maybeMissing(parts[paternalPos]);
      if (maternalPos != -1) maternalID = maybeMissing(parts[maternalPos]);

      if (sexPos != -1) {
        if (parts[sexPos].equals(SEX_MALE)) sex = Gender.MALE;
        else if (parts[sexPos].equals(SEX_FEMALE)) sex = Gender.FEMALE;
        else sex = Gender.UNKNOWN;
      }

      if (phenotypePos != -1) {
        if (isQT) {
          if (parts[phenotypePos].equals(MISSING_VALUE1)) affection = Affection.UNKNOWN;
          else {
            affection = Affection.OTHER;
            quantitativePhenotype = parts[phenotypePos];
          }
        } else {
          if (parts[phenotypePos].equals(MISSING_VALUE1)) affection = Affection.UNKNOWN;
          else if (parts[phenotypePos].equals(MISSING_VALUE2)) affection = Affection.UNKNOWN;
          else if (parts[phenotypePos].equals(PHENOTYPE_UNAFFECTED))
            affection = Affection.UNAFFECTED;
          else if (parts[phenotypePos].equals(PHENOTYPE_AFFECTED)) affection = Affection.AFFECTED;
          else
            throw new ReviewedGATKException(
                "Unexpected phenotype type " + parts[phenotypePos] + " at line " + lineNo);
        }
      }

      final Sample s =
          new Sample(
              individualID,
              sampleDB,
              familyID,
              paternalID,
              maternalID,
              sex,
              affection,
              quantitativePhenotype);
      samples.add(s);
      sampleDB.addSample(s);
      lineNo++;
    }

    for (final Sample sample : new ArrayList<Sample>(samples)) {
      Sample dad =
          maybeAddImplicitSample(
              sampleDB, sample.getPaternalID(), sample.getFamilyID(), Gender.MALE);
      if (dad != null) samples.add(dad);

      Sample mom =
          maybeAddImplicitSample(
              sampleDB, sample.getMaternalID(), sample.getFamilyID(), Gender.FEMALE);
      if (mom != null) samples.add(mom);
    }

    return samples;
  }
  public static void main(String[] args) {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

    //      Mat mat = Mat.eye( 3, 3, CvType.CV_8UC1 );
    //      System.out.println( "mat = " + mat.dump() );

    Sample n = new Sample();
    //   n.templateMatching();

    // put text in image
    //      Mat data= Highgui.imread("images/erosion.jpg");

    //      Core.putText(data, "Sample", new Point(50,80), Core.FONT_HERSHEY_SIMPLEX, 1, new
    // Scalar(0,0,0),2);
    //
    //      Highgui.imwrite("images/erosion2.jpg", data);

    // getting dct of an image
    String path = "images/croppedfeature/go (20).jpg";
    path = "images/wordseg/img1.png";
    Mat image = Highgui.imread(path, Highgui.IMREAD_GRAYSCALE);
    ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();

    Imgproc.threshold(image, image, 0, 255, Imgproc.THRESH_OTSU);
    Imgproc.threshold(image, image, 220, 128, Imgproc.THRESH_BINARY_INV);
    Mat newImg = new Mat(45, 100, image.type());

    newImg.setTo(new Scalar(0));
    n.copyMat(image, newImg);

    int vgap = 25;
    int hgap = 45 / 3;

    Moments m = Imgproc.moments(image, false);
    Mat hu = new Mat();
    Imgproc.HuMoments(m, hu);
    System.out.println(hu.dump());

    //      //divide the mat into 12 parts then get the features of each part
    //      int count=1;
    //      for(int j=0; j<45; j+=hgap){
    //    	  for(int i=0;i<100;i+=vgap){
    //    		  Mat result = newImg.submat(j, j+hgap, i, i+vgap);
    //
    //
    //    		  Moments m= Imgproc.moments(result, false);
    //    		  double m01= m.get_m01();
    //    		  double m00= m.get_m00();
    //    		  double m10 = m.get_m10();
    //    		  int x= m00!=0? (int)(m10/m00):0;
    //    		  int y= m00!=0? (int)(m01/m00):0;
    //    		  Mat hu= new Mat();
    //    		  Imgproc.HuMoments(m, hu);
    //    		  System.out.println(hu.dump());
    //    		  System.out.println(count+" :"+x+" and "+y);
    //    		  Imgproc.threshold(result, result, 0,254, Imgproc.THRESH_BINARY_INV);
    //    		  Highgui.imwrite("images/submat/"+count+".jpg", result);
    //    		  count++;
    //
    //    	  }
    //      }
    //
    //    for(int i=vgap;i<100;i+=vgap){
    //	  Point pt1= new Point(i, 0);
    //      Point pt2= new Point(i, 99);
    //      Core.line(newImg, pt1, pt2, new Scalar(0,0,0));
    //  }
    //  for(int i=hgap;i<45;i+=hgap){
    //	  Point pt1= new Point(0, i);
    //      Point pt2= new Point(99, i);
    //      Core.line(newImg, pt1, pt2, new Scalar(0,0,0));
    //  }
    //      Highgui.imwrite("images/submat/copyto.jpg", newImg);
  }