Skip to content

Commit

Permalink
Fixed up inverted Hertz conversions for annotation draw surface.
Browse files Browse the repository at this point in the history
Zeroed border model compensation in jquery.drawabox - it seems its not needed.

Added checks for badly sized spectrograms along with automatic cropping to the nearest power of two beneath the problem Image's naturalHeight
  • Loading branch information
atruskie committed Oct 9, 2013
1 parent 1635d88 commit d34e25d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
16 changes: 12 additions & 4 deletions src/common/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ if (!Array.prototype.filter) {
}
}());


(function (undefined) {

var baw = window.baw = window.baw || {};
Expand All @@ -136,7 +135,7 @@ if (!Array.prototype.filter) {
baw.shuffle = function shuffle(list) {
var i, j, t;
for (i = 1; i < list.length; i++) {
j = Math.floor(Math.random()*(1+i)); // choose j in [0..i]
j = Math.floor(Math.random() * (1 + i)); // choose j in [0..i]
if (j != i) {
t = list[i]; // swap list[i] and list[j]
list[i] = list[j];
Expand Down Expand Up @@ -243,10 +242,19 @@ if (!Array.prototype.filter) {
return !isNaN(parseFloat(n)) && isFinite(n);
};

baw.parseInt = function(n) {
baw.parseInt = function (n) {
return parseInt(n, 10);
};

baw.isPowerOfTwo = function isPowerOfTwo(x) {
return (x & (x - 1)) === 0;
};

baw.closestPowerOfTwoBelow = function closestPowerOfTwoBelow(x) {
var power = Math.floor(Math.log(257)/Math.log(2));
return Math.pow(2, power);
};

baw.popUpWindow = function popUpWindow(provider_url, width, height, callback) {
var screenX = typeof window.screenX != 'undefined' ? window.screenX : window.screenLeft,
screenY = typeof window.screenY != 'undefined' ? window.screenY : window.screenTop,
Expand Down Expand Up @@ -283,7 +291,7 @@ if (!Array.prototype.filter) {
return false;
};

function Angular() {
function Angular() {
this.fixedEncodeURIComponent = function fixedEncodeURIComponent(str) {
str = str || "";
return encodeURIComponent(str)
Expand Down
2 changes: 1 addition & 1 deletion src/common/jquery.drawabox.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@
* This is dodgy as fuck - if the border width changes...
* @type {number}
*/
var BORDER_MODEL_DIFFERANCE = 2;
var BORDER_MODEL_DIFFERANCE = 0;

var getBox = function ($element) {
var selectedAttr = $element.attr(SELECTED_ATTRIBUTE);
Expand Down
26 changes: 22 additions & 4 deletions src/components/directives/bawAnnotationViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ bawds.directive('bawAnnotationViewer', [ 'conf.paths', function (paths) {
console.warn("the image width does not3 conform well with the meta data");
}

return { pixelsPerSecond: spectrogramPps, pixelsPerHertz: imagePph};
return { pixelsPerSecond: spectrogramPps, pixelsPerHertz: imagePph, nyquistFrequency: nyquistFrequency };
}

function updateUnitConversions(scope, imageWidth, imageHeight) {
var conversions = {};
if (scope.model.media && scope.model.media.spectrogram){
if (scope.model.media && scope.model.media.spectrogram) {
conversions = unitConversions(scope.model.media.sampleRate, scope.model.media.spectrogram.window, imageWidth, imageHeight);
}

Expand All @@ -63,6 +63,9 @@ bawds.directive('bawAnnotationViewer', [ 'conf.paths', function (paths) {
hertzToPixels: function hertzToPixels(hertz) {
var pixels = hertz * conversions.pixelsPerHertz;
return pixels;
},
invertHertz: function invertHertz(hertz) {
return Math.abs(conversions.nyquistFrequency - hertz);
}
};
}
Expand All @@ -78,10 +81,10 @@ bawds.directive('bawAnnotationViewer', [ 'conf.paths', function (paths) {

if (audioEvent.__temporaryId__ === boxId) {
audioEvent.startTimeSeconds = scope.model.converters.pixelsToSeconds(box.left || 0);
audioEvent.highFrequencyHertz = scope.model.converters.pixelsToHertz(box.top || 0);
audioEvent.highFrequencyHertz = scope.model.converters.invertHertz(scope.model.converters.pixelsToHertz(box.top || 0));

audioEvent.endTimeSeconds = audioEvent.startTimeSeconds + scope.model.converters.pixelsToSeconds(box.width || 0);
audioEvent.lowFrequencyHertz = audioEvent.highFrequencyHertz + scope.model.converters.pixelsToHertz(box.height || 0);
audioEvent.lowFrequencyHertz = audioEvent.highFrequencyHertz - scope.model.converters.pixelsToHertz(box.height || 0);
}
else {
console.error("Box ids do not match on resizing or move event", audioEvent.__temporaryId__, boxId);
Expand Down Expand Up @@ -182,6 +185,21 @@ bawds.directive('bawAnnotationViewer', [ 'conf.paths', function (paths) {

// init unit conversion
function updateConverters() {
if (scope.$image[0].src) {
var natHeight = scope.$image[0].naturalHeight;
if (natHeight === undefined) {
// TODO: handle better
throw "can't determine natural height!";
}
if (natHeight !== 0 && !baw.isPowerOfTwo(natHeight)) {
var croppedHeight = baw.closestPowerOfTwoBelow(natHeight);
console.warn("The natural height (" + natHeight +
"px) for image " + scope.$image[0].src +
" is not a power of two. The image has been cropped to " + croppedHeight + "px!");

scope.$image.height(croppedHeight);
}
}
scope.model.converters = updateUnitConversions(scope, scope.$image.width(), scope.$image.height());
}

Expand Down

0 comments on commit d34e25d

Please sign in to comment.