Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3 new encoder methods #10

Merged
merged 4 commits into from
Dec 30, 2016
Merged

3 new encoder methods #10

merged 4 commits into from
Dec 30, 2016

Conversation

dh1tw
Copy link
Contributor

@dh1tw dh1tw commented Dec 30, 2016

this PR supersedes the original PR. Is has incorporated the from @hraban.

It compiles on MacOS and Linux.

Copy link
Owner

@hraban hraban left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice PR, thanks for updating the naming. I'm being critical because I want to make sure this patch is stand alone and we don't start pulling unrelated stuff in. But even those const things I agree, in principle, I just want to keep it separated from this (good) patch.

Thank you very much Tobias, your help is definitely appreciated 🙂 👍

// Optimize encoding for low latency applications
AppRestrictedLowdelay = Application(C.CONST_APPLICATION_RESTRICTED_LOWDELAY)
AppRestrictedLowdelay = C.OPUS_APPLICATION_RESTRICTED_LOWDELAY
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not thrilled about these changes. They're outside the scope of this PR. I have no problem with a separate PR for this, but not as part of this patch. If this turns out to break something, I can't roll it back without deleting your other changes, too.

BandwidthSuperWideBand = C.OPUS_BANDWIDTH_SUPERWIDEBAND
// 20 kHz passband
BandwidthFullband = C.OPUS_BANDWIDTH_FULLBAND
)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Give the bandwidths a separate type and you can remove the Bandwidth prefix from the variable names.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

// Auto bitrate
Auto = C.OPUS_AUTO
// Maximum bitrate
BitrateMax = C.OPUS_BITRATE_MAX
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the maximum bitrate? Then MaxBitrate is a better name, like math.MaxInt for example.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I can sure rename it, although I thought it tried to be with the names as close as possible with the opus macros from C. It makes life easier understanding the documentation.

@@ -62,7 +129,7 @@ func NewEncoder(sample_rate int, channels int, application Application) (*Encode
// Init initializes a pre-allocated opus encoder. Unless the encoder has been
// created using NewEncoder, this method must be called exactly once in the
// life-time of this object, before calling any other methods.
func (enc *Encoder) Init(sample_rate int, channels int, application Application) error {
func (enc *Encoder) Init(sample_rate int, channels int, application int) error {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here, too, I'm not a fan of including these unrelated changes in this patch. this patch is about adding encoder settings, but these changes are not necessary for that. I'm not opposed to them (I agree; they should be const, not var), but this PR is not the place to do that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok - I understand. I will revert it.

@hraban
Copy link
Owner

hraban commented Dec 30, 2016

forgot to mention that I'm especially grateful for the unit tests. They're a pain to ask for and unrewarding, but these are thorough and good. thanks

@hraban
Copy link
Owner

hraban commented Dec 30, 2016 via email

@dh1tw
Copy link
Contributor Author

dh1tw commented Dec 30, 2016

So I guess you would also expect a type Bitrate ? And the methods (enc * Encoder) Bitrateand (enc *Encoder) SetBitrate to accept / return Bitrate types, right?

// SetBitrate sets the bitrate of the Encoder
func (enc *Encoder) SetBitrate(bitrate Bitrate) error {
	res := C.bridge_encoder_set_bitrate(enc.p, C.opus_int32(bitrate))
	if res != C.OPUS_OK {
		return Error(res)
	}
	return nil
}

// Bitrate returns the bitrate of the Encoder
func (enc *Encoder) Bitrate() (Bitrate, error) {
	var bitrate C.opus_int32
	res := C.bridge_encoder_get_bitrate(enc.p, &bitrate)
	if res != C.OPUS_OK {
		return 0, Error(res)
	}
	return Bitrate(bitrate), nil
}

@hraban
Copy link
Owner

hraban commented Dec 30, 2016

No the bitrate is fine as an int because it is an actual integer. It's not an enum, it's the actual number used to represent the bitrate. So an int makes sense here.

As a rule of thumb you can try this: does it make sense to use + or - on the value? Then it's a number.

@dh1tw
Copy link
Contributor Author

dh1tw commented Dec 30, 2016

Ok. But as far as I can see there are also two Enums available for SetBitrate:

#define OPUS_AUTO                           -1000 /**<Auto/default setting @hideinitializer*/
#define OPUS_BITRATE_MAX                       -1 /**<Maximum bitrate @hideinitializer*/

So in this case there are Enums and integers.

@hraban
Copy link
Owner

hraban commented Dec 30, 2016

Good point, however these are not enums but magic values. It's a common practice in C but it's not Go-like. Especially AUTO, which is just a terribly confusing thing because it's an int but not really, and you can pass it to two different functions, and its semantics depend on the function you pass it to, it's really kind of weird. I was planning to remove Auto from your PR after it's done and replace it with this, which, imo, is more Go like:

SetBitrate(br int) error { /* normal */ }
SetBitrateAuto() error { /* sets it to Auto. */ }
SetBitrateMax() error { /* sets it to max */ }

This completely hides the implementation details of that ugly OPUS_AUTO value from the caller and gives a nice, clean, Go-like API.

@dh1tw dh1tw mentioned this pull request Dec 30, 2016
@dh1tw
Copy link
Contributor Author

dh1tw commented Dec 30, 2016

updated the Pull Request. I hope that it matches now your expectations!

Copy link
Owner

@hraban hraban left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

two small typos left.

return nil
}

// Complexity returns the bitrate of the Encoder
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo in the comment :)


err = enc.SetBitrateMax()
if err != nil {
t.Error("Error setting Auto bitrate:", err)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrong error message

@hraban
Copy link
Owner

hraban commented Dec 30, 2016

it looks great. thanks for implementing the Auto() and Max() variations.

@dh1tw
Copy link
Contributor Author

dh1tw commented Dec 30, 2016

fixed the typos... tnx for carefully checking!

@hraban hraban merged commit dc3e8b1 into hraban:master Dec 30, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants