StringForRate()

Introduce a function to generate the string representation of a bitrate
(kbps, mbps, gbps, etc..)

* Factor out the code from MediaPlayer InfoWindow
* Allow different bases (/1000 or /1024)
This commit is contained in:
Philippe Saint-Pierre 2012-06-25 13:29:22 -04:00
parent 9741d697e8
commit 8d87f2b43a
4 changed files with 90 additions and 7 deletions

View File

@ -0,0 +1,24 @@
/*
* Copyright 2010 Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#ifndef STRING_FOR_RATE_H
#define STRING_FOR_RATE_H
#include <SupportDefs.h>
namespace BPrivate {
const char* string_for_rate(double rate, char* string, size_t stringSize,
double base = 1024.0f);
} // namespace BPrivate
using BPrivate::string_for_rate;
#endif // COLOR_QUANTIZER_H

View File

@ -32,6 +32,7 @@
#include <Mime.h>
#include <NodeInfo.h>
#include <String.h>
#include <StringForRate.h>
#include <StringView.h>
#include <TextView.h>
@ -407,14 +408,10 @@ printf("InfoWin::Update(0x%08lx)\n", which);
}
if (format.type == B_MEDIA_ENCODED_AUDIO) {
float br = format.u.encoded_audio.bit_rate;
if (br > 0.0) {
char rateString[20];
snprintf(rateString, sizeof(rateString), B_TRANSLATE(", %.0f kbps"),
br / 1000);
s << rateString;
}
char string[20] = "";
if (br > 0.0)
s << ", " << string_for_rate(br, string, 20, 1000.0);
}
s << "\n\n";
fContentsView->Insert(s.String());
}

View File

@ -29,6 +29,7 @@ StaticLibrary libshared.a :
RWLockManager.cpp
SHA256.cpp
ShakeTrackingFilter.cpp
StringForRate.cpp
StringForSize.cpp
Variant.cpp
;

View File

@ -0,0 +1,61 @@
/*
* Copyright 2012 Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#include "StringForRate.h"
#include <stdio.h>
#include <SystemCatalog.h>
using BPrivate::gSystemCatalog;
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "StringForRate"
namespace BPrivate {
const char*
string_for_rate(double rate, char* string, size_t stringSize, double base)
{
double kbps = rate / base;
if (kbps < 1.0) {
const char* trKey = B_TRANSLATE_MARK("%.0f bps");
snprintf(string, stringSize, gSystemCatalog.GetString(trKey,
B_TRANSLATION_CONTEXT), (int)rate);
return string;
}
double mbps = kbps / base;
if (mbps < 1.0) {
const char* trKey = B_TRANSLATE_MARK("%.0f kbps");
snprintf(string, stringSize, gSystemCatalog.GetString(trKey,
B_TRANSLATION_CONTEXT), kbps);
return string;
}
double gbps = mbps / base;
if (gbps < 1.0) {
const char* trKey = B_TRANSLATE_MARK("%.0f mbps");
snprintf(string, stringSize, gSystemCatalog.GetString(trKey,
B_TRANSLATION_CONTEXT), mbps);
return string;
}
double tbps = gbps / base;
if (tbps < 1.0) {
const char* trKey = B_TRANSLATE_MARK("%.0f gbps");
snprintf(string, stringSize, gSystemCatalog.GetString(trKey,
B_TRANSLATION_CONTEXT), gbps);
return string;
}
const char* trKey = B_TRANSLATE_MARK("%.0f tbps");
snprintf(string, stringSize, gSystemCatalog.GetString(trKey,
B_TRANSLATION_CONTEXT), tbps);
return string;
}
} // namespace BPrivate