transistor gain and loudspeaker

That depends on the transistor, but misses the most important point, a (moving coil) loudspeaker needs
AC drive with no/little DC bias (else it will travel to the end-stops and get hot and distort the
sound).

Therefore I suggested another simple wiring - see above. Transformer does remove DC too..

The right base resistor to get is easy for that wiring:

  1. decide on the required collector current, for example Ic=20mA.
  2. look at datasheet - hfe is the gain under similar conditions, let say 100.
  3. you need base current Ib = Ic/hfe, Ib = 200uA
  4. base resistor value will be
    Rb = (5V - Ube)/Ib = (5 - 0.7)/0.0002 = 21500 ohm, for this wiring, approximately (some may suggest lower values, if saturation is required).

The biq Q is how to decide on the Ic. If you want PWM the amplifier, the design has to consider that. As a beeper it may work that way (the loudspeaker coupled via capacitor or a transformer).