Avatar

Please consider registering
Guest

sp_LogInOut Log In sp_Registration Register

Register | Lost password?
Advanced Search

— Forum Scope —




— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

sp_Feed sp_TopicIcon
combination: constructor / prepareForPlay
No permission to create posts
March 7, 2015
4:57 pm
Avatar
JD Young
Leiden, The Netherlands
Admin
Forum Posts: 80
Member Since:
November 5, 2014
sp_UserOfflineSmall Offline

Hi Will and Tom,

I have a question about the conventions of using a combination of the constructor and prepareForPlay() functions. As I go through the examples in the FX book, I notice that some functions placed in prepareForPlay() don’t seem necessary. I was wondering if it’s just good practice to include them there as well, or if there might be another reason to include them.

For example, when you code an EQ, and you have a function calculating the biquad coefficients. If you add this function in the constructor and in userInterfaceChange(), there would be no need to add it to prepareForPlay(), would there? (as long as you do flush the delay elements here).

All the best,

JD

EDIT: I guess in this example it has to do with updating the samplerate in prepareForPlay(). That should answer my question in all cases I think; I'll check ;) Does that mean that if you have a function to calculate the value of some variables which includes the samplerate in its calculation, you never include this function in the constructor?

March 7, 2015
11:57 pm
Avatar
W Pirkle
Admin
Forum Posts: 143
Member Since:
January 28, 2017
sp_UserOfflineSmall Offline

Hi JD

Yes, anything that is sample rate (or bit-depth or channel count) dependent or anything that must be cleared/reset/recalculated out on a per-run basis needs to be updated in prepareForPlay().

The filtering objects have their default sample rate set in their constructors, so initializing them in the constructor of the plugin is OK - they won't have garbage or 0.0 coefficients. I almost always initialize every member variable in the constructor, even if I know the value will get overwritten later.

- Will

March 8, 2015
12:08 am
Avatar
JD Young
Leiden, The Netherlands
Admin
Forum Posts: 80
Member Since:
November 5, 2014
sp_UserOfflineSmall Offline

Hi Will,

Thanks, that’s very helpful :) I have written some classes of my own the last couple of weeks. They do the job, but sometimes I get a bit confused with these class-within-class structures haha. I will implement a default samplerate too then in my classes when appropriate… Good to know that it’s not strange to initialize variables even when you know they will be overwritten. Better safe than sorry!

All the best,

JD

March 13, 2015
5:05 pm
Avatar
Tom
Admin
Forum Posts: 65
Member Since:
April 3, 2014
sp_UserOfflineSmall Offline

Hi JD,

not only is it "ok" to initialize variables in the constructor but it actually is a must ;-) Depending on your compiler and warning level it will output a warning if you don't do it. Some programming languages won't allow at all to leave the constructor without having touched all variables (or they have a built-in initialization).
In general your objects must/should not ever be in an undefined state but which is what happens if you don't intialize variables. "Interesting" things can happen this way, so better get into the habit of initializing everything always ;-)
C++11 allows you to initialize basic types directly with the declaration (e.g. int m_myVar = 42;). This way you can initialize stuff correctly without explicitly implementing a constructor at all (of course this works only for simple classes).
Tom

March 18, 2015
3:22 pm
Avatar
JD Young
Leiden, The Netherlands
Admin
Forum Posts: 80
Member Since:
November 5, 2014
sp_UserOfflineSmall Offline

Thanks for your explanation, Tom. That is very useful advice!

Forum Timezone: America/New_York

Most Users Ever Online: 36

Currently Online:
5 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

Skyler: 47

Peter: 41

Derek: 41

clau_ste: 39

Frodson: 38

Gwen: 32

EZB: 24

lppier: 23

Msaldaña: 18

Jorge: 17

Member Stats:

Guest Posters: 1

Members: 477

Moderators: 1

Admins: 4

Forum Stats:

Groups: 11

Forums: 30

Topics: 483

Posts: 1877

Newest Members:

Jaggxn, sam, annaharris, Marie Weaver, kev, Steven, Mr Anderson, mguy, omelc

Moderators: W Pirkle: 143

Administrators: Tom: 65, JD Young: 80, Will Pirkle: 0, W Pirkle: 143