public void init(Image image) { width = image.Width; height = image.Height; _slices = new ImageSlice[width]; int _numSlices = width; for (int x = 0; x < width; x++) { ImageSlice slice = new ImageSlice(); slice.init(image, x); _slices[x] = slice; red += slice.getAverageRed(); green += slice.getAverageGreen(); blue += slice.getAverageBlue(); hue += slice.getAverageHue(); sat += slice.getAverageSat(); val += slice.getAverageVal(); } _averageRed = red / width; _averageGreen = green / width; _averageBlue = blue / width; _averageHue = hue / width; _averageSat = sat / width; _averageVal = val / width; sat = 0; val = 0; for (int x = 0; x < width; ++x) { ImageSlice slice = _slices[x]; float deltaSat = slice.getAverageSat() - _averageSat; sat += deltaSat * deltaSat; float deltaVal = slice.getAverageVal() - _averageVal; val += deltaVal * deltaVal; } _deviationVal = (float)Math.Sqrt((val / (float)width) / 255.0); _deviationSat = (float)Math.Sqrt((sat / (float)width) / 255.0); }
private void updateSongNotes() { //ImageSlice slice = new ImageSlice(); //slice.init(image, 4); updateChart(); float[] sliceAvArray = new float[songLength]; float[] filteredSliceAvArray = new float[songLength]; float[] ModifiedSliceAvArray = new float[songLength]; bool[] rests = new bool[songLength]; DrumsWrap drums = new DrumsWrap(); drums.bassNotes = new float[songLength]; drums.hihatNotes = new float[songLength]; drums.rideNotes = new float[songLength]; drums.snareNotes = new float[songLength]; drums.splashNotes = new float[songLength]; float[] melodyNotes = new float[songLength]; int prevSliceNumber = 0; for (int i = 1; i < songLength + 1; i++) { int sliceBack; int sliceNum = (int)(((i / (float)(songLength) * (imageprop.getNumSlices() - 1)))); ImageSlice slice = imageprop.getSlice(sliceNum); ImageSlice filteredSlice = filteredimageprop.getSlice(sliceNum); sliceBack = (int)(((i / (float)(songLength)) * (imageprop.getNumSlices()) - prevSliceNumber)) - 1; ////sliceBack = (int)((i * songLength) / (imageprop.getNumSlices() - prevSliceNumber) - 1); Console.WriteLine("from slice" + (sliceNum - sliceBack) + "to" + sliceNum); float sliceAv = 0; float filteredSliceAv = 0; int numOfLoops = 0; for (int j = i; (j - i) < sliceBack; j++) { int sliceIndex = (int)(((i / (float)(songLength)) * (imageprop.getNumSlices() - 1)) - j); slice = imageprop.getSlice(sliceIndex); filteredSlice = filteredimageprop.getSlice(sliceIndex); sliceAv += slice.getAverageVal(); filteredSliceAv += filteredSlice.getAverageVal(); numOfLoops++; } sliceAv /= numOfLoops; filteredSliceAv /= numOfLoops; i--; // to adjust for array sliceAvArray[i] = sliceAv; filteredSliceAvArray[i] = filteredSliceAv; if (filteredSliceAvArray[i] > filteredimageprop.getAverageVal()) { rests[i] = false; } else { rests[i] = true; } prevSliceNumber = sliceNum; const float bassVolume = 0.6f; const float hihatVolume = 0.6f; const float rideVolume = 0.6f; const float snareVolume = 0.6f; const float splashVolume = 0.6f; const float bassVariation = 0.5f; const float hihatVariation = 0.5f; const float rideVariation = 0.5f; const float snareVariation = 0.5f; const float splashVariation = 0.5f; float change = (slice.getAverageSat() - imageprop.getAverageSat()); float splash = Math.Abs(change) > ((imageprop.getDeviationSat()) * 10) ? 1.0f : 0.0f; switch (mode) { case playbackMode.rock: drums.bassNotes[i] = i % 4 == 0 ? 1.0f : 0.0f; //drums.hihatNotes[i] = [slice getAverageVal] < 60? 1.0f : 0.0f; drums.hihatNotes[i] = 1; drums.rideNotes[i] = 1 - drums.hihatNotes[i]; drums.snareNotes[i] = i % 4 == 2 ? 1.0f : 0.0f; drums.splashNotes[i] = splash; break; case playbackMode.dance: drums.bassNotes[i] = i % 2 == 0 ? 1.0f : 0.0f; drums.hihatNotes[i] = i % 2 == 1 ? 1.0f : 0.0f; drums.rideNotes[i] = 0.0f; drums.snareNotes[i] = splash; drums.splashNotes[i] = i % 4 == 3 ? 1.0f : 0.0f; break; default: break; } if (drums.bassNotes[i] > 0) { drums.bassNotes[i] += -bassVariation / 2 - bassVariation * slice.getAverageRed() / 255.0f; } if (drums.hihatNotes[i] > 0) { drums.hihatNotes[i] += -hihatVariation / 2 + hihatVariation * slice.getAverageGreen() / 255.0f; } if (drums.rideNotes[i] > 0) { drums.rideNotes[i] += -rideVariation / 2 - rideVariation * slice.getAverageBlue() / 255.0f; } if (drums.snareNotes[i] > 0) { drums.snareNotes[i] += -rideVariation / 2 - snareVariation * slice.getAverageRed() / 255.0f; } if (drums.splashNotes[i] > 0) { drums.splashNotes[i] += -splashVariation / 2 - splashVariation * slice.getAverageGreen() / 255.0f; } drums.bassNotes[i] *= bassVolume; drums.hihatNotes[i] *= hihatVolume; drums.rideNotes[i] *= rideVolume; drums.snareNotes[i] *= snareVolume; drums.splashNotes[i] *= splashVolume; i++;//readjust for array } //Gets Highest and lowest slices and maps to a range of 0 - 1 float lowSlice = 0; float highSlice = 0; for (int j = 0; j < songLength; j++) { if (sliceAvArray[j] < lowSlice || j == 0) { lowSlice = (float)sliceAvArray[j]; } if (sliceAvArray[j] > highSlice || j == 0) { highSlice = (float)sliceAvArray[j]; } } for (int i = 0; i < songLength; i++) { sliceAvArray[i] = (sliceAvArray[i] - lowSlice) / (highSlice - lowSlice); } ModifiedSliceAvArray[0] = 0.5f; for (int i = 0; i < songLength - 1; /*!-1 may be bug in obj c code*/ i++) { float step = sliceAvArray[i + 1] - sliceAvArray[i]; if (step > 0) { if (Math.Abs(step) < 0.01) { ModifiedSliceAvArray[i + 1] = step1 + ModifiedSliceAvArray[i]; } else if (Math.Abs(step) < 0.05) { ModifiedSliceAvArray[i + 1] = step2 + ModifiedSliceAvArray[i]; } else if (Math.Abs(step) < 0.1) { ModifiedSliceAvArray[i + 1] = step3 + ModifiedSliceAvArray[i]; } else { ModifiedSliceAvArray[i + 1] = step4 + ModifiedSliceAvArray[i]; } if (ModifiedSliceAvArray[i + 1] > 1.0 && i > 0) { ModifiedSliceAvArray[i + 1] = ModifiedSliceAvArray[i - 1]; } else if (ModifiedSliceAvArray[i + 1] > 1.0) { ModifiedSliceAvArray[i + 1] = step1; } if (ModifiedSliceAvArray[i + 1] < 0.0 && i > 0) { ModifiedSliceAvArray[i + 1] = ModifiedSliceAvArray[i - 1]; } else if (ModifiedSliceAvArray[i + 1] < 0.0) { ModifiedSliceAvArray[i + 1] = step1; } } else { if (Math.Abs(step) < 0.01) { ModifiedSliceAvArray[i + 1] = ModifiedSliceAvArray[i]; } else if (Math.Abs(step) < 0.05) { ModifiedSliceAvArray[i + 1] = ModifiedSliceAvArray[i] - step2; } else if (Math.Abs(step) < 0.1) { ModifiedSliceAvArray[i + 1] = ModifiedSliceAvArray[i] - step3; } else { ModifiedSliceAvArray[i + 1] = ModifiedSliceAvArray[i] - step4; } if (ModifiedSliceAvArray[i + 1] > 1.0 && i > 0) { ModifiedSliceAvArray[i + 1] = ModifiedSliceAvArray[i - 1]; } else if (ModifiedSliceAvArray[i + 1] > 1.0) { ModifiedSliceAvArray[i + 1] = step1; } if (ModifiedSliceAvArray[i + 1] < 0.0 && i > 0) { ModifiedSliceAvArray[i + 1] = ModifiedSliceAvArray[i - 1]; } else if (ModifiedSliceAvArray[i + 1] < 0.0) { ModifiedSliceAvArray[i + 1] = step1; } } } for (int i = 0; i < songLength; i++) { //C Scale float[] scale = { 0.5f, 0.56122f, 0.6299f, 0.6674f, 0.7491f, 0.8408f, 0.9438f, 1.0f, 1.1124f, 1.2600f, 1.3348f, 1.4983f, 1.684f, 1.8877f }; if (!rests[i]) { melodyNotes[i] = getNote(scale, scale.Length, ModifiedSliceAvArray[i]); } else { melodyNotes[i] = 0; } } SetupTrack(6, songLength, melodyNotes, drums); }