comparison foosdk/sdk/foobar2000/helpers-mac/fooDecibelFormatter.m @ 1:20d02a178406 default tip

*: check in everything else yay
author Paper <paper@tflc.us>
date Mon, 05 Jan 2026 02:15:46 -0500
parents
children
comparison
equal deleted inserted replaced
0:e9bb126753e7 1:20d02a178406
1 #import "fooDecibelFormatter.h"
2
3 @implementation fooDecibelFormatter
4
5 - (NSString*) formatNumber: (id) obj {
6
7 double v = [ obj doubleValue ];
8
9 if ( self.showSignAlways ) {
10 if (v == 0) return [NSString stringWithUTF8String: "\xc2\xb1" "0"];
11 if ( v > 0 ) {
12 if (v == (double) (int) v) return [NSString stringWithFormat: @"+%i", (int)v];
13 else return [NSString stringWithFormat: @"+%.02f", v];
14 }
15 }
16
17 if (v == (double) (int) v) return [NSString stringWithFormat: @"%i", (int)v];
18 else return [NSString stringWithFormat: @"%.02f", v];
19 }
20
21 - (NSString *)stringForObjectValue:(id)obj {
22 return [NSString stringWithFormat: @"%@ dB", [self formatNumber: obj]];
23 }
24
25 - (NSAttributedString *)attributedStringForObjectValue:(id)obj withDefaultAttributes:(NSDictionary *)attrs {
26 return nil;
27 }
28
29 - (NSString *)editingStringForObjectValue:(id)obj {
30 return [self stringForObjectValue: obj];
31 // return [self formatNumber: obj];
32 }
33
34 - (BOOL)getObjectValue:(out __autoreleasing id *)obj forString:(NSString *)string errorDescription:(out NSString *__autoreleasing *)error {
35 if (error) *error = nil;
36
37 {
38 NSMutableCharacterSet * trimMe = [NSMutableCharacterSet whitespaceAndNewlineCharacterSet];
39 [trimMe addCharactersInString: [NSString stringWithUTF8String: "+\xc2\xb1" ]];
40 string = [ string.lowercaseString stringByTrimmingCharactersInSet: trimMe ];
41 }
42
43 if ( [string hasSuffix: @"db"]) {
44 string = [ [string substringToIndex: string.length-2] stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet] ];
45 }
46
47 double v = string.doubleValue;
48 if (self.minValue) {
49 if (v < self.minValue.doubleValue) v = self.minValue.doubleValue;
50 }
51 if (self.maxValue) {
52 if (v > self.maxValue.doubleValue) v = self.maxValue.doubleValue;
53 }
54 *obj = [NSNumber numberWithDouble: v];
55 return YES;
56 }
57
58 @end